-1
// 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

1

2 Answers 2

1
// 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.clone borrows ss, it doesn't take ownership. When clone returns, it releases the borrow and so ss is still available. The fact that ss is an exclusive reference (&mut) rather than a shared reference (&) doesn't change anything here. It only means that no one could have used ss until clone returned, which is impossible anyway.

    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 ???

This is called a reborrow. At this point:

  • a is borrowed exclusively by ss, so a can't be used until ss gets dropped.
  • ss is borrowed exclusively by b, so ss can't be used until b gets dropped. Note that ss is only borrowed by b, not moved, so ss will become available again once b releases the borrow.
    // 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 is still alive and holds an exclusive borrow of ss, so ss can't be accessed directly.

    b.push('b');

This is the last time we use b, so b can be dropped now thanks to non-lexical lifetimes, and ss becomes available again.

    ss.push('c'); // why? ss still available!

b only borrowed ss, it didn't take ownership. So now that b has been dropped, the borrow is released and ss is available again. For exactly the same reason that a is available again on the next line once ss has been dropped:

    println!("{}", a); // string-aaabc
}
0
  1. First, the variants and references are two different things. In general, when you're done using a variant, rust will automatically drop that variable for you.

    let s = &mut a; // s dropped, because it's not used afterwards.
    
  2. clone: only reference for ss

  3. let b = &mut (*ss); In here, you're not borrowing the ss variable instead of getting raw reference a that is raw pointer metadata saved by ss:

    b  --> &mut a
    ss --> &mut a 
    
    • However, problems can be occurred when you use mutable borrowing.
     let b = &mut (*ss); // -------
                         //        |
     b.push('b');        //   ------
     // variant `b` was dropped
     // in here, you can just use `ss`
    

If you write it like as follow, which is your confusing, then

//...
 ss.push('c'); // cannot borrow `*ss` as mutable more than once at a time second mutable borrow occurs hererustcClick for full compiler diagnostic

 b.push('b'); // `b` was not dropped
//...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.