I want to create a struct which referecnces to it self like following:
struct Item<'a> {
id: String,
children: Vec<&'a mut Item<'a>>,
parents: Vec<&'a mut Item<'a>>
}
At the moment I use a hashmap for all Items and save the id: String
in the children/parents vectors.
I am curious how I can improve the performance and usablility by avoiding the hashmap lookup for all my related ids with references. It is working with unmutable borrowed references, but then I loose the advantage to add or remove children/parents from related Items.
Because one Item can be linked to multiple other Items, I would need multiple mutable references which is prohibited. Is it possible with Rc
and RefCell
?
How do I design a datatype like this correctly or is it nonsense?