Skip to content

Instantly share code, notes, and snippets.

@wycats
Created July 15, 2014 16:33
Show Gist options
  • Save wycats/12d4b0d3a1628984a472 to your computer and use it in GitHub Desktop.
Save wycats/12d4b0d3a1628984a472 to your computer and use it in GitHub Desktop.
struct Person {
first: String,
last: String
}
fn main() {
let person = Person { first: "Alex".to_string(), last: "Matchneer".to_string() };
yehuda(&person);
}
// this is a compile-time error
fn yehuda(person: &Person) {
person.first = "Yehuda".to_string() // lol
set_yehuda(person) // this is also an error, because I don't have a mutable Person to send
}
// this is valid, because you borrowed mutably
fn set_yehuda(person: &mut Person) {
person.first = "Yehuda".to_string()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment