It’s a bit ugly because the standard library functions are replete with underscores, and of course it isn’t empty. But I still think it’s quite surprising, as most people would think it actually does some sort of semantic “move”.
unique_ptr disposes of the object its holding when it goes out of scope unless passed to another unique_ptr or ownership is explicitly released. Presumably klipt would std::move the unique_ptr, hence the universal reference (which seems unnecessary, just pass it by value).
Am I missing something? Does it not work with std::move?
This is not correct; moves must leave the value in a valid state, because its destructor will still run. The correct version is actually the same as Rust's:
You're correct, sorry (I did have that at one point...). But the equivalent of Rust's pass-by-moving is to pass an r-value reference. Passing a const reference to drop is certainly an error and should be disallowed.
NLL does not change those semantics; drop still runs at the end of the scope. It counts as a "use" for the purposes of NLL and thus keeps values with destructors alive.
For some history, it was talked about changing this, but we couldn’t, due to back compatibility and it wasn’t clear it was actually a good idea. This was called “wary drop”.
Would be nice if types could annotate their Drop trait with #[early_drop] if the drop has no visible side effect, do rust could free the memory earlier.