我想使用Rust实现其节点具有parent
字段的树。根据官方指南adding-a-reference-from-a-child-to-its-parent,他们使用该结构
struct Node {
value: i32,
parent: RefCell<Weak<Node>>,
children: RefCell<Vec<Rc<Node>>>,
}
要可变地使用RefCell<Rc<T>>
的实例,我可以叫.borrow_mut()
。
在doc of std::cell
中,let shared_map: Rc<RefCell<_>> = Rc::new(RefCell::new(HashMap::new()));
用于向Rc
引入可变性。
如果我想要一棵可以变异的亲本树,推荐哪一种?