Re:Rust Ownership Rules
by bschwindHN on Tuesday March 03, 2020 @01:18AM
(#22472258)
I always liked to think of it in terms of car ownership. You can immutably borrow my car (&car) but if you modify it, you're a jerk (you obviously can't modify it in Rust). Or if you're a mechanic you can mutably borrow (&mut car) it and upgrade it or something. Orr, if I sell my car to you, you've taken ownership of it.
fn borrow_car(car: &Car); fn modify_car(car: &mut Car); fn sell_car(car: Car);
Obviously there'd be other parameters in the functions, but yeah that's the gist of it.
The analogy may not be perfect but the point is complete control of the object has been transferred to the function and so you no longer have access to it and it will be destroyed once the function ends, unless it's returned.