gpt4 book ai didi

rust - 了解 rust `Rc>`

转载 作者:行者123 更新时间:2023-12-05 03:29:44 25 4
gpt4 key购买 nike

我正在尝试创建一个类型定义类似于

的树
#[derive(Debug, Clone)]
pub(crate) struct TreeBox<T> {
root: Option<Box<NodeBox<T>>>,
}

#[derive(Debug, Clone)]
struct NodeBox<T> {
value: T,
left: Option<Box<NodeBox<T>>>,
right: Option<Box<NodeBox<T>>>,
}

具有插入功能

impl<T: Ord> TreeBox<T> {
fn new() -> Self {
Self { root: None }
}

pub fn insert(&mut self, value: T) -> bool {
let mut node = &mut self.root;

while let Option::Some(current_node) = node {
match current_node.value.cmp(&value) {
Ordering::Less => node = &mut current_node.right,
Ordering::Equal => return false,
Ordering::Greater => node = &mut current_node.left,
}
}

*node = Option::Some(Box::new(NodeBox {
value,
left: Option::None,
right: Option::None,
}));

return true;
}
}

这非常有效,我对实现非常满意。但是我想将每个节点的引用存储到它的父节点。经过一些研究,我发现了这个 article描述使用 RefCell 的实现和 Weak结构。

有了这些知识,我的计划是更新上面的示例。我的想法是我可以用 Box<...> 代替与 Rc<RefCell<..>> .我的想法是,这些类型非常相似,因为它们都存储对某些数据结构的引用,唯一的区别是可以有多个 Rc<RefCell<..>>。指向那个数据结构。我将我的实现更改为

#[derive(Debug, Clone)]
pub(crate) struct Tree<T> {
root: Option<Rc<RefCell<Node<T>>>>,
}

#[derive(Debug, Clone)]
struct Node<T> {
value: T,
left: Option<Rc<RefCell<Node<T>>>>,
right: Option<Rc<RefCell<Node<T>>>>,
}

impl<T: Ord> Tree<T> {
fn new() -> Self {
Self { root: None }
}

pub fn insert(&mut self, value: T) -> bool {
let mut node = &mut self.root;

while let Option::Some(current_node) = node {
let cmp = current_node.borrow().value.cmp(&value);
match cmp {
Ordering::Less => node = &mut current_node.borrow_mut().right,
Ordering::Equal => return false,
Ordering::Greater => node = &mut current_node.borrow_mut().left,
};
}

*node = Option::Some(Rc::new(RefCell::new(Node {
value,
left: Option::None,
right: Option::None,
})));

return true;
}
}

但是这个更新的例子没有运行,说

error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:28:47
|
28 | Ordering::Less => node = &mut current_node.borrow_mut().right,
| ^^^^^^^^^^^^^^^^^^^^^^^^^ -
| | |
| | temporary value is freed at the end of this statement
| | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `RefMut<'_, Node<T>>`
| creates a temporary which is freed while still in use
| a temporary with access to the borrow is created here ...
|
= note: consider using a `let` binding to create a longer lived value

我的示例是错误的,还是我对使用rust 仍然不太了解 Rc<RefCell<_>>

带有上述示例的 Playground 链接:https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9929b2118e2430f03035da4c65a4cf2f

最佳答案

所以,你有一些问题。主要的是您正在尝试引用 Option包含一个生命周期很短的值,因为它与 borrow() 相关联在 RefCell . (你也在尝试 borrow_mutborrow 就位,这会引起 panic 。)谢天谢地,Rc使获得对 Rc 的引用的所有权变得便宜且容易(这就是重点),所以这个问题可以通过存储 Option 来解决。 , 不是 &Option , 并自由克隆包含的 Rc .我们使用 Option::as_ref转换 &Option<Rc<_>>进入 Option<&Rc<_>> ,然后变成 Option<Rc<_>>通过映射Rc::clone在上面。

pub fn insert(&mut self, value: T) -> bool {
let mut node = self.root.as_ref().map(Rc::clone);

while let Some(current_node) = node {
let current_node = current_node.borrow();
let cmp = current_node.value.cmp(&value);
let new_node = match cmp {
Ordering::Less => &current_node.left,
Ordering::Equal => return false,
Ordering::Greater => &current_node.right,
};
node = new_node.as_ref().map(Rc::clone);
}

let node = &mut node;
*node = Some(Rc::new(RefCell::new(Node {
value,
left: None,
right: None,
})));

true
}

关于rust - 了解 rust `Rc<RefCell<_>>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70957110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com