Nicht leihen unveränderlich ausgeliehen Inhalt als veränderlich

Ich versuche zu entwickeln, die eine message-routing-app. Ich habe gelesen, die offizielle Rost docs und einige Artikel und dachte, dass ich bekam, wie Zeiger, den Besitz und Kreditaufnahme Zeug funktioniert, aber erkannte, dass ich nicht.

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
    }

}

(Auch auf der Spielplatz)

Erzeugt den Fehler:

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

Könntest du bitte erläutern Sie den Fehler und es wäre toll, wenn du mir die richtige Umsetzung.

InformationsquelleAutor dmgcodevil | 2016-01-26
Schreibe einen Kommentar