The book outlines the ways variables and data interact: Move, Clone, and Copy.
I’m particularly interested in the cognitive load their differences places on developers. One has to really know how the type will behave when they are reading code to know what is really going on. For example:
let x = a; let y = x;
Given this example, you need to know if
a has the
Copy trait to know how it will behave and whether or not you can continue using it.
Move was the default behavior, you would know that
x is invalidated upon the
let y = x assignment. This would be consistent for all variables. Likewise, you could always explicitly copy it:
let x = 5; let y = x.clone();
Then, you would be able to keep using x.
When you see a call to
clone, you know that some arbitrary code is being executed and that code may be expensive. It’s a visual indicator that something different is going on.
It seems like this would lose some value, since scalar values would be using
clone as well.
When considering the performance here, you would need to take on the burden that is removed above. It’s during these performance considerations that you would need to think about the type and what
clone really means for that type. Nevertheless, it seems to make the most sense to be giving more thought to the internal workings of a copy when considering performance than when tracking the scope of variables.