4 ms·
Minor clarification: afaik it's impossible to implement Copy such that it would allocate. Unlike Clone, the Copy trait is only a marker trait which doesn't have
by dthul 6y ago
Minor clarification: afaik it's impossible to implement Copy such that it would allocate. Unlike Clone, the Copy trait is only a marker trait which doesn't have methods, so you cannot customize its behavior. It will always be a bitwise stack copy.
- bestouff 6y agoThis is not minor. In Rust "x = y" specifically only copies or moves bytes, no magic involved, no code is run except something like memcpy. - If the type isn't marked Copy, it's a bytes move (which will very probably be optimized out). - If the type is marked Copy (which is possible only if all its subtypes are also marked Copy, which allocating types are not) then it's a bytes-for-bytes copy. As soon as you need something more involved, then you have to implement (or derive for simple types) Clone.
- steveklabnik 6y agoThe real key here is that something that allocates will want to Drop said memory, and you can’t implement both Drop and Copy.