Last active
August 29, 2015 14:01
-
-
Save aturon/3c09c2cf77b27f38d5c7 to your computer and use it in GitHub Desktop.
can add mut to self in trait impl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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