9.8 Structs and Ownership

9.8.1 Owning Data

A struct typically owns its fields. When the struct goes out of scope, Rust drops those fields in a well-defined order:

struct DataHolder {
    data: String,
}

fn main() {
    let holder = DataHolder {
        data: String::from("Some data"),
    };
    // `holder` owns the string
}

9.8.2 Borrowing Data

If a struct needs to hold references, you must specify lifetimes:

#![allow(unused)]
fn main() {
struct RefHolder<'a> {
    data: &'a str,
}
}

This ensures the referenced data outlives the struct.