Rust:“因为借用而不能移出“自我"".错误 [英] Rust: "cannot move out of `self` because it is borrowed" error

查看:462
本文介绍了Rust:“因为借用而不能移出“自我"".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a recursive method that adds an item to a tree and returns the tree node corresponding to that item.

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) =>
                           ^~~~~~~~~~~~~

移出something"是什么意思?如何解决此错误?

What does "moving out of something" mean? How do I fix this error?

我正在使用Rust 0.10.

I'm using Rust 0.10.

推荐答案

在您的示例中,node_itemleftrightself变量拥有.借用检查器在

In your example node_item, left and right are owned by the self variable. The borrow checker doesn't know that in the Equal branch of

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

既没有使用node_itemleft也没有使用right,但是它看到self正在移动(正在返回),而这3个变量仍在借用(您仍然在匹配(在哪里借用).我认为这是一个已知的错误,表明此行为过于严格,请参见 issue#6993 .

neither node_item, left nor right is used, but it sees that self is moving (you are returning it) while those 3 variables are still borrowed (you are still in the lexical scope of the match, where they are borrowed). I think this is a known bug that this behavior is too strict, see issue #6993.

关于修复代码的最佳方法,老实说,我不确定.我会使用完全不同的结构(至少在修复之前的错误之前):

As for the best way to fix the code, honestly I'm not sure. I would go with using a completely different structure (at least until the previous bug is fixed) :

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()
              }
            }
        }
    }
}

这样,当您返回节点时,就永远不会再借用它的任何成员.

This way when you return your node, you never have any of its members still borrowed.

这篇关于Rust:“因为借用而不能移出“自我"".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆