How do I recursively pass a mutable reference?
<p>I am attempting to solve <a href="https://projecteuler.net/problem=2" rel="nofollow noreferrer">this problem</a> in Rust.</p>

<p>Here is my non-compiling Rust code:</p>

<pre><code>use std::collections::HashMap;

fn main() {
    // initialize HashMap
    let mut fibs: HashMap&lt;u32, u32&gt; = HashMap::new();
    fibs.insert(0, 1);
    fibs.insert(1, 1);
    let mut n = 1;
    let mut sum = 0;
    while fib(n, &amp;mut fibs) &lt; 4000000 {
        sum += if fib(n, &amp;mut fibs) % 2 == 0 {
            fib(n, &amp;mut fibs)
        } else {
            0
        };
        n += 1;
    }
    println!("{}", sum);
}

fn fib(n: u32, fibs: &amp;mut HashMap&lt;u32, u32&gt;) -&gt; u32 {
    if !fibs.contains_key(&amp;n) {
        fibs.insert(n, fib(n - 1, &amp;mut fibs) + fib(n - 2, &amp;mut fibs));
    }
    *fibs.get(&amp;n).unwrap()
}
</code></pre>

<pre class="lang-none prettyprint-override"><code>error[E0596]: cannot borrow `fibs` as mutable, as it is not declared as mutable
  --&gt; src/main.rs:22:35
   |
20 | fn fib(n: u32, fibs: &amp;mut HashMap&lt;u32, u32&gt;) -&gt; u32 {
   |                ---- help: consider changing this to be mutable: `mut fibs`
21 |     if !fibs.contains_key(&amp;n) {
22 |         fibs.insert(n, fib(n - 1, &amp;mut fibs) + fib(n - 2, &amp;mut fibs));
   |                                   ^^^^^^^^^ cannot borrow as mutable

error[E0499]: cannot borrow `fibs` as mutable more than once at a time
  --&gt; src/main.rs:22:35
   |
22 |         fibs.insert(n, fib(n - 1, &amp;mut fibs) + fib(n - 2, &amp;mut fibs));
   |         ---- ------               ^^^^^^^^^ second mutable borrow occurs here
   |         |    |
   |         |    first borrow later used by call
   |         first mutable borrow occurs here

error[E0596]: cannot borrow `fibs` as mutable, as it is not declared as mutable
  --&gt; src/main.rs:22:59
   |
20 | fn fib(n: u32, fibs: &amp;mut HashMap&lt;u32, u32&gt;) -&gt; u32 {
   |                ---- help: consider changing this to be mutable: `mut fibs`
21 |     if !fibs.contains_key(&amp;n) {
22 |         fibs.insert(n, fib(n - 1, &amp;mut fibs) + fib(n - 2, &amp;mut fibs));
   |                                                           ^^^^^^^^^ cannot borrow as mutable

error[E0499]: cannot borrow `fibs` as mutable more than once at a time
  --&gt; src/main.rs:22:59
   |
22 |         fibs.insert(n, fib(n - 1, &amp;mut fibs) + fib(n - 2, &amp;mut fibs));
   |         ---- ------ first borrow later used by call       ^^^^^^^^^ second mutable borrow occurs here
   |         |
   |         first mutable borrow occurs here
</code></pre>

<p>The Rust to Python3 translation looks like this:</p>

<pre class="lang-py prettyprint-override"><code>def main():
    fibs = {}
    fibs[0] = 1
    fibs[1] = 1
    n = 1
    summ = 0
    while fib(n, fibs) &lt; 4000000:
        summ += fib(n, fibs) if fib(n, fibs) % 2 == 0 else 0
        n+=1
    print(summ)
    print(fibs)
def fib(n, fibs):
    if n not in fibs:
        fibs[n] = fib(n-1, fibs) + fib(n-2, fibs)
    return fibs[n]
main()
</code></pre>

<p>I understand that this particular implementation is not ideal, but I am solely trying to learn the language. I am trying to only pass a reference of the hashmap to the function. Without changing the approach to solving this problem, how can I use mutable <code>HashMap</code> references, if it's even possible?</p>