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?
-
1users.rust-lang.org/t/why-no-lifetime-elision-for-structs/8216Robert Harvey– Robert Harvey06/27/2018 14:27:43Commented Jun 27, 2018 at 14:27