在HashMap-value之后匹配的更优雅方式比克隆它

时间:2017-08-26 12:49:23

标签: caching hash rust

我正在使用The Rust Programming Language学习Rust。我正在chapter dealing with closures中处理以下任务:

  

尝试修改Cacher以保存哈希映射而不是单个值。哈希映射的键将是传入的arg值,并且哈希映射的值将是在该键上调用闭包的结果。而不是查看self.value是否直接具有SomeNone值,而值函数将在哈希映射中查找arg并返回值(如果它存在) 。如果它不存在,Cacher将调用闭包并将结果值保存在与其arg值关联的哈希映射中。

这是我的解决方案:

use std::collections::HashMap;

struct Cacher<T>
where
    T: Fn(i32) -> i32,
{
    calculation: T,
    values: HashMap<i32, i32>,
}

impl<T> Cacher<T>
where
    T: Fn(i32) -> i32,
{
    fn new(calculation: T) -> Cacher<T> {
        Cacher {
            calculation,
            values: HashMap::new(),
        }
    }
    fn value(&mut self, arg: i32) -> i32 {
        match self.values.get(&arg) {
            Some(v) => *v,
            None => {
                let v = (self.calculation)(arg);
                self.values.insert(arg, v);
                v
            }
        }
    }
}

编译错误:

error[E0502]: cannot borrow `self.values` as mutable because it is also borrowed as immutable
  --> src/main.rs:26:17
   |
22 |         match self.values.get(&arg) {
   |               ----------- immutable borrow occurs here
...
26 |                 self.values.insert(arg, v);
   |                 ^^^^^^^^^^^ mutable borrow occurs here
...
29 |         }
   |         - immutable borrow ends here

我可以通过使用:

来解决这个问题
match self.values.clone().get(&arg) { ...

我是否真的需要克隆整个HashMap以在match-statement中使用它并在之后插入一个值?

0 个答案:

没有答案