I just started on Rust couple days ago and work through concept of ownership now.
So ok - you can't have more than one mutable reference - I dig that - so this fails:
let mut s4= String::from("ZOOT");
let s5 = &mut s4;
let s6 = &mut s4; //error here - second mutable borrow occurs here
s5.push_str(" suit"); //added this line to force error on s6
but why is this allowed???
let mut s4= String::from("ZOOT");
let s5 = &mut s4;
s5.push_str(" suit");
let s6 = &mut s4; //but WHY NOT here?
and yes println!("s6 = {} ", s6);
prints ZOOT suit
.
PS. I do understand that after the line let s6 = &mut s4;
the s5
is no longer accessible - if you try then suddenly compiler wakes up and throws error on s6
declaration. But should it throw fit - on the second reference to s4
with or without s5
being used to modify content of s4
?