不能借用不可变借来的内容作为可变性

时间:2016-01-26 01:41:33

标签: rust

我正在尝试开发邮件路由应用。我已经阅读了正式的Rust文档和一些文章,并认为我得到了指针,拥有和借用的东西,但意识到我没有。

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

struct Component {
    address: &'static str,
    available_workers: i32,
    lang: i32
}

struct Components {
    data: HashMap<i32, Vec<Component>>
}

impl Components {
    fn new() -> Components {
        Components {data: HashMap::new() }
    }

    fn addOrUpdate(&mut self, component: Component) -> &Components {
        if !self.data.contains_key(&component.lang) {

            self.data.insert(component.lang, vec![component]);
        } else {
            let mut q = self.data.get(&component.lang); // this extra line is required because of the error: borrowed value does not live long enough
            let mut queue = q.as_mut().unwrap();
            queue.remove(0);
            queue.push(component);
        }
        self
    }

}

(也可在playground上找到)

产生错误:

error: cannot borrow immutable borrowed content `**queue` as mutable
  --> src/main.rs:26:13
   |
26 |             queue.remove(0);
   |             ^^^^^ cannot borrow as mutable

error: cannot borrow immutable borrowed content `**queue` as mutable
  --> src/main.rs:27:13
   |
27 |             queue.push(component);
   |             ^^^^^ cannot borrow as mutable

请您解释一下这个错误,如果能给我正确的实施,那将会很棒。

1 个答案:

答案 0 :(得分:32)

以下是您问题的MCVE

use std::collections::HashMap;

struct Components {
    data: HashMap<u8, Vec<u8>>,
}

impl Components {
    fn add_or_update(&mut self, component: u8) {
        let mut q = self.data.get(&component);
        let mut queue = q.as_mut().unwrap();
        queue.remove(0);
    }
}

在NLL之前

error[E0596]: cannot borrow immutable borrowed content `**queue` as mutable
  --> src/lib.rs:11:9
   |
11 |         queue.remove(0);
   |         ^^^^^ cannot borrow as mutable

NLL之后

error[E0596]: cannot borrow `**queue` as mutable, as it is behind a `&` reference
  --> src/lib.rs:11:9
   |
11 |         queue.remove(0);
   |         ^^^^^ cannot borrow as mutable

很多时候,当这样的事情看起来令人惊讶时,它对print out the types involved很有用。我们打印出queue

的类型
let mut queue: () = q.as_mut().unwrap();
error[E0308]: mismatched types
  --> src/lib.rs:10:29
   |
10 |         let mut queue: () = q.as_mut().unwrap();
   |                             ^^^^^^^^^^^^^^^^^^^ expected (), found mutable reference
   |
   = note: expected type `()`
              found type `&mut &std::vec::Vec<u8>`

我们对不可变Vec<u8>的引用有 mutable 引用。因为我们对Vec有一个不可变的引用,所以我们无法修改它!将self.data.get更改为self.data.get_mut会将类型更改为&mut &mut collections::vec::Vec<u8>并编译代码。

如果您想实现“插入或更新”的概念,您应该查看entry API,这样更有效,更简洁。

除此之外,Rust使用snake_case进行方法命名,而不是camelCase

相关问题