在同一功能中多次使用self

风战士

我与借阅检查器有一个论点。我的问题稍微复杂一点,但是在这种情况下,我使用的是类似结构的缓冲区。我的缓冲区具有一个函数safe_write_to_slot该函数首先检索第一个空元素(返回Ok(位置)或Err(错误消息)的结果),然后将值写入该检索到的位置。但是问题是,当我将检索到的位置分配给一个值时,rust会抱怨我稍后再使用一次self。我如何首先调用一个返回结果的(self)函数,然后继续使用self做一些动作?

use std::result::Result;

struct Elems {
    pub elems : Vec<int>,
}

impl Elems {
    pub fn new() -> Elems {
        Elems{elems: vec![0,0,0,0,0,0]}
    }

    pub fn safe_write_to_slot(&mut self, elem : uint) -> Result<(), &str> {
        let loc = try!(self.get_slot());

        self.unsafe_write_to_slot(loc);

        Ok(())
    }

    pub fn get_slot(&self) -> Result<uint, &str>{
        let mut loc = -1i;

        for x in range(0, self.elems.len()) {
            if *self.elems.get(x) == 0 {
                loc = x as int;
            }
        }

        if loc != -1 { Ok(loc as uint) } else { Err("No free slots") }
    }

    fn unsafe_write_to_slot(&mut self, elem : uint) {
        self.elems[elem] = 1;
    }
}

我得到的错误是:

   Compiling borrow v0.0.1 (file:///borrow)
main.rs:19:9: 19:13 error: cannot borrow `*self` as mutable because it is also borrowed as immutable
main.rs:19         self.unsafe_write_to_slot(loc);
                   ^~~~
main.rs:17:24: 17:28 note: previous borrow of `*self` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `*self` until the borrow ends
main.rs:17         let loc = try!(self.get_slot());
                                  ^~~~
/main.rs:17:19: 17:41 note: expansion site
main.rs:22:6: 22:6 note: previous borrow ends here
main.rs:16     pub fn safe_write_to_slot(&mut self, elem : uint) -> Result<(), &str> {
/main.rs:22     }
               ^
main.rs:37:9: 37:29 error: cannot assign to immutable dereference (dereference is implicit, due to indexing)
main.rs:37         self.elems[elem] = 1;
                   ^~~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
Could not compile `borrow`.

To learn more, run the command again with --verbose.
弗朗西斯·加涅

终生推断是导致此问题的原因。

get_slot方法解释为:

pub fn get_slot<'a>(&'a self) -> Result<uint, &'a str> {

结果与绑定到相同的生存期self,这将导致self冻结,直到删除结果为止。但是,您不希望将的生存期链接self&str,因为您只返回字符串文字。通过更改&str&'static stringet_slotsafe_write_to_slot,您将不再收到错误,因为self在调用时将不再被认为是借来的unsafe_write_to_slot

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过网格视图功能在同一页面中多次使用同一用户控件

Numpy高级索引:同一索引在+ =中多次使用

我可以在TabBarController中多次使用同一ViewController吗?

在同一列中多次使用“或”,“和”,“喜欢”

在CSS / LESS中连续多次使用同一类

JavaScript函数只能运行一次?尝试多次使用同一功能

在Angular Test中使用不同的参数多次监视同一功能

多次使用同一脚本导致功能错误

在同一查询中多次插入?

在python中多次调用同一函数

在select中多次调用同一函数

HTTPD多次使用同一端口

如何多次使用同一外键

为什么我可以多次导出同一功能?

无法在同一进程中多次使用 imp.load_module 加载同一个模块

如何在Qore的SqlUtil中的where哈希中多次使用同一列

如何使用Javascript中的DOM在HTML父标记中多次插入同一图像?

如何从Java Swing中多次使用的同一文本字段中获取文本

如何在同一datagridview中多次使用已定义的datagridview单元

如何在同一组件中多次使用自定义组件

如何使用sed搜索和替换在同一行中多次出现的模式?

在Rust中多次使用同一个迭代器

Java:使用新参数在输出流中多次写入同一对象

如何在不同的输入框中多次使用同一警报框?

如何使用PL / SQL中的循环多次运行同一查询?

如何在同一页面中多次使用AngularJS 2组件?

如何在sql server中的同一列上多次使用case语句

如何在表单中多次使用同一字段?

在React中的页面上多次使用同一组件时道具混合