Why do I borrow twice in this example?
fn main() {
let mut my_string = String::from("this is a string");
let mut other_string_ref = &mut my_string; // first mutable reference
mut_ref(&mut my_string);
mut_ref(&mut other_string_ref); // invalid, because borrowed twice
}
// borrow happens when this function is called
fn mut_ref(s: &mut String) {
print!("{}\n", s)
}
I know that you can only borrow once to avoid memory races. I also know that a variable goes out of scope when it is no longer used. Why does my_string
not go out of scope after mut_ref(&mut my_string);
. It is not used afterwards. We only use other_string_ref