如何说服借用检查器允许我缓存值?

def*_*fuz 3 rust borrow-checker

借款检查员打败了我:

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

error[E0502]: cannot borrow `*cache` as mutable because it is also borrowed as immutable
  --> src/main.rs:16:17
   |
10 |         match cache.get(&key) {
   |               ----- immutable borrow occurs here
...
16 |                 cache.insert(key, r.clone());
   |                 ^^^^^ mutable borrow occurs here
...
19 |         }
   |         - immutable borrow ends here
Run Code Online (Sandbox Code Playgroud)

如何重写我的代码以便编译?

Bur*_*hi5 5

另一种方法是使用entry界面.这种方法的唯一缺点是它(当前)不使用BorrowFromget方法使用的基础结构,这使其灵活性降低.在您的情况下,这不是一个问题,因为get需要一个拥有的密钥.优点entry是它只进行一次哈希查找,而使用get强制进行两次查找.

use std::collections::HashMap;

struct Cache {
    cache: Vec<HashMap<String, String>>,
}

impl Cache {
    fn get(&mut self, index: usize, key: String) -> String {
        self.cache[index]
            .entry(key)
            .or_insert_with(|| "foo".to_string())
            .clone()
    }
}
Run Code Online (Sandbox Code Playgroud)