r/learnrust • u/Status-Aside-5583 • 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
3
u/garagedragon 4d ago edited 4d ago
*, 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 valuexis aBoxwhich is itself a (smart) pointer to another value. If you'd started withlet 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.let z = &mut *ymeans that z borrows the valueywas pointing at, not the variableyitself. It is not a reference to a reference, it is a reference directly to the10thatywas pointing at. That means that after assigningz, you cannot use*yfor anything (because you'll try to work on the same value thatzis pointing to) but by settingy(not*y) you change the value thatyis pointing at to the same one asx1is pointing at (but again, notx1itself). I'm not entirely sure why the borrow checker allows that, if it even does, but if it does, it's becauseBoxcounts as owning the value. That'd mean you can change the value insidex1only by writing throughy, and you can't usex1untilydrops/reaches end of scope.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?
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/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.