Hi,

I have updated the "Cacher" exercise from chapter 13.1 of "The Book of Rust". In my attempts to make it work I first was returning references to values from the HashMap... and I got compilation errors in my tests that I cannot mutably borrow more than once.

Those errors went away when I was no longer returning references.

I have since noticed that I get those errors only when repeatedly trying to get the same value from the "Cacher" ... Which I can see makes sense.

However, I also get the error on lines that retrieve a different value, when it's in the same function that tries to retrieve same value twice.

Can anyone please help me to understand the errors?

Without copy-pasting the full code, for brevity:

```
#[test]
fn test_with_returning_refs_3x_1() {
let mut c = Cacher::new(|a| a);
let v1 = c.value_ref(1);
let v2 = c.value_ref(2); // <-- Error here which I don't get
let v3 = c.value_ref(1); // <-- Error here which I can understand but didn't expect at first. No error if I remove this line.
assert_eq!(v2, &2);
}
```

Error:

```
error[E0499]: cannot borrow `c` as mutable more than once at a time
```

`Cacher`

implemented as follows:

```
struct Cacher<F, V, R>
where F: Fn(V) -> R
{
calculation: F,
values: HashMap<V, R>,
}
impl<F, V, R> Cacher<F, V, R> where
F: Fn(V) -> R,
V: std::marker::Copy + std::cmp::Eq + Hash,
R: std::marker::Copy,
{
fn new(calculation: F) -> Cacher<F, V, R> {
Cacher {
calculation,
values: HashMap::new(),
}
}
fn value_ref(&mut self, arg: V) -> &R {
if !self.values.contains_key(&arg) {
let result = (self.calculation)(arg);
self.values.insert(arg, result);
}
return &self.values[&arg]
}
}
```