Copy
trait, the value is not considered to have moved even if it is reassigned to a new memory location. Instead, the value is copied, and both the old and new locations remain accessible.⚠️ To see why, consider what would happen if a type like
Box
were Copy. If we executed box2 = box1, then box1 and box2 would both believe that they owned the heap memory allocated for the box, and they would both attempt to free it when they went out of scope. Freeing the memory twice could have catastrophic consequences.
Types usually recursively drop values they contain, so dropping a variable of a complex type may result in many values being dropped.
A variable that holds a reference to another value does not own that other value, so the value isn’t dropped when the variable drops.
let x1 = 42;
let y1 = Box::new(84);
{ // starts a new scope
let z = (x1, y1);
// z goes out of scope, and is dropped;
// it in turn drops the values from x1 and y1
}
// x1's value is Copy, so it was not moved into z
let x2 = x1;
// ❌ y1's value is not Copy, so it was moved into z
//let y2 = y1;