- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个类型定义类似于
的树#[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_mut
而 borrow
就位,这会引起 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 => ¤t_node.left,
Ordering::Equal => return false,
Ordering::Greater => ¤t_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/
Rust 文档涵盖 Rc>相当广泛,但不涉及 RefCell> ,我现在遇到了。 这些是否有效地给出了相同的结果?它们之间有重要区别吗? 最佳答案 Do these effectively give
Rust 文档涵盖 Rc>相当广泛,但不涉及 RefCell> ,我现在遇到了。 这些是否有效地给出了相同的结果?它们之间有重要区别吗? 最佳答案 Do these effectively give
如果我做对了,就不可能通过 std::rc::Rc 创建一个可变的借用。在 Rust 中,你必须使用 Cell或 RefCell .但无论如何我无法理解如何使用它们。例如考虑这个简单的 example
我正在尝试转换 Rc>至 Rc> ( Data 实现 Interface )但在通用方法中是不可能的: use std::cell::RefCell; use std::rc::Rc; trait I
我最初问过这个问题 here , 但它被标记为重复,尽管在我看来它只复制了一部分,所以我创建了一个更具体的: 考虑以下代码: use std::rc::Rc; trait MyTrait {
我正在实现VirtualTapInterface的特征。此特征的receive函数应创建一个TxToken结构,其中lower属性必须是包含当前Rc>的VirtualTapInterface,即sel
我不知道如何在以下代码片段中实现 children_iter: use std::{cell::RefCell, rc::Rc};
我有一段代码需要对列表进行操作。此列表包含来自其他来源并需要处理并最终删除的项目。该列表还传递给多个函数,这些函数决定是添加还是删除项目。我创建了一个示例代码来反射(reflect)我的问题: use
是否可以创建类型为 RefCell 的东西?在使用rust ?我尝试了以下方法: fn test2(x : T) -> RefCell{ return RefCell::new(x) as R
背景 我正在编写一个库,一些相关的代码如下所示: pub struct MyStruct1; pub struct MyStruct2; pub enum MyEnum { Variant1(
这个问题在这里已经有了答案: How do I return a reference to something inside a RefCell without breaking encapsulat
我正在使用Rc>(Playground)实现二进制搜索树 use std::cell::RefCell; use std::rc::Rc; type RcRefBaseNode = Rc>>; typ
我一直在思考为什么 Rust 中的内部可变性在大多数情况下需要运行时检查(例如 RefCell )。看起来我找到了一个没有运行时成本的安全替代方案。我调用类型 SafeCell (主要是因为它是围绕
为什么我们不能将 RefCell 用于 Rust 中的递归数据结构? 无效: enum List { Cons(i32, RefCell), Nil, } 有效: enum List
我有一个包含两个 Vec 的结构,它们被包裹在 RefCell 中。我想在该结构上有一个方法,它结合了两个向量并将它们作为新的 RefCell 或 RefMut 返回: use std::cell::
我正在尝试创建一个方法,该方法返回一个遍历 HashMap 值的迭代器装在 RefCell 内,但我在 Ref 处遇到错误由 RefCell::borrow 返回没有足够长的时间从方法返回迭代器。这是
我试图将 RefCell 传递给闭包中的函数,然后从闭包内部修改同一个变量。这是我的代码: let path: Rc>>> = Rc::new(RefCell::new(None)); ... //v
我有一个包含两个 C 指针和一个 Rust 指针的结构 HashMap . struct MyStruct { p1: *mut ..., p2: *mut ..., hm:
我有一个链表类型的结构,使用 Option、Rc 和 RefCell。 我想为它实现 fmt::Debug,但遇到了可爱的“无法移出借用的内容”错误。 use std::fmt; use std::r
我有一个具有内部可变性的结构。 use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec, } s
我是一名优秀的程序员,十分优秀!