4 ms·
Huh, TIL! Had no idea `x;` actually moved `x`.
by fasterthanlime 7y ago
Huh, TIL! Had no idea `x;` actually moved `x`.
- AnimalMuppet 7y agoBut where is it moved to? Who owns it now? Or is it just lost?
- kzrdude 7y agoyes, it is dropped
- runarberg 7y agoAhh, I remember a while back reading a blog where the author talked about `std::mem::drop` being their favorite standard library function because they used this effect. It is literately defined as: pub fn drop<T>(_x: T) { } https://doc.rust-lang.org/std/mem/fn.drop.html https://doc.rust-lang.org/std/mem/fn.drop.html
- kzrdude 7y agoRight, so drop is useless :) We can drop things fine without it. Since of course it's scope/ownership based and not something that's explicitly called.
- AnimalMuppet 7y agoWell, drop(x); is a lot clearer than x;
- e_proxus 7y agoSo is drop like free, or is it something else?
- cyphar 7y agoYes, it's effectively like free -- though C++'s "auto" is probably a closer idea. 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.
- pixel_fcker 7y agoIt’s more like delete in c++ because you can implement the Drop trait for a type in order to run code when it is dropped in order to clean up resources etc.
- kzrdude 7y agoDrop is just what runs when a value goes out of scope. Like destructors in C++.
- kzrdude 7y agoWhen you put it like that, I see that it's a rather minor detail.