title: Do I need to use a `let` binding to create a longer lived value?
body: <p>I've very recently started studying Rust, and while working on a test program, I wrote this method:</p>
  
  <pre><code>pub fn add_transition(&amp;mut self, start_state: u32, end_state: u32) -&gt; Result&lt;bool, std::io::Error&gt; {
      let mut m: Vec&lt;Page&gt;;
      let pages: &amp;mut Vec&lt;Page&gt; = match self.page_cache.get_mut(&amp;start_state) {
          Some(p) =&gt; p,
          None    =&gt; {
              m = self.index.get_pages(start_state, &amp;self.file)?;
              &amp;mut m
          }
      };
  
      // omitted code that mutates pages 
      // ...
  
      Ok(true)
  }
  </code></pre>
  
  <p>it does work as expected, but I'm not convinced about the <code>m</code> variable. If I remove it, the code looks more elegant:</p>
  
  <pre><code>pub fn add_transition(&amp;mut self, start_state: u32, end_state: u32) -&gt; Result&lt;bool, std::io::Error&gt; {
      let pages: &amp;mut Vec&lt;Page&gt; = match self.page_cache.get_mut(&amp;start_state) {
          Some(p) =&gt; p,
          None    =&gt; &amp;mut self.index.get_pages(start_state, &amp;self.file)?
      };
  
      // omitted code that mutates pages
      // ...
  
      Ok(true)
  }
  </code></pre>
  
  <p>but I get: </p>
  
  <pre><code>error[E0716]: temporary value dropped while borrowed
    --&gt; src\module1\mod.rs:28:29
     |
  26 |           let pages: &amp;mut Vec&lt;Page&gt; = match self.page_cache.get_mut(&amp;start_state) {
     |  _____________________________________-
  27 | |             Some(p) =&gt; p,
  28 | |             None    =&gt; &amp;mut self.index.get_pages(start_state, &amp;self.file)?
     | |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
     | |                             |                                            |
     | |                             |                                            temporary value is freed at the end of this statement
     | |                             creates a temporary which is freed while still in use
  29 | |         };
     | |_________- borrow later used here
     |
     = note: consider using a `let` binding to create a longer lived value
  </code></pre>
  
  <p>I fully understand the error, which directed me to the working snippet, but I'm wondering if there's a more elegant and/or idiomatic way of writing this code. I am declaring <code>m</code> at the beginning of the function, only to prevent a temporary variable from being freed too early. Is there a way of telling the compiler that the lifetime of the return value of <code>self.index.get_pages</code> should be the whole <code>add_transition</code> function?</p>
  
  <p>Further details:</p>
  
  <ul>
  <li><code>Page</code> is a relatively big struct, so I'd rather not implement the <code>Copy</code> trait nor I'd clone it.</li>
  <li><code>page_cache</code> is of type <code>HashMap&lt;u32, Vec&lt;Page&gt;&gt;</code></li>
  <li><code>self.index.get_pages</code> is relatively slow and I'm using <code>page_cache</code> to cache results</li>
  <li>The return type of <code>self.index.get_pages</code> is <code>Result&lt;Vec&lt;Page&gt;, std::io::Error&gt;</code></li>
  </ul>