Do shallow copy and Bitwise copy in rust mean the same thing? How should I understand them.
If by "shallow copy" you mean
let y = x;
- A move of
x
, ifx
's type does not implementCopy
- You can't use
x
afterwards
- You can't use
- A copy of
x
, ifx
's type implementsCopy
- You can still use
x
afterwards
- You can still use
And both are bitwise copies (that are almost surely optimized away if you don't use x
afterwards).
It’s important to note that in these two examples, the only difference is whether you are allowed to access
x
after the assignment. Under the hood, both a copy and a move can result in bits being copied in memory, although this is sometimes optimized away.
Copies happen implicitly, for example as part of an assignment
y = x
. The behavior ofCopy
is not overloadable; it is always a simple bit-wise copy.
2 Likes