I see a lot of confusion when I read comments on here regarding Copy/Clone and memcopy with respect to move.
"Move" in Rust refers to the logical movement of ownership of a memory location from one variable to another. It does not (directly) refer to whether the contents of that memory location are moving or being copied or anything else.
The "Copy" trait simply says that the given type can be copied, instead of being moved, so ownership transfer isn't necessary when assigning one variable to another or calling a function, etc. All fundamental/primitive types are "Copy" (like i32) because the values can be copied without breaking semantics AND they are trivially mem-copyable (meaning they don't require any deallocation of memory or other resources through drop) with little to no overhead. Types that are not ""Copy" cannot have ownership ''''Moved" by simply making another "Copy" of the thing.
Think of it like this: If I have something that can be trivially copied, and you want one, I can make a copy and give it to you (Digital Songs are a good example); however, if I have a '67 Mustang, and you want one, I can't just make a copy (easily) and give it to you. Instead, I need to give it to you. So, if something is "Copy", I can give it to you and keep it at the same time. We both end up with one and they are both of equal value and utility. Something that isn't copy, I must give (or sell) it to you for you to have it. We both can't possess (Own) it at the same time.
Now, I could make you a copy of my '67 Mustang (clone it), but, that won't be cheap. If I clone it, you'll then have one and I'll still have one, but, there was a significant cost involved (unlike the copy of "Mmm-Bop" I gave you where I still have my copy of "Mmm-Bop" and can still listen to it whenever I want and giving you a copy cost me almost nothing).
If I own something, and you want one there are the following options:
- If the thing is easily/cheaply copied I can make a copy and give it to you. Now, you own one and I own one, but, they are not the same one, and it cost me or you almost nothing for me to give you a copy. These are "Copy" types.
- If the thing can be duplicated through some expensive process, I can clone mine and give you the clone. Now, also, we both have one, and the one you have is not the same as the one I have; however, either your or I had to pay a significant cost to have the clone created.
- I can transfer ownership to you. Now, you have it and control it, but, I don't have it anymore and can't use it. For example, I could just give you my '67 Mustang. I could also give you my Copy of "Mmm-Bop" without making a new Copy and I'll never
be able have to listen to it again.
- I can loan it to you, with the understanding you'll return it when you're done with it AND that no-one else can use it (including me) while you're using it. This is an exclusive loan, also known, by somewhat of a misnomer, as a "mutable borrow" (
&mut thing : Thing
).
- I can allow you to use it, but, I may also continue to use it, and I may also let other people use it (and you may let other people use it as well), with the understanding that the thing cannot (by design) be misused by any of us to adversely effect the others' usage of the same. This is a "immutable borrow" (
& thing : Thing
) - also, somewhat of a misnomer.
When I say that "mutable borrow" and "immutable borrow" are misnomers, I say that because the important thing is "Exclusive Access" without ownership vs "Shared Access" without ownership. For example, I could lease you my '67 Mustang, and you would have exclusive access. Whether or not you could modify it, is a separate issue, that would be dictated by the contract (Visibility, Traits, and Functions that operate on it). For example, if I leased you the car, it would be with the understanding that you could use all of the car's functions as designed. If the car had a mechanism to change the color, you'd be free to change the color. Not because you have exclusive access, but, because the contract of the functionality the car provides says you can change the color as long as you have exclusive access to it. If the contract that comes with the car says you can only change the color if you are the owner, then, you can't change the color just because you have exclusive access (mut). By the same token, if the contract that comes with the car says that you can change the color of the car even if you only have shared access to the car, then you can change the color of the car with a shared borrow. The contract only allows what is safe to do. A contract that allowed you to change the color, even though you didn't have exclusive access, would be a bad/invalid (Undefined Behavior) contract if changing the color while someone else was using it would POTENTIALLY adversely effect that person. The contract could allow you to change the color with shared access as long as you verify before doing so that you changing the color right then wouldn't adversely impact anyone else - that would be an "unsafe" contract (but, not necessarily "undefined behavior" as long as you are sure to check and make sure it is OK to change the color right then. That "unsafe" contract is frowned upon in Rust because now the other users of the car can't be guaranteed that your actions will not cause them problems.
Now, getting back to the Copy and Clone traits: They are only meaningful when talking about ownership, not borrowing (shared or exclusive). If I loan you the thing (whether shared or exclusive), the thing is neither copied nor cloned. What Copy and Clone indicates, is what kind of transfer of ownership is feasible/efficient.
- Copy - says, if you want what I have, and I can easily/trivially give you a copy of what I have and now we'll both own one. If this is the case, the compiler will automatically make these copies instead of transferring ownership when what I have is what you need.
- Clone -says, if you want what I have, I can give you a copy of what I have, but, there will be a significant cost involved. We'll still both end up with our own independent copy of the original, but, it would've had a cost. The compiler will not automatically do this to transfer ownership. If you want what I have, and I want to keep the one I have, and the thing can be cloned, I have to decide to clone it and give you the clone. I paid the price knowing the cost and understanding that I'd rather pay the price than give up ownership of my own copy.
- Without Clone or Copy, the only way you can have (own) what I have (own) is for me to surrender it to you and give up my ownership and allow you to take ownership. This is what the compiler will do automatically on assignment or passing a value to a function when the thing is not trivially copyable (Copy).
So, you want a new Trait that isn't Copy, but, isn't Clone. For what purpose? How does it fit into all of the above. Copy and Clone is not about whether or not something can or can't be mem-copied, it is about whether or not a mem-copy should happen automatically or not when you want ownership of something I own. With your new trait, what should happen implicitly differently (if anything).
I personally think that you don't understand what "Move", "Borrow'" means if you are asking for this, and I hope the above explanation helps to clarify things.