Skip to content

Instantly share code, notes, and snippets.

@aturon
Last active August 29, 2015 14:01
Show Gist options
  • Save aturon/3c09c2cf77b27f38d5c7 to your computer and use it in GitHub Desktop.
Save aturon/3c09c2cf77b27f38d5c7 to your computer and use it in GitHub Desktop.
can add mut to self in trait impl
// The trait itself says the method takes self as immutable
trait ImmutableOp {
fn do_immutable(self);
}
impl ImmutableOp for Vec<u8> {
// FAILS TO COMPILE, AS IT SHOULD:
// fn do_immutable(self) {
// self.push(0);
// }
// COMPILES FINE; SHOULD IT?:
fn do_immutable(mut self) {
self.push(0);
}
}
fn main() {
// immutable binding
let v = Vec::new();
// mutated
v.do_immutable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment