I'm having issues understanding how the problem of a self-referencing struct can be solved. Consider the following:
enum Color {
Brown,
}
struct Pot<'lab> {
color: Color,
labels: Vec<&'lab String>,
}
struct Shop<'a> {
labels: Vec<String>,
pots: Vec<Pot<'a>>,
}
impl<'a> Shop<'a> {
fn create_pot(&mut self, label_index: usize) {
let mut new_pot: Pot<'a> = Pot {
color: Color::Brown,
labels: Vec::new(),
};
let new_label = &self.labels[label_index];
new_pot.labels.push(new_label);
self.pots.push(new_pot);
}
}
Once I try to compile it, I get this error: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
on self.labels[label_index]
.
How can I solve this error, knowing that I need Pot
to have a reference to a label inside Shop
and I need the labels: Vec<String>
to stay inside Shop
?