How Does Ownership Work with Vectors and Collections?

Vectors own their data, so assigning them moves ownership, while references allow borrowing without transfer.

Ownership in Rust means the Vec or collection owns its data, and moving the collection transfers that ownership to a new variable, invalidating the old one. To share data without moving it, you must borrow references instead of assigning the collection directly.

let v1 = vec![1, 2, 3];
let v2 = v1; // v1 is moved; v2 now owns the data
// println!("{:?}", v1); // Error: use of moved value
let v3 = &v2; // v3 borrows v2; v2 still owns the data
println!("{:?}", v3); // OK