title: Strange borrowing lifetime in Rust
body: <p>I'm building a little Rust application used to make some statistics about programs and projects. My initial goal was to make it generic, so it can be modified and used as a library.</p>
  
  <p>To do that I've created a <code>Processor</code> structure:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub struct Processor {
      handles: Vec&lt;Box&lt;dyn FileTypeHandle&gt;&gt;,
      files_types: HashMap&lt;String, Box&lt;dyn FileType&gt;&gt;,
      bytes_count: usize,
      files_count: usize,
      dirs_count: usize,
  }
  </code></pre>
  
  <p>What I've named <code>handle</code> is an implementation of <code>FileTypeHandle</code> trait that checks, from the extension of a file, if it can handle it. If it can, the file is processed by it and statistics are made.</p>
  
  <p>Secondly, <code>file_types</code> are implementations of <code>FileType</code> trait that implement methods to write final statistics in console.</p>
  
  <p>Let's assume that my <code>handles</code> vector is filled with some handles.</p>
  
  <p>First, I needed to implement a function in the <code>Processor</code> to search in <code>handles</code> vector for one that can handle the <code>ext</code> file extension:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub fn handle_ext(&amp;mut self, ext: &amp;String) -&gt; Option&lt;&amp;Box&lt;dyn FileTypeHandle&gt;&gt; {
      self.handles
          .iter()
          .find(move |handle: &amp;&amp;Box&lt;dyn FileTypeHandle&gt;| handle.can_handle(ext))
  }
  </code></pre>
  
  <p><code>FileTypeHandle::can_handle</code> is defined as:</p>
  
  <pre class="lang-rust prettyprint-override"><code>fn can_handle(&amp;self, ext: &amp;String) -&gt; bool;
  </code></pre>
  
  <p>I'm using the next function to get a file type in the processor, or create a default one:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub fn get_file_type(&amp;mut self, ext: &amp;String) -&gt; Option&lt;&amp;Box&lt;dyn FileType&gt;&gt; {
      if !self.files_types.contains_key(ext) {
          let base = FileTypeBase::new(String::clone(ext));
  
          if let Some(v) = self.handle_ext(ext) {
              self.files_types.insert(String::clone(ext), v.handle(base));
          }
      }
  
      self.files_types.get(ext)
  }
  </code></pre>
  
  <p>I think you understand what I expect from this code, but:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0499]: cannot borrow `self.files_types` as mutable more than once at a time
    --&gt; src\processor.rs:61:17
     |
  59 |             if let Some(v) = self.handle_ext(ext) {
     |                              ---- first mutable borrow occurs here
  60 | 
  61 |                 self.files_types.insert(String::clone(ext), v.handle(base));
     |                 ^^^^^^^^^^^^^^^^                            - first borrow later used here
     |                 |
     |                 second mutable borrow occurs here
  </code></pre>
  
  <p>I've read lot of Rust documentation including the official book, but I didn't know why the static compilation check fails.</p>
  
  <p>Okay, line 61 uses the <code>self.file_types</code> as said in the error message, but why is it the second borrow?! The first underlined word on line 59 is just <code>self</code> (or <code>self.handle_ext</code>?) which are not borrowing <code>self.file_types</code>.</p>