为什么在 Rust 元组模式匹配中需要显式借用?
Why is an explicit borrow required in Rust tuple pattern matching?
我正在用 Rust 写一个二叉树,借用检查器真的让我很困惑。这里是 a minimal example that reproduces the problem.
二叉树定义如下:
struct NonEmptyNode;
pub struct BinaryTree {
root: Option<NonEmptyNode>,
}
借用检查器拒绝的代码是:
// Implementation #1
fn set_child_helper(&self, bt: &Self, setter: fn(&NonEmptyNode, &NonEmptyNode)) -> bool {
match (self.root, bt.root) {
(Some(ref rt), Some(ref node)) => {
setter(rt, node);
true
}
_ => false,
}
}
错误信息是
error[E0507]: cannot move out of borrowed content
--> src/main.rs:10:16
|
10 | match (self.root, bt.root) {
| ^^^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> src/main.rs:10:27
|
10 | match (self.root, bt.root) {
| ^^ cannot move out of borrowed content
要使其正常工作,必须将代码修改为:
// Implementation #2
fn set_child_helper(&self, bt: &Self, setter: fn(&NonEmptyNode, &NonEmptyNode)) -> bool {
match (&self.root, &bt.root) {
// explicit borrow
(&Some(ref rt), &Some(ref node)) => {
// explicit borrow
setter(rt, node);
true
}
_ => false,
}
}
如果我在没有显式借用的情况下一次对一个变量进行模式匹配,借用检查器根本不会抱怨:
// Implementation #3
fn set_child_helper(&self, bt: &Self, setter: fn(&NonEmptyNode, &NonEmptyNode)) -> bool {
match self.root {
Some(ref rt) => match bt.root {
// No explict borrow will be fine
Some(ref node) => {
// No explicit borrow will be fine
setter(rt, node);
true
}
_ => false,
},
_ => false,
}
}
为什么实施 #3 不需要显式借用,而实施 #1 需要?
关键是 self.root
和 bt.root
是 "place expression",而元组不是。 #3 起作用的原因是编译器知道如何 "reach through" 中间表达式以绑定到原始存储位置。
另一种看待它的方式:像 self.root
这样非常简单的表达式的特殊之处在于它们看起来和行为都像值(并且具有值类型),但编译器会秘密地记住它是如何达到该值的,允许它返回并获取指向从中读取该值的位置的指针。
判断某物是否为 "place expression" 的简单方法是尝试为其赋值。如果你能做到expr = some_value;
,那么expr
一定是"place expression"。顺便说一下,这也是为什么当你写 &self.root
时,你会得到一个指向 self.root
存储位置的指针,而不是指向 self.root
.
副本的指针
此 "place expression" 业务不适用于元组,因为他们没有此 属性。要构造一个元组,编译器必须实际读取元组元素的值并将它们移动或复制到元组的新存储中。这会破坏编译器可能拥有的任何位置关联:这些值 字面意思 不再是它们以前所在的位置。
最后,您可能想看看 Option::as_ref
,它将 &Option<T>
变成 Option<&T>
。这会让你在 (self.root.as_ref(), bt.root.as_ref())
上匹配 (Some(rt), Some(node))
这样的模式,这可能更方便。
我正在用 Rust 写一个二叉树,借用检查器真的让我很困惑。这里是 a minimal example that reproduces the problem.
二叉树定义如下:
struct NonEmptyNode;
pub struct BinaryTree {
root: Option<NonEmptyNode>,
}
借用检查器拒绝的代码是:
// Implementation #1
fn set_child_helper(&self, bt: &Self, setter: fn(&NonEmptyNode, &NonEmptyNode)) -> bool {
match (self.root, bt.root) {
(Some(ref rt), Some(ref node)) => {
setter(rt, node);
true
}
_ => false,
}
}
错误信息是
error[E0507]: cannot move out of borrowed content
--> src/main.rs:10:16
|
10 | match (self.root, bt.root) {
| ^^^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> src/main.rs:10:27
|
10 | match (self.root, bt.root) {
| ^^ cannot move out of borrowed content
要使其正常工作,必须将代码修改为:
// Implementation #2
fn set_child_helper(&self, bt: &Self, setter: fn(&NonEmptyNode, &NonEmptyNode)) -> bool {
match (&self.root, &bt.root) {
// explicit borrow
(&Some(ref rt), &Some(ref node)) => {
// explicit borrow
setter(rt, node);
true
}
_ => false,
}
}
如果我在没有显式借用的情况下一次对一个变量进行模式匹配,借用检查器根本不会抱怨:
// Implementation #3
fn set_child_helper(&self, bt: &Self, setter: fn(&NonEmptyNode, &NonEmptyNode)) -> bool {
match self.root {
Some(ref rt) => match bt.root {
// No explict borrow will be fine
Some(ref node) => {
// No explicit borrow will be fine
setter(rt, node);
true
}
_ => false,
},
_ => false,
}
}
为什么实施 #3 不需要显式借用,而实施 #1 需要?
关键是 self.root
和 bt.root
是 "place expression",而元组不是。 #3 起作用的原因是编译器知道如何 "reach through" 中间表达式以绑定到原始存储位置。
另一种看待它的方式:像 self.root
这样非常简单的表达式的特殊之处在于它们看起来和行为都像值(并且具有值类型),但编译器会秘密地记住它是如何达到该值的,允许它返回并获取指向从中读取该值的位置的指针。
判断某物是否为 "place expression" 的简单方法是尝试为其赋值。如果你能做到expr = some_value;
,那么expr
一定是"place expression"。顺便说一下,这也是为什么当你写 &self.root
时,你会得到一个指向 self.root
存储位置的指针,而不是指向 self.root
.
此 "place expression" 业务不适用于元组,因为他们没有此 属性。要构造一个元组,编译器必须实际读取元组元素的值并将它们移动或复制到元组的新存储中。这会破坏编译器可能拥有的任何位置关联:这些值 字面意思 不再是它们以前所在的位置。
最后,您可能想看看 Option::as_ref
,它将 &Option<T>
变成 Option<&T>
。这会让你在 (self.root.as_ref(), bt.root.as_ref())
上匹配 (Some(rt), Some(node))
这样的模式,这可能更方便。