Cannot borrow immutable borrowed content as mutable
Asked Answered
E

1

23

I'm trying to develop a message routing app. I've read the official Rust docs and some articles and thought that I got how pointers, owning, and borrowing stuff works but realized that I didn't.

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
    }

}

(Also available on the playground)

Produces the error:

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

Could you please explain the error and it would be great if you can give me the right implementation.

Electrotechnology answered 26/1, 2016 at 1:41 Comment(0)
F
37

Here is an MCVE of your problem:

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

Before NLL

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

After 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

Many times, when something seems surprising like this, it's useful to print out the types involved. Let's print out the type of 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>`

We have a mutable reference to an immutable reference to a Vec<u8>. Because we have an immutable reference to the Vec, we cannot modify it! Changing self.data.get to self.data.get_mut changes the type to &mut &mut collections::vec::Vec<u8> and the code compiles.


If you want to implement the concept of "insert or update", you should check into the entry API, which is more efficient and concise.

Beyond that, Rust uses snake_case for method naming, not camelCase.

Futurism answered 26/1, 2016 at 2:56 Comment(3)
Thanks. Now I understand it better. Thanks for advice about printing types, it turns out very helpful for troubleshootingElectrotechnology
Is the link under print out the types involved still available? There is a SO questions, but I thought the link pointed to some answer.Minelayer
@Minelayer the link is to the question. That allows a better answer to come in or for the implementer to pick a different answer if the situation is different. In this case I used the "assign to the empty tuple" version.Futurism

© 2022 - 2024 — McMap. All rights reserved.