title: Rust "Types differ in mutability"
body: <p>I'm trying to remove an element from a vector if it existed in it:</p>
  
  <pre><code>use std::collections::HashMap;
  
  fn test(map: HashMap&lt;String, Vec&lt;String&gt;&gt;, department: String, employee: String) {
      let &amp;mut list = map.get(&amp;department).unwrap();
      let index = list.iter().position(|x| x == &amp;employee);
      match index {
          Some(i) =&gt; {
              list.remove(i);
          },
          None =&gt; {
              println!("No records of {} in {}!", &amp;employee, &amp;department);
          },
      }
  }
  </code></pre>
  
  <p>I'm getting this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
   --&gt; src/lib.rs:4:9
    |
  4 |     let &amp;mut list = map.get(&amp;department).unwrap();
    |         ^^^^^^^^^   ----------------------------- this expression has type `&amp;std::vec::Vec&lt;std::string::String&gt;`
    |         |
    |         types differ in mutability
    |
    = note:      expected reference `&amp;std::vec::Vec&lt;std::string::String&gt;`
            found mutable reference `&amp;mut _`
  </code></pre>
  
  <p><sup><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=40888df3bc4daf7f6979ef04b3f7533c" rel="nofollow noreferrer">Playground</a></sup></p>
  
  <p>I thought I understood what the error is saying (line 170's RHS returns a immutable reference to the vector), but I'm not so sure how to resolve it. If try something like this instead:</p>
  
  <pre><code>let mut list = map.get(&amp;department).unwrap();
  let index = list.iter().position(|x| x == &amp;employee);
  match index {
      Some(i) =&gt; {
          list.remove(i);
      },
      ...
  }
  </code></pre>
  
  <p>I then get </p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0596]: cannot borrow `*list` as mutable, as it is behind a `&amp;` reference
   --&gt; src/lib.rs:8:13
    |
  4 |     let mut list = map.get(&amp;department).unwrap();
    |         -------- help: consider changing this to be a mutable reference: `&amp;mut std::vec::Vec&lt;std::string::String&gt;`
  ...
  8 |             list.remove(i);
    |             ^^^^ `list` is a `&amp;` reference, so the data it refers to cannot be borrowed as mutable
  </code></pre>
  
  <p><sup><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5ca232b6741143a0b54bee49a59dbc94" rel="nofollow noreferrer">Playground</a></sup></p>
  
  <p>These errors seem kind of circular to me, which makes me feel I need to rethink my design. How would I go about fixing this issue?</p>