// print:
// string-a
// string-aaabc
fn main() {
let mut a = String::from("string-");
let s = &mut a;
let ss = &mut a; // s goes out of scope here
ss.push('a');
// string-a
println!("{}", ss.clone()); // ss.clone() ==> borrow shared reference, ss(mutable reference) should be gone out of scope. but it still available. why ???
ss.push_str("aa");
let b = &mut (*ss); // borrow '&mut (*ss)' twice to mutable, ss shoudle be gone out of scope. but it still available. why ???
// uncommenting,error(cannot borrow `*ss` as mutable more than once at a time)
// let b = &mut (*ss); ==> first mutable borrow occurs here ==> why?
// ss.push('d'); ==> second mutable borrow occurs here ==> why?
// b.push('b'); ==> first borrow later used here
// why ?
// ss.push('d');
b.push('b');
ss.push('c'); // why? ss still available!
println!("{}", a); // string-aaabc
}
This example subverts my understanding of ownership and reference scope. I'm confused. Questions are like code comments