我是否应将RefCell <Rc <T >>或Rc <RefCell <T >>用于具有可变异父代的树?

时间:2019-08-14 03:53:22

标签: tree rust reference-counting interior-mutability

我想使用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引入可变性。

如果我想要一棵可以变异的亲本树,推荐哪一种?

0 个答案:

没有答案