5 ms·
> Move by default. If you came from c++, I think this makes a lot of sense. > Immutable by default. In C++, these two fight each other. You can't (for the mos
by dataflow 2y ago
> Move by default. If you came from c++, I think this makes a lot of sense.
> Immutable by default.
In C++, these two fight each other. You can't (for the most part) move from something that's immutable.
How does Rust handle this? I assume it drops immutability upon the move, and that doesn't affect optimizations because the variable is unused thereafter?
- lightingthedark 2y agoRust moves aren't quite the same as C++ moves, you can think of them more like a memcpy where the destructor (if there is one) doesn't get run on the original location. This means you can move an immutable object, the object itself doesn't have to do anything to be moved.
- NobodyNada 2y agoIn Rust, when you move out of a variable, that variable is now effectively out-of-scope; trying to access it will result in a compile error. Mutability in Rust is an attribute of a location; not a value, so you can indeed move a value from an immutable location into a mutable one, thus "dropping immutability". (But you can only move out of a location that you have exclusive access to -- you can't move out of an & reference, for example -- so the effect is purely local.)
- dataflow 2y agoYeah that sounds about like what I expected. Thanks!
- remram 2y agoYou can't refer to any old location so there is no observable mutation. For example you can't move if a reference exists.
- baq 2y agoNot 100% sure but sounds like you want Pin<>?