rustborrow-checkermutableownership

Does a mutable reference means an ownership loss in Rust?


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:

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


Solution

  • 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)