不能借用 `*self` 作为可变,因为它也被借用为不可变 [英] cannot borrow `*self` as mutable because it is also borrowed as immutable

查看:52
本文介绍了不能借用 `*self` 作为可变,因为它也被借用为不可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 struct 函数在特殊条件下调用自己.当我将 HashMap 作为字段之一时它起作用,但是当我将 HashMap 更改为 Vec 时它会中断.它甚至不必使用,这看起来很奇怪,我找不到任何合理的解释.

I want my struct function to call itself under special conditions. It worked when I had a HashMap as one of the fields, but it broke when I changed the HashMap to be a Vec. It doesn't even have to be used, which seems very weird and I can't find any reasonable explanation for this.

use std::vec::Vec;
use std::collections::HashMap;

struct Foo<'a> {
    bar: Vec<&'a str>
    //bar: HashMap<&'a str, &'a str>
}

impl<'a> Foo<'a> {
    pub fn new() -> Foo<'a> {
        Foo { bar: Vec::new() }
        //Foo { bar: HashMap::new() }
    }

    pub fn baz(&'a self) -> Option<int> {
        None
    }

    pub fn qux(&'a mut self, retry: bool) {
        let opt = self.baz();
        if retry { self.qux(false); }
    }
}

pub fn main() {
   let mut foo = Foo::new();
   foo.qux(true);
}

围栏:http://is.gd/GgMy79

错误:

<anon>:22:24: 22:28 error: cannot borrow `*self` as mutable because it is also borrowed as immutable
<anon>:22             if retry { self.qux(false); }
                                 ^~~~
<anon>:21:23: 21:27 note: previous borrow of `*self` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `*self` until the borrow ends
<anon>:21             let opt = self.baz();
                                ^~~~
<anon>:23:10: 23:10 note: previous borrow ends here
<anon>:20         pub fn qux(&'a mut self, retry: bool) {
<anon>:21             let opt = self.baz();
<anon>:22             if retry { self.qux(false); }
<anon>:23         }

我该如何解决这个问题?这可能是由 #6268 引起的吗?

How can I fix this? Could this be caused by #6268?

推荐答案

我想我找到了原因.这是HashMap定义:

I guess I found the reason. This is HashMap definition:

pub struct HashMap<K, V, H = RandomSipHasher> {
    // All hashes are keyed on these values, to prevent hash collision attacks.
    hasher: H,

    table: RawTable<K, V>,

    // We keep this at the end since it might as well have tail padding.
    resize_policy: DefaultResizePolicy,
}

这是Vec定义:

pub struct Vec<T> {
    ptr: *mut T,
    len: uint,
    cap: uint,
}

唯一的区别是类型参数的使用方式.现在让我们检查一下这段代码:

The only difference is how type parameters are used. Now let's check this code:

struct S1<T> { s: Option<T> }
//struct S1<T> { s: *mut T }

struct Foo<'a> {
    bar: S1<&'a str>
}

impl<'a> Foo<'a> {
    pub fn new() -> Foo<'a> {  // '
        Foo { bar: S1 { s: None } }
        //Foo { bar: S1 { s: std::ptr::null_mut() } }
    }

    pub fn baz(&'a self) -> Option<int> {
        None
    }

    pub fn qux(&'a mut self, retry: bool) {
        let opt = self.baz();
        if retry { self.qux(false); }
    }
}

pub fn main() {
   let mut foo = Foo::new();
   foo.qux(true);
}

这个编译通过.如果您为 S1 选择另一个定义,使用 *mut T 指针,则程序将失败并显示此错误.

This one compiles. If you pick another definition for S1, with *mut T pointer, then the program fails with exactly this error.

我认为这看起来像是一个错误,大约是生命周期差异.

This looks like a bug, somewhere around lifetime variance, I think.

更新这里

这篇关于不能借用 `*self` 作为可变,因为它也被借用为不可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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