7

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?

asked Jun 27, 2018 at 14:26
1

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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