Hacker News new | ask | show | jobs
by insanitybit 1262 days ago
The rust borrow checker has been described in trivial terms many times. The first time I had it explained, as I recall, was as a book.

You own a book.

`&` - You can lend others the book, they can't fuck with it. `&mut` - You can lend the book to one person, they can fuck with it `move` - You give someone else the book, it's theirs now

Or `many reader NAND one writer`

Is this a complete explanation? No. But it's quite simple and you can be plenty productive with just this amount of understanding.

Your experience is not my experience. I used Rust for the first project I wrote as an intern after dropping out, having never used it before, and I even used it to interact with mysql, which I had never used before.

At my last company I had multiple people pick up rust in a matter of days.

I'm not denying rust as being difficult, I'm saying it's easy for some and hard for some. I found it easy, I was writing productive code on day 0 with virtually no preparation other than that I wanted to try it out. Many people find it easy. Obviously some people, many people, find it hard. Life's weird like that.

1 comments

OK, simple issue that a beginning Rust user runs into immediately:

  - function arguments are moved into the called function

  - you can call a function with a ref, and then you can keep using it in the calling function, because there is a blanket impl of Copy for refs

  - you can call a function with a ref mut, and then you can keep using it in the calling function because ... ???
Because when you pass by reference, you lend the value (or to phrase it the other way around: the function borrows the value). And so when the function’s done, it gives you the value back (because that’s how borrowing works, in the everyday sense of the word, as well as in th Rust sense)
This is either wrong or incomplete. When you call a function `f()` with some `&mut`, the callee gets a `&mut`, not a borrowed `&mut &mut`. The callee doesn't borrow the ref mut, it gets the actual ref mut, and for some time you have multiple mutable references in the same scope. How?
Because the calling function isn't using it at the time. Rust isn't proving something about the number of references in memory; it's proving something about the dynamic behaviour of the program, i.e. that there can be at most one reference actively being used to mutate it, at a time.

Hence there's no problem cloning a mutable reference implicitly, when calling a function. Notice that you can't for instance send it to another thread, however.

There are not multiple mutable references in the same scope. The mutable reference is only valid for the scope of the function `f()`. This doesn't sound like a problem understanding mutable references but a problem understanding scope.
It gets `&mut *&mut` (where * doesn't mean a pointer, just deref)

Not sure if that's good that this feature is automatic. Maybe it wouldn't be too bad to call functions like that for consistency:

    go_break_stuff(&mut *my_stuff_ref);
if you want to keep using the reference after the call.
The function isn’t borrowing the reference. It’s using the reference to borrow the value thats being pointed to by the reference.
I was looking for the word "reborrowing," which I think is crucial for beginners trying to form an effective mental model of Rust semantics.
You add '.clone()' or '&', the code compiles, you move on.