Kann nicht unveränderliche geliehene Inhalte als veränderlich ausleihen
Ich versuche, eine Nachrichtenrouting-App zu entwickeln. Ich habe die offiziellen Rust-Dokumente und einige Artikel gelesen und gedacht, dass ich weiß, wie Zeiger, Besitz und Ausleihen von Sachen funktionieren, habe aber gemerkt, dass ich das nicht getan hab
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 verfügbar auf demSpielplat)
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önnten Sie bitte den Fehler erklären und es wäre toll, wenn Sie mir die richtige Implementierung geben könnten.