r/learnrust • • 4d ago

Borrow Checker

So , i read the original book ownership chapter it made sense but then i went to brown unverisity and boy i cant wrap my head around the same concept
see this what i know are:
1.when we create a mutable or immutable refernce to the original owner or its value with *
it removes corresponding permissions
2. When we create a reference pointing to another refernce this part i dont understnd like see above ss it says that after we assign z a mutable reference to *y , y and *y both lose write permission if they both lose write permission then how can i be able to reassign y in next step???

This is my own understanding but if u create a immutable refernce to a *y it only removes write from *y and not Y and if u make a immutbale reference to y itself it removes write from both

plus the rest of book has permisson system too so should i forget what i learned al together and focus on main book or is there a way to learn this permisson thing

8 Upvotes

15 comments sorted by

3

u/garagedragon 4d ago edited 4d ago
  1. You don't create a reference with *, you use either & or &mut. * is applied to references and pointers to get you the underlying value those things are pointing at. The only reason that * works in this context is because the value x is a Box which is itself a (smart) pointer to another value. If you'd started with let x = 1, then the * would give you a type error because you can't dereference plain numbers. I don't know why your example includes Boxes here, they just add a layer of complication.
  2. AIUI, the line let z = &mut *y means that z borrows the value y was pointing at, not the variable y itself. It is not a reference to a reference, it is a reference directly to the 10 that y was pointing at. That means that after assigning z, you cannot use *y for anything (because you'll try to work on the same value that z is pointing to) but by setting y (not *y) you change the value that y is pointing at to the same one as x1 is pointing at (but again, not x1 itself). I'm not entirely sure why the borrow checker allows that, if it even does, but if it does, it's because Box counts as owning the value. That'd mean you can change the value inside x1 only by writing through y, and you can't use x1 until y drops/reaches end of scope.

1

u/Status-Aside-5583 4d ago

Man what U said makes sense My point is why permission system in one line says y lost all permissions including write when I assigned it's value reference to z And in the next line it said that y now "magically" can be reassigned to the value reference of x1 And has W now. Rest of things like dereferencing etc I know This permission system is confusing that's all

1

u/garagedragon 4d ago edited 4d ago

I think the image is just wrong about that. You only lose permission to write to the value itself (not what the value refers to) if another reference points at the value. If z were a reference-to-reference (because you'd written z = &mut y) then that'd block you from changing the value of y except via z and the next line wouldn't work, but it's actually a direct reference to the 10, which means it "takes over" from y. This gives you the situation where the 10 can be modified by using *z, but can't be changed with *y, but y itself can be pointed at something else. The reason y can be pointed at something else is because we defined it let mut y, which is distinct and independent of whether we say &mut to create the reference we want to assign to y. We could not re-direct z, because z is not mutable. (let z)

z = &mut *y "taking over" the mutable borrow (which must be unique) from y is called "re-borrowing" if you want to look for more detail.

1

u/Status-Aside-5583 4d ago

Eggzactly man now U got it Btw I am on chapter 4 what U recommend this brown book confusing as hell at least this chapter Or forget it and use original book

1

u/garagedragon 4d ago

it sounds like the original book worked better for you so I'd stick with that

2

u/paulstelian97 3d ago

I think borrow checker converts that line as “z = y” which is valid, leaving y empty and unusable until reassignment.

1

u/Discordbothost 3d ago

Try *z += 1 followed by *y += 1 after let z = &mut *y. That compiles because the reborrow ends after the last use of z. With let z = y, the same test gives E0382 because y was moved.

1

u/paulstelian97 3d ago

So it does make a nested lifetime and allow non-lexical lifetimes to end z and restore y as needed. Hmm.

3

u/rnottaken 4d ago

Your post is a bit hard to parse. Maybe you can explain with a code example? And where you're getting stuck

But if i understand correctly.

A mutable reference actually "removes " the permissions that ANY other variable can read AND write. So it is the only one with those permissions

A non-mutable reference "removes" the permissions that ANY other variable can write. But al other variables/references can still read.

Also, * is used to DE-reference, &(mut) is used to create references.

Does that help?

1

u/Status-Aside-5583 4d ago

What I mean is according to brown book There are three type of permissions R W and O When we create a mutable reference of an owner The owner loses all 3 and The reference variable gets R O and W if it's mut And it's referenced value gets R and W If it's immutable the referenced value gets R only and the owner loses all except R These make sense To have multiple readers but one writer The problem is : See the above ss I create a mutable reference to value of X and sorted it in y So X lost all 3 y got all 3 and it's value got r and W Now when I store the immutable reference of mutable reference of y value in z The permissions says that y loses all 3 and it's value both r and W It make sense that value loses all as mutable reference takes r and W But how come Y lost write permission too And if it did lost how come I was able to re assign it to X1 value later That's what I meant. In the end I said that should I just move on with original non brown book as these permissions are going round and round or is there a way to understand the permissions. That's all

3

u/garagedragon 4d ago edited 4d ago

I'm sceptical of this book's framework, particularly because I don't know what they mean by "ownership permission." A mutable borrow can read and write the value, but only the owner can move and consume it. A mutable borrow doesn't inherit that, you can only move or drop a value when it has no mutable borrows. You may not have run into the distinction because simple types like numbers implement Copy, which loosens the rules, but that is not the default. (There's also std::mem::replace to take ownership of a value that you have a mutable reference to, but you need to replace it with something of the same type, and if that value is, e.g. a file handle, you might not be able to get a sensible replacement.)

The other thing that the image you posted isn't clear on is that references are themselves values, you can borrow them, and the same rules apply to those borrows. That means in the book's framework, a mutable reference has permissions to read and write (and "own"?) its target, but it also ought to have permissions associated to the reference itself - you read, write and own the reference independent of the thing its pointing at. The images don't seem to distinguish those things.

2

u/SirKastic23 4d ago

Honestly, I found the brown book to confuse me a lot more than just learning the borrow checker the normal way. Just read the official book

2

u/Middle-Year-9185 4d ago edited 4d ago

I've tried to skim through the Brown's book some time in the past, but quickly changed my mind. They have some (needlessly) opinionated and, dare I say, weirdly "unique" takes on the matter. Unless you're planning to work closely with whomever wrote that book and/or study Rust in that uni yourself, I'd skip it altogether.

Every value has a single* owner. `let (mut) x = ...` defines exactly that. Taking a mutable/exclusive reference via `&mut x` prohibits doing anything else, while that reference remains outstanding. Which includes both taking other mutable/immutable references, or dropping/deallocating the value itself. Taking a plain immutable/shared reference via `&x`, on the other hand, lets you create as many other shared references as you need. The owner gets to clean up / run the destructor for the underlying value when it goes out of scope. That's all there is to it.

Sure, you can break it down into a matrix of R-W-O permissions, track those via some weirdly obscure arrows throughout the given scope, elaborate on each and every permission "flag" is removed at which exact point in the code, and so on. But why? If you own the value, you clean it up. If you take a `&` to it, you're "locking" it in a read-only mode. If you take `&mut` to it, you're allowed to do anything you want with it - other than drop it, which is still the responsibility of the owner - but for you to keep on doing whatever while that reference remains outstanding, nobody else must be able to trying to access or read or clean it up. Which is what the compiler tracks for you.

* this becomes a bit fuzzier with smart pointers and/or reference counters, but the general idea still holds

2

u/SirKastic23 4d ago

Yeah I agree, I think the brown book is a diservice that makes Rust feel more confusing than it actually is.

I hate that the official book links to it in one of the very first pages. Now whenever I share the book with someone I skip the introduction so they don't get tempted to go to the brown book...

2

u/Aaron1924 3d ago

This section in the Brown university version of the Rust book essentially teaches you how to use Aquascope, a tool for visualizing borrow checking information, also developed at Brown university.

This tool is designed to give you as much information about ownership as possible, so e.g. if you're not sure why you can't "write" into a variable at a certain point in the program, this tool tells you exactly where you lost "write permissions" for the variable. The downside of this approach is that (1) it teaches you to think of every variable as having multiple boolean permission flags which is a lot to keep in your head at once, and (2) the compiler and community don't speak the language of permission flags; rustc error messages will always talk about stuff being borrowed rather than permission flags missing.

If this part of the Brown book helps you understand ownership better, it served its purpose, if it just confuses you, just ignore it.