rust - 如何解决“创建一个仍在使用时释放的临时变量”?
问题描述
我正在尝试使用循环Debug
为我的自定义实现。List<T>
while
use std::cell::RefCell;
use std::rc::Rc;
type Link<T> = Option<Rc<RefCell<Node<T>>>>;
struct Node<T> {
val: T,
next: Link<T>,
}
struct List<T> {
head: Link<T>,
tail: Link<T>,
len: usize,
}
use std::fmt;
impl<T: fmt::Debug> fmt::Debug for List<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut temp = &self.head;
while let Some(r) = temp {
write!(f, "{:?} -> ", r.borrow().val);
temp = &r.borrow().next; // ERROR
}
write!(f, "[]")
}
}
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:21:21
|
21 | temp = &r.borrow().next; // ERROR
| ^^^^^^^^^^ -
| | |
| | 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 `std::cell::Ref<'_, 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
我知道我正在参考一个临时值r
,这很糟糕。有没有围绕这个问题的优雅和惯用的解决方案?
Rust 代码后跟 Rustaceans 是否有任何借用模式?我知道一种这样的模式是std::mem::replace
拥有一个价值,它只有在我们拥有时才有效&mut T
,有人称之为印第安纳琼斯模式
解决方案
我在 Rust 用户论坛上问了同样的问题,得到了alice的回答:
impl<T: fmt::Debug> fmt::Debug for List<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut temp = self.head.clone();
while let Some(r) = temp {
write!(f, "{:?} -> ", r.borrow().val);
temp = r.borrow().next.clone();
}
write!(f, "[]")
}
}
这样做的原因是
borrow()
返回一个带有析构函数的守卫,并且您只能RefCell
在该守卫仍处于活动状态时访问引用。因此,如果不克隆 anRc
,您将需要保留每次迭代的所有守卫,但循环会在前进到下一次迭代时破坏前一个守卫。Rc
克隆让您无需通过防护即可访问下一个节点,从而回避了这个问题。
推荐阅读
- django - Django All_auth/rest_auth 使用 HTTP GET 请求验证电子邮件地址
- google-cloud-firestore - 在集合之间移动文档的云功能
- java - 在特定点显示对象,但仅显示几秒钟
- c# - 有没有办法让多个正在运行的线程从流中提取数据,同时开始将该数据写入单独的文件?
- java - 在自定义反序列化器中将 JsonNode 转换为 POJO
- c# - 在没有登录的情况下输入超链接并在登录后重定向到带有日期时间(小时和分钟)的 ReturnUrl
- c# - 如何在 C# 上通过 winapi 获取默认播放设备 ID?
- python - 尝试使用脚本在 Atom 中导入熊猫时出现 ImportError
- objective-c - iOS 13 获取应用程序状态栏崩溃
- assembly - 堆栈红色区域的实际大小是多少?