我正在使用The Rust Programming Language学习Rust。我正在chapter dealing with closures中处理以下任务:
尝试修改
Cacher
以保存哈希映射而不是单个值。哈希映射的键将是传入的arg
值,并且哈希映射的值将是在该键上调用闭包的结果。而不是查看self.value
是否直接具有Some
或None
值,而值函数将在哈希映射中查找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中使用它并在之后插入一个值?