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,


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?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.