Should I use RefCell<Rc<T>> or Rc<RefCell<T>> for a tree with parents which can be mutated?

82 Views Asked by At

I want to implement a tree whose node has parent field using Rust. From the official guide adding-a-reference-from-a-child-to-its-parent, they use the structure

struct Node {
    value: i32,
    parent: RefCell<Weak<Node>>,
    children: RefCell<Vec<Rc<Node>>>,
}

To use an instance of RefCell<Rc<T>> mutably, I can call .borrow_mut().

In the doc of std::cell, let shared_map: Rc<RefCell<_>> = Rc::new(RefCell::new(HashMap::new())); is used to introduce mutability to Rc.

If I want a tree with parents which can be mutated, which one is recommended?

0

There are 0 best solutions below