I'm trying to create a struct that contains a list of other structs, (could be fixed size if that helped)
struct Container<'m> {
contained: Vec<&'m Contained>,
}
struct Contained {
id: u64,
}
impl Container<'_> {
pub fn new<'m>() -> Container<'m> {
Container {
contained: Vec::new()
}
}
pub fn new_contained<'m>(&mut self, id: u64) {
let c = Contained {
id
};
self.contained.push(&c);
^^ lifetime of Contained ends here
}
}
I can see that life time of Contained ends but I cant see how to create a struct with a given lifetime.
Do I have any alternatives I tried using a fixed size array instead of a Vec this way the Container owns the contents of the Array, but I cant create a zero sized array to start with.
Vec
contain references toContained
? Simplycontained: Vec<Contained>
should suffice for most use cases. – Sebastian RedlContained
objects? They have to live somewhere else that outlives theContainer
. – Acorn