pattern-matching - Rust: "cannot move out of ` self` 因为它是借来的”错误

标签 pattern-matching rust lifetime

我正在尝试编写一个递归方法,将一个项目添加到树中并返回与该项目对应的树节点。

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 中,leftrightself 变量拥有。借用检查器不知道在

的 Equal 分支中
match item.cmp(node_item) {
    Less => left.insert(item),
    Equal => self,
    Greater => right.insert(item)
}

node_itemleftright 都没有被使用,但是它看到 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/

相关文章:

.net - 使用单例区分联合制作计算表达式

string - 在 Rust 中,将 &str 拆分为每个包含一个字符的 &strs 迭代器的惯用方法是什么?

rust - cargo 生成在 Ubuntu 20.04 上安装失败

rust - 为什么 MutexGuard 在结构中需要生命周期参数,但在函数返回类型中不需要?

scala - 如何在scala中使用没有案例类的模式匹配?

PHP - 如何找到数组中的重复值分组

scala - 如何模式匹配继承树中的抽象父类

rust - 你如何在 Rust 的通用特征上实现特定类型?

rust - 为什么我的结果类型别名不满足 failure::Fail 特征绑定(bind)?

rust - 在 Rust 中正确设置生命周期和可变性期望