How to transfer ownership of a Vec

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.