Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Last active December 15, 2015 22:09
Show Gist options
  • Save JohnMurray/5331186 to your computer and use it in GitHub Desktop.
Save JohnMurray/5331186 to your computer and use it in GitHub Desktop.
struct Bob {
priv name : ~str,
priv age : int
}
impl Bob {
fn new(name: ~str, age: int) -> Bob {
Bob { name: name, age: age }
}
fn get_older(&self) -> () {
// This should work as long as self is mutable right??
// Obviously it doesn't though (compile error)
self.age += 1;
}
}
fn main() -> () {
let mut bob = Bob::new(~"Roberto", 35);
}
@JohnMurray
Copy link
Author

To post an answer to my issue above (thanks to strcat in the rust irc), I should have been using mutable self such as:

impl Bob {
    fn get_older(&mut self) -> () {
        self.age += 1;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment