Transfer ownership of a Vec by assigning it to a new variable, which moves the data pointer without copying the underlying heap memory.
You transfer ownership of a Vec by assigning it to a new variable, which moves the pointer to the heap data rather than copying the data itself.
let v1: Vec<i32> = vec![1, 2, 3];
let v2 = v1; // Ownership of v1 is moved to v2
// v1 is no longer valid here
println!("{v2}"); // Prints: [1, 2, 3]
If you need both variables to own the data, you must clone the vector using the .clone() method, which allocates new memory and copies the contents.
Transferring ownership of a Vec is like handing over a key to a storage unit; the original owner loses access, and the new owner takes full control. This prevents accidental duplication of large data sets and ensures memory is cleaned up exactly once. If you need two people to have the key, you must make a copy of the contents instead of just passing the key.