How Does Ownership Work with Structs in Rust?

Struct ownership follows Rust's move semantics, transferring control of all fields to the new owner upon assignment.

Ownership of a struct is determined by the ownership of its fields: assigning a struct moves ownership of all its fields to the new variable unless the fields implement Copy.

struct Point { x: i32, y: i32 }

let p1 = Point { x: 10, y: 20 };
let p2 = p1; // p1 is moved; p2 owns the data
// println!("{}", p1.x); // Error: borrow of moved value

If a struct contains a non-Copy type like String, the entire struct is moved on assignment, making the original variable unusable.