6

In the Rust documentation, under the structs section, they give this example of how structs need lifetimes when they contain references:

struct Foo<'a> {
    x: &'a i32,
}

because

We need to ensure that any reference to a Foo cannot outlive the reference to an i32 it contains.

My question is: shouldn't that be implicit? When would we ever not care about an instance of our struct outliving a reference it contains?

Rust already has lifetime elision rules for functions and methods so that we don't have to explicitly declare lifetime constraints. Why don't they have similar elision rules for structs?

AxiomaticNexus
  • 776
  • 2
  • 7
  • 11

0 Answers0