我正在尝试编写一个递归方法,将一个项目添加到树中并返回与该项目对应的树节点。
enum BstNode {
Node(int, ~BstNode, ~BstNode),
Leaf
}
impl BstNode {
fn insert<'a>(&'a mut self, item: int) -> &'a mut BstNode {
match *self {
Leaf => {
*self = Node(item, ~Leaf, ~Leaf);
self
},
Node(ref node_item, ref mut left, ref mut right) =>
match item.cmp(node_item) {
Less => left.insert(item),
Equal => self,
Greater => right.insert(item)
}
}
}
}
我遇到了以下错误:
bst.rs:19:30: 19:34 error: cannot move out of `self` because it is borrowed
bst.rs:19 Equal => self,
^~~~
bst.rs:16:18: 16:31 note: borrow of `self#0` occurs here
bst.rs:16 Node(ref node_item, ref mut left, ref mut right) =>
^~~~~~~~~~~~~
“搬出某物
”是什么意思?如何修复此错误?
我正在使用 Rust 0.10。
最佳答案
在您的示例 node_item
中,left
和 right
由 self
变量拥有。借用检查器不知道在
match item.cmp(node_item) {
Less => left.insert(item),
Equal => self,
Greater => right.insert(item)
}
node_item
、left
和 right
都没有被使用,但是它看到 self
在移动(你是返回它)而这 3 个变量仍然是借用的(你仍然在匹配的词法范围内,它们被借用的地方)。我认为这是一个已知的错误,这种行为过于严格,请参阅 issue #6993 .
至于修复代码的最佳方法,老实说我不确定。我会使用完全不同的结构(至少在修复之前的错误之前):
pub struct BstNode {
item: int,
left: Option<~BstNode>,
right: Option<~BstNode>
}
impl BstNode {
pub fn insert<'a>(&'a mut self, item: int) -> &'a mut BstNode {
match item.cmp(&self.item) {
Less => match self.left {
Some(ref mut lnode) => lnode.insert(item),
None => {
self.left = Some(~BstNode {item: item, left: None, right: None});
&mut **self.left.as_mut().unwrap()
}
},
Equal => self,
Greater => match self.right {
Some(ref mut rnode) => rnode.insert(item),
None => {
self.right = Some(~BstNode {item: item, left: None, right: None});
&mut **self.right.as_mut().unwrap()
}
}
}
}
}
这样当你返回你的节点时,你永远不会有任何它的成员仍然被借用。
关于pattern-matching - Rust: "cannot move out of ` self` 因为它是借来的”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23328702/