News for hackers. Stuff that matters.
Re:Rust Ownership Rules by jakeec on Monday March 02, 2020 @06:53AM (#22463627)
What does 'consume' mean in this context?
Re:Rust Ownership Rules by Waterluvian on Monday March 02, 2020 @07:11AM (#22463711)
The function "consumes" the value. Its no longer valid in the function caller's scope.

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.

Re:Rust Ownership Rules by Cyph0n on Monday March 02, 2020 @08:40AM (#22464280)
In Rust, this is how “ownership” works. Once a variable is moved to a new scope, ownership moves with it, which means that it cannot be used in the current scope anymore.
Re:Rust Ownership Rules by dthul on Monday March 02, 2020 @07:11AM (#22463714)
"Consume" is comparable to a "move" in C++.
Re:Rust Ownership Rules by ChrisSD on Monday March 02, 2020 @07:20AM (#22463752)
If I consume your apple then you no longer have your apple and the apple will be gone once I finish with it. But because I eat apples whole, I can spit it out before I swallow and return it to you.

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.

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.

Re:Rust Ownership Rules by estebank on Monday March 02, 2020 @06:45PM (#22470361)
It is equivalent to "pass by value and that binding is no longer valid after the call".