title: Cannot get Hash::get_mut() and File::open() to agree about mutability
body: <p>During a lengthy computation, I need to look up some data in a number of different files. I cannot know beforehand how many or which files exactly, but chances are high that each file is used many times (on the order of 100 million times).</p>
  <p>In the first version, I opened the file (whose name is an intermediate result of the computation) each time for lookup.</p>
  <p>In the second version, I have a <code>HashMap&lt;String, Box&lt;File&gt;&gt;</code> where I remember already open files and open new ones lazily on demand.</p>
  <p>I couldn't manage to handle the mutable stuff that arises from the need to have <code>File</code>s to be mutable. I got something working, but it looks overly silly:</p>
  <pre class="lang-rust prettyprint-override"><code>let path = format!(&quot;egtb/{}.egtb&quot;, self.signature());
  let hentry = hash.get_mut(&amp;self.signature());
  let mut file = match hentry {
      Some(f) =&gt; f,
      None =&gt; {
          let rfile = File::open(&amp;path);
          let wtf = Box::new(match rfile {
              Err(ioe) =&gt; return Err(format!(&quot;could not open EGTB file {} ({})&quot;, path, ioe)),
              Ok(opened) =&gt; opened,
          });
          hash.insert(self.signature(), wtf);
          // the following won't work
          // wtf
          // &amp;wtf
          // &amp;mut wtf
          // So I came up with the following, but it doesn't feel right, does it?
          hash.get_mut(&amp;self.signature()).unwrap()
      }
  };
  </code></pre>
  <p>Is there a canonical way to get a <code>mut File</code> from <code>File::open()</code> or <code>File::create()</code>? In the manuals, this is always done with:</p>
  <pre><code>let mut file = File:open(&quot;foo.txt&quot;)?;
  </code></pre>
  <p>This means my function would have to return <code>Result&lt;_, io::Error&gt;</code> and I can't have that.</p>
  <p>The problem seems to be that with the hash-lookup <code>Some(f)</code> gives me a <code>&amp;mut File</code> but the <code>Ok(f)</code> from <code>File::open</code> gives me just a <code>File</code>, and I don't know how to make a mutable reference from that, so that the match arm's types match. I have no clear idea why the version as above at least compiles, but I'd very much like to learn how to do that without getting the <code>File</code> from the <code>HashMap</code> again.</p>