I have some code...
struct Foo<'a> {
items: Vec<&'a u32>,
}
fn compiles() {
let n = 123;
let mut foo = Foo { items: vec![] };
foo.items.push(&n);
}
fn does_not_compile() {
let mut foo = Foo { items: vec![] };
let n = 123;
foo.items.push(&n);
}
fn main() {}
I know why does_not_compile
doesn't compile: the lifetime of n
is shorter than the lifetime of foo
, and foo.items
can only contain values that live at least as long as foo
does.
What I do not understand is how to declare my intent that members of foo.items
should live for a shorter period of time than foo
itself. It seems similar to the lifetime issues involved with Iterators, but I do not see how to translate those ideas to this situation.
How can I declare the type Foo
so that does_not_compile
becomes correct?