- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Rust会在编译时提供借入支票。但是,如果使用Rc
和RefCell
,则将在运行时推迟检查,并且在程序违反规则时会引发 panic 。像这样:
use std::rc::Rc;
use std::cell::RefCell;
fn func1(reference: Rc<RefCell<String>>){
let mut a = reference.borrow_mut();
*a = String::from("func1");
func2(reference.clone());
}
fn func2(reference: Rc<RefCell<String>>){
let mut a = reference.borrow_mut();
*a = String::from("func2");
func3(reference.clone());
}
fn func3(reference: Rc<RefCell<String>>){
let mut a = reference.borrow_mut();
*a = String::from("func3");
}
fn main() {
let a = Rc::new(RefCell::new(String::from("hello")));
func1(a.clone());
}
这段代码仍将错误(可能不是错误)留给运行时并感到 panic 。所以我应该避免尽可能多地使用
Rc
和
RefCell
吗?并将此代码算作安全代码吗?
最佳答案
由于Rc
和RefCell
允许您编译在运行时可能出现紧急情况的代码,因此请不要轻易使用它们。您可以使用try_borrow_mut
而不是borrow_mut
来避免出现 panic 并自行处理结果。
话虽如此,即使您阻止所有 panic ,Rc
和RefCell
在运行时也要付出代价,因为它们保留了一个引用计数器。在许多情况下,可以通过以更使用rust 的方式重写代码来避免它们。
fn func1(mut string: String) -> String {
string = "func1".into();
func2(string)
}
fn func2(mut string: String) -> String {
string = "func2".into();
func3(string)
}
fn func3(string: String) -> String {
"func3".into()
}
fn main() {
let a = func1("hello".into());
}
更简单,更安全。 Rust会为您进行优化。
borrow_mut
视为不安全的代码,因为即使使用
#![forbid(unsafe_code)]
指令,该代码也可以编译
关于rust - 如果可能,我是否应该避免在Rust中使用Rc和RefCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65338749/
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
我是一名优秀的程序员,十分优秀!