let _ = x;
This is a no-op and the value remains owned by the variable x, and is not thrown away. let _ = x;
This is a no-op and the value remains owned by the variable x, and is not thrown away.It has its limits though - in your example I'd say you're throwing away the result of evaluating "x", just as if you did:
x;1:
let x = String::new();
let _ = x;
println!("{}", x);
2: let x = String::new();
x;
println!("{}", x); // ERROR: Use of moved value x.
And this is why I said that let _ = x; is a no-op. :) pub fn drop<T>(_x: T) { }
https://doc.rust-lang.org/std/mem/fn.drop.html drop(x);
is a lot clearer than x;The main difference to C's free is that you simply cannot double-free a value (without using unsafe). That's because a value is only dropped when it falls out of scope -- and since it values only have one owner (and references must not outlive values) there cannot be any dangling references after it's dropped.
The documentation for std::mem::drop is explaining that dropping a value is a property of the language and scoping rules -- thus there is nothing for the function to do other than take a value and let it go out of scope.
let _ = timeConsumingFunction();
Still needs to spend time performing timeConsumingFunction(). It might not move, but it still did the work and produced a result, you are just not using it (and hence not need to move it).If you you had lazy evaluation I would agree with your argument for the no-op terminology.
let _ = <expr>;
and: let _x = <expr>;
The first one drops ownership over the variable right after `let`, and the second drops in the end of the scope where the `let` is contained. It was mentioned in an open Rust github issue, but I can't seem to find it.Thanks for adding this important detail.