Help understand the ownership

Below references are from the official language book, highlighted in red circle.


Why "v" & "a" lost 'R' permission, usually after reference 'W' & 'O' will be lost? as per below reference, where in another example 'R' permission is retained!!

Because &mut references are exclusive. While it is active (i.e. neither dropped nor reborrowed), nothing else can have any permissions on its target.

I think you’re correct, the annotations in this example (the first screenshot) are wrong. Probably they didn’t notice the error as it’s hidden behind this “…”-button. Creating a borrow like in

let v: Vec<i32> = vec![0, 1, 2];
let n_ref: &i32 = &v[0];

should definitely not take away read permissions from v.

It looks like this is the issue in their visualizations:

So apparently, it’s not completely wrong – just very misleading – their visualization tool removes the read permission at this point only because v is no longer directly used/mentioned after this point.

Note that this isn’t the official version of the book, and these visualizations in particular are not part of the Rust project, either. Furthermore, I think the whole ownership chapter they have is also completely different. To be honest, I would really wish for a clearer indication on their page, on which parts are adapted from the normal version of the book, and which parts are new. Especially with this chapter, I’m convinced many readers would appreciate the info that they could additionally read the original version for a different presentation on the same general topic. (In particular, I don’t even find any super clear indication – within their book version – that where the visualizations come from, or that they are automatically generated and could contain bugs :man_shrugging:

4 Likes

Thank you