Created
July 15, 2014 16:33
-
-
Save wycats/12d4b0d3a1628984a472 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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