在 "if let Some(ref mut x) = option" 和 "if let Some(x) = option.as_mut()" 中匹配可变选项引用有什么区别?
What is the difference between matching a mutable Option reference in "if let Some(ref mut x) = option" and in "if let Some(x) = option.as_mut()"?
背景
考虑一个玩具问题,其中我有一个表示链表节点的 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
函数中的match表达式修改为如下,编译失败:
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()
?
(作为学习 Rust 的初学者)我原以为这些匹配表达式是等价的,但显然我遗漏了一些细微的差别。
更新
我清理了原始示例中的代码,这样我就不需要列表头部的占位符元素。差异(和编译错误)仍然存在,我只是得到一个额外的编译错误,用于分配给借来的 *tail
.
更新 2
(这只是一个好奇的观察,无助于回答原始问题)
在考虑了@Emoun 的回答之后,听起来很重要(在第一个工作示例中)编译器应该能够知道 tail
在循环的每次迭代中都在变化(这样它可以确保 &mut x.next
每次借的都不一样)。因此,我做了一个实验,通过向 tail = &mut x.next;
赋值添加 if n % 2 == 0
条件,以编译器无法判断是否是这种情况的方式更改代码。果然,导致了和另一个类似的编译错误:
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 的 methods/function 总是借用整个对象而不是它们的一部分。
在您的情况下,这意味着 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
没有被借用,因此可以愉快地重新分配。
Method/function 调用的局限性在于它们不知道您稍后将使用对象的哪些部分。因此,as_mut()
必须借用整个 tail
,即使您只使用它的一部分。这是类型系统的一个限制,也是 getter/setter 方法比直接调用结构的成员 weaker/more 有限制的原因之一:getters/setters 会强制你借用整个结构,同时访问一个member directly 只会借用那个成员,不会借用其他人。
背景
考虑一个玩具问题,其中我有一个表示链表节点的 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
函数中的match表达式修改为如下,编译失败:
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()
?
(作为学习 Rust 的初学者)我原以为这些匹配表达式是等价的,但显然我遗漏了一些细微的差别。
更新
我清理了原始示例中的代码,这样我就不需要列表头部的占位符元素。差异(和编译错误)仍然存在,我只是得到一个额外的编译错误,用于分配给借来的 *tail
.
更新 2
(这只是一个好奇的观察,无助于回答原始问题)
在考虑了@Emoun 的回答之后,听起来很重要(在第一个工作示例中)编译器应该能够知道 tail
在循环的每次迭代中都在变化(这样它可以确保 &mut x.next
每次借的都不一样)。因此,我做了一个实验,通过向 tail = &mut x.next;
赋值添加 if n % 2 == 0
条件,以编译器无法判断是否是这种情况的方式更改代码。果然,导致了和另一个类似的编译错误:
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 的 methods/function 总是借用整个对象而不是它们的一部分。
在您的情况下,这意味着 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
没有被借用,因此可以愉快地重新分配。
Method/function 调用的局限性在于它们不知道您稍后将使用对象的哪些部分。因此,as_mut()
必须借用整个 tail
,即使您只使用它的一部分。这是类型系统的一个限制,也是 getter/setter 方法比直接调用结构的成员 weaker/more 有限制的原因之一:getters/setters 会强制你借用整个结构,同时访问一个member directly 只会借用那个成员,不会借用其他人。