2

I have a root Node, and I store other Nodes 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 Nodes, 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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
madprops
  • 3,909
  • 5
  • 34
  • 42
  • I suggest reading this section from the rust book: https://doc.rust-lang.org/book/ch15-01-box.html#enabling-recursive-types-with-boxes – ruohola Jul 29 '19 at 20:02
  • An arena is a data structure to contain many objects of the same type, you can then refer to them by index instead of using referenes. Take a look at the [slab crate](https://crates.io/crates/slab). – rodrigo Jul 29 '19 at 22:48

0 Answers0