How does ownership work with structs

Struct ownership transfers all field ownership when the struct is moved, invalidating the original variable.

Ownership of a struct is determined by the ownership of its fields: if any field owns data (like a String), the struct owns that data, and moving the struct moves all its fields. When you assign a struct to a new variable, the entire struct is moved, invalidating the original variable unless the fields implement Copy.

struct User {
    username: String,
    active: bool,
}

fn main() {
    let user1 = User {
        username: String::from("alice"),
        active: true,
    };
    let user2 = user1; // user1 is now invalid
    // println!("{}", user1.username); // Error: value borrowed here after move
    println!("{}", user2.username); // Works
}