- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景
考虑一个玩具问题,其中我有一个 Node
表示链接列表节点的结构,并且我想创建一个函数来构建一个值从 1 到 9 的列表。以下代码按预期工作:
struct Node {
val: i32,
next: Option<Box<Node>>,
}
fn build_list() -> Option<Box<Node>> {
let mut head = None;
let mut tail = &mut head;
for n in 1..10 {
*tail = Some(Box::new(Node {val: n, next: None}));
if let Some(ref mut x) = tail {
tail = &mut x.next;
};
}
head
}
但是如果我修改
build_list
中的匹配表达式函数如下,编译失败:
fn build_list() -> Option<Box<Node>> {
let mut head = None;
let mut tail = &mut head;
for n in 1..10 {
*tail = Some(Box::new(Node {val: n, next: None}));
if let Some(x) = tail.as_mut() {
tail = &mut x.next;
};
}
head
}
编译错误:
error[E0506]: cannot assign to `*tail` because it is borrowed
--> src/main.rs:72:9
|
72 | *tail = Some(Box::new(Node {val: n, next: None}));
| ^^^^^
| |
| assignment to borrowed `*tail` occurs here
| borrow later used here
73 | {
74 | if let Some(x) = tail.as_mut() {
| ---- borrow of `*tail` occurs here
error[E0499]: cannot borrow `*tail` as mutable more than once at a time
--> src/main.rs:74:30
|
74 | if let Some(x) = tail.as_mut() {
| ^^^^ mutable borrow starts here in previous iteration of loop
问题
if let Some(ref mut x) = tail
和
if let Some(x) = tail.as_mut()
?
*tail
.
tail
听起来很重要(在第一个工作示例中)。在循环的每次迭代中都在变化(这样它可以确保每次借用的
&mut x.next
都是不同的)。所以我做了一个实验,通过添加
if n % 2 == 0
以编译器无法判断是否是这种情况的方式更改代码。条件为
tail = &mut x.next;
任务。果然,它导致了与另一个类似的编译错误:
fn build_list() -> Option<Box<Node>> {
let mut head = None;
let mut tail = &mut head;
for n in 1..10 {
*tail = Some(Box::new(Node {val: n, next: None}));
if let Some(ref mut x) = tail {
if n % 2 == 0 {
tail = &mut x.next;
}
};
}
head
}
新错误:
error[E0506]: cannot assign to `*tail` because it is borrowed
--> src/main.rs:60:9
|
60 | *tail = Some(Box::new(Node {val: n, next: None}));
| ^^^^^
| |
| assignment to borrowed `*tail` occurs here
| borrow later used here
61 | if let Some(ref mut x) = tail {
| --------- borrow of `*tail` occurs here
error[E0503]: cannot use `*tail` because it was mutably borrowed
--> src/main.rs:61:16
|
61 | if let Some(ref mut x) = tail {
| ^^^^^---------^
| | |
| | borrow of `tail.0` occurs here
| use of borrowed `tail.0`
| borrow later used here
error[E0499]: cannot borrow `tail.0` as mutable more than once at a time
--> src/main.rs:61:21
|
61 | if let Some(ref mut x) = tail {
| ^^^^^^^^^ mutable borrow starts here in previous iteration of loop
最佳答案
你的代码的第二个版本失败的原因是因为 rust 的方法/函数总是借用整个对象,而不是它们的一部分。
在您的情况下,这意味着 tail.as_mut()
借tail
可变,并且只要 tail
,此借用将保持有效正在使用:
...
for n in 1..10 {
*tail = Some(Box::new(Node {val: n, next: None})); // Error in the second iteration,
// 'tail' was already borrowed
if let Some(x) = tail.as_mut() { // <-+ Borrow of 'tail' starts in the first iteration
tail = &mut x.next; // <-+ 'tail' now borrows itself
}; // |
} // <-+ Borrow of 'tail' ends here, after the last iteration
...
由于
x
借用
tail
,
&mut x.next
也是借用
tail
, 这意味着
tail = &mut x.next
是
tail
借自己。因此,
tail
的初始借用只要
tail
就不能超出范围是在范围内。
tail
在每次迭代中使用,因此借用只能在循环的最后一次迭代之后超出范围。
build_list
的第一个版本工作?简而言之:因为
tail
从来没有借过。
if let Some(ref mut x) = tail
是
tail
的解构进入其组件(在本例中为
Option::Some
和
x
)。这不借
tail
整体来说就是借
x
.当你然后
tail = &mut x.next
, 你现在也解构
x
到它的组件中(仅提取
next
),并使用
tail
借用它.在下一次迭代中,
tail
没有借用,因此可以很高兴地重新分配。
as_mut()
不得不借全
tail
,即使您只使用它的一部分。这是类型系统的限制,也是 getter/setter 方法比直接调用结构的成员更弱/更受限制的原因之一:getter/setter 将强制您借用整个结构,而直接访问成员只会借用它成员而不是其他人。
关于rust - 匹配 "if let Some(ref mut x) = option"和 "if let Some(x) = option.as_mut()"中的可变选项引用有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62869446/
从 Pin::as_mut 的来源- pin.rs: impl Pin { ... pub fn as_mut(&mut self) -> Pin { unsaf
这个问题在这里已经有了答案: What is the relation between auto-dereferencing and deref coercion? (1 个回答) 2年前关闭。 给定
Rust 生命周期再次让我感到困惑。我正在尝试返回对我拥有的盒装对象的可变引用。这是我的问题简化: pub trait Foo { fn foo(&self); } pub struct Ba
背景 考虑一个玩具问题,其中我有一个 Node表示链接列表节点的结构,并且我想创建一个函数来构建一个值从 1 到 9 的列表。以下代码按预期工作: struct Node { val: i32
我是一名优秀的程序员,十分优秀!