How can I convince the borrow checker to allow me to cache values?
问题 The borrow checker beat me: use std::collections::HashMap; struct Cache { cache: Vec<HashMap<String, String>>, } impl Cache { fn get(&mut self, index: usize, key: String) -> String { let mut cache = &mut self.cache[index]; match cache.get(&key) { Some(r) => { return r.clone(); } None => { let r = "foo".to_string(); // something smart here cache.insert(key, r.clone()); return r; } } } } What I get: error[E0502]: cannot borrow `*cache` as mutable because it is also borrowed as immutable --> src