I want to have a list of items in a struct to reference other items from another list in the same struct. I can do this with Rc&RefCell but then code turns into a mess and I feel that employing Rcs for such a basic thing is an overkill. So it looks like
struct A <'a> {
items_b:Vec<B>,
items_c:Vec<C<'a>>
}
...
struct C<'c> {
item_b: &'c B
}
and the method to add an item
fn add_c(&mut self, item_b_index:usize) {
self.items_c.push(C::new(self.items_b.get(item_b_index).unwrap()))
}
but it either fails with
cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
or, if I add lifetime to mut self, with
cannot borrow
a
as mutable more than once at a time
so it becomes unusable. And I don't understand why lifetime matters here at all...
Full example in playground.
Any help is highly appreciated!