1

This exercice from Rustlings, once corrected, gives:

 fn move_semantics4() { let mut x = Vec::new(); let y = &mut x; y.push(42); let z = &mut x; z.push(13); assert_eq!(x, [42, 13]); } 

If I put it through Aquascope, on the borrow checker, it gives:

Aquascope borrow checker explanation

I don't understand why x is loosing ownership on L3 and L5. AFAIK, the mutable reference assignated to y and z implies:

  • No more reference can be created on x (only one mutable reference at a time)
  • Ability for y and z (sequentially) to read and write on vector

But not loosing ownership.

According to me, visual representation seems validating the ownership staying on x:

Memory visual representation

Could someone elaborate on this?

EDIT: for the sake of completness, I created another snippet with both move and mutable reference, and there is a special char (fat arrow) when a move is made :

enter image description here

2
  • 2
    No. x keeps being the owner of the Vec during the entire snippet. Commented Aug 21, 2024 at 10:00
  • I visited that Aquascope site, and I could not find any key on the site defining what O (or W and R) stand for. Commented Aug 21, 2024 at 10:05

1 Answer 1

3

On Aquascope website, you can hover the pictograms like +R/-R/+W/-W/+O/-O and arrows like , and it shows a popup tooltip on the meaning (the same can be also viewed in raw HTML tree).

In particular, it shows:

+O Path did not have own permissions before the preceding line, and gained it after this line"

-O Path had own permissions before the preceding line, and lost it after this line.

Path is borrowed here

So after all, line 3, the action on x → -O can be understood as x is borrowed here, and "loss of own permission" doesn't mean that x no longer owns the object, it means that it can no longer transfer ownership to anybody, because a borrowing exists. Meanwhile, y → +O means that y object is instantiated, but it's a reference, and it can do ownership transfer of the reference, not the referenced object.

On line 4, the lifetime of y ends, it's destroyed, thus x → +O received the ability to transfer the ownership (though it always held the ownership)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.