I'd appreciate some help in understanding the source of the compiler error regarding mismatched lifetimes. Here's the playground link.
I have the following structs:
enum Data<'a> {
Simple,
Complex(&'a Data<'a>)
}
struct Foo<'a> {
table: HashMap<String, Box<Data<'a>>>,
values: Vec<Bar<'a>>
}
struct Bar<'a> {
data: &'a Data<'a>
}
Basically, the table
owns the data (via Box) and the values
vec contains references to some of them. Except, the compiler complains that it can't infer lifetimes properly when I try to insert data into values
:
impl<'a> Foo<'a> {
fn foo(&mut self) {
let v = &self.table[&"xyz".to_string()];
self.values.push(Bar{data: v});
}
}
As far as I can see, all the objects live under 'a
, so what is the source of the error? Do I need a lifetime parameter for the &mut self
too?