4 ms·
A little mental model I used to explain ownership and borrowing to someone who is new to Rust. I hope others find it helpful!
by emschwartz 4y ago
A little mental model I used to explain ownership and borrowing to someone who is new to Rust. I hope others find it helpful!
- hannofcart 4y agoIt's helpful. It's especially helpful to people who are both new to Rust AND come at Rust from language ecosystems with a garbage collector. People are new to Rust but have come to it from a C/C++ have had to worry about ownership all the time, so they might not find anything here a revelation except this: "Oh wow, the Rust compiler takes care of all of this for me! No inadvertent memory leaks?! Yaay!"
- emschwartz 4y agoJust want to say that I appreciated this comment when I read it earlier in the day and when reading through the thread again just now. > It's especially helpful to people who are both new to Rust AND come at Rust from language ecosystems with a garbage collector. This is exactly the audience I was writing for so I'm very happy that you thought so.
- cornholio 4y agoPerhaps add a part on the syntax, i.e: passing a reference to a function, then passing that along to some other function, declaring that function as accepting a mutable reference to a vector, versus having a mutable parameter that is an (unmutable) reference to that vector, versus declaring that parameter is a reference to a that mutable reference; same for closures, what does a &¶meter even means, when to use & in the declaration and when to *deref at the point you use it, iterating by default "for x in Vec" versus iterating &Vec, versus using .iter() and .into_iter() etc. etc. Those subtle distinctions were the overwhelming force leading me to ampersand driven development in the early days, i.e I had the "pointer" mental model and tried to shoehorn that into Rust somehow and get that pointer to work like it does in C. I don't think I saw a simple write-up with clear examples of what to do and what not aimed for beginners, seasoned rusters just act like slick professionals that wouldn't make such nonsensical silly mistakes and beginners are to somehow emulate their genius.