How do lifetimes work in struct definitions

Lifetimes in struct definitions ensure that references stored within the struct remain valid for the required duration.

Lifetimes in struct definitions tell the compiler how long references stored in the struct must remain valid. You add a lifetime parameter to the struct and every reference field to ensure the data outlives the struct itself.

struct ImportantExcerpt<'a> {
    part: &'a str,
}

In this example, the struct ImportantExcerpt holds a reference part that must live for at least the duration 'a.