3 ms·
The compiler can just say "this value is dead, you can't use it". That's what rust does - rust isn't literally freeing/clearing memory when a struct is moved.
by olliej 7y ago
The compiler can just say "this value is dead, you can't use it".
That's what rust does - rust isn't literally freeing/clearing memory when a struct is moved.
- gumby 7y agoRust can do this for an arbitrary element of a dynamically allocated array?
- olliej 7y agoRust can only do that in unsafe blocks. The C++ version would need the existing (unchangeable) move semantics.
- roca 7y agoNo. You can't move an element out of an array in Rust. What Ollie said is true for local variables, and collections generally support some kind of "move element(s) out of the collection" API (e.g. Vec::pop(), T::into_iter()).
- gumby 7y agoSo rust can do it in a subset of cases, such that it can make a stronger assertion; C++ can do it in the general case, and is therefore unable to make the stronger assertion. Neither is correct vs the other, rather they reflect different choice points in the language possibility space.
- roca 7y agoYou can do a C++ style "swap object with a placeholder" move in Rust, easily, with std::mem::replace(). There's no advantage for C++ here.