3 ms·
> things don't move if you don't tell them to This is something I've struggled a bit with when playing with Rust. I've had a hard time understanding whether I'
by jay-anderson 9y ago
> things don't move if you don't tell them to
This is something I've struggled a bit with when playing with Rust. I've had a hard time understanding whether I'm telling the compiler to move or copy. In other words I need to understand better what I'm telling it to do (I probably just need to buckle down and study the new book in this area better). It wasn't immediately obvious to me when I last played with it.
- steveklabnik 9y agoIt depends on the type, not what you say. Copy types copy, other types move.
- masklinn 9y ago> I've had a hard time understanding whether I'm telling the compiler to move or copy. In other words I need to understand better what I'm telling it to do You're not telling it anything, in Rust copy v move is a type-level concern: either the type is "move" (the default) or it's "copy". There is no difference at the machine-level either (it's semantically doing a memcpy either way, Rust has no move ctor v copy ctor and its move semantics are somewhat different than C++), the difference is whether the compiler will let you use the source object afterwards. Incidentally, I believe you're not really "telling the compiler to move or copy" in C++ either, you're only suggesting that it can move and whether it actually does so depends on the type's ctors and IIRC the receiver's overloads: std::move on a type which can't be moved or with a target which doesn't care does not do anything.