by Waterluvian · 6y ago · 0 comments
This was from the methods chapter but I think it applies globally. You either borrow to read, borrow to mutate, or consume (and possibly return a different value back)
I'm sure there will be more nuance but I think this is a good conceptual foundation to begin with.
Often the purpose is to consume the value and return a new kind of value. Maybe `validate_square` takes in `Shape` and returns `Option<Shape>`
If valid or invalid, we have consumed the shape so it cant be reused by accident and can't be used without handling the optional branches.
I'm new so maybe I'm off on this.
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.
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.