I have a root Node
, and I store other Node
s as children:
struct Node {
header: Header,
children: Vec<Node>,
metadata: Vec<usize>,
parent: &Node,
}
I want to have a way to go upwards to parent Node
s, so I wanted to have a parent that is a reference to another Node
. Asking around, this seems to be like a bad idea. I know I have to use lifetime declarations if I want to achieve this, but I'm not sure if the reference idea is the correct approach.
Making them 'static
would be OK in this current small program, which is a way to solve Day 8b of Advent of Code 2018.
I received this piece of information:
just remember that you can't have 2 mutable references to the same object at the same time. Those kinds of structures are easy to get wrong when designed with a bunch of back pointers. Consider a memory arena instead, where your relations are defined in terms of indices inside that arena
I have no idea what a memory arena is or how I would implement, nor if it's actually the best way to achieve this.