title: How do I borrow a reference to what is inside an Option<T>?
body: <p>How do I pull a reference out of an <code>Option</code> and pass it back with the specific lifespan of the caller? </p>
  
  <p>Specifically, I want to borrow a reference to a <code>Box&lt;Foo&gt;</code> from a <code>Bar</code> that has an <code>Option&lt;Box&lt;Foo&gt;&gt;</code> in it. I thought I would be able to do:</p>
  
  <pre><code>impl Bar {
      fn borrow(&amp;mut self) -&gt; Result&lt;&amp;Box&lt;Foo&gt;, BarErr&gt; {
          match self.data {
              Some(e) =&gt; Ok(&amp;e),
              None =&gt; Err(BarErr::Nope),
          }
      }
  }
  </code></pre>
  
  <p>...but that results in:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: `e` does not live long enough
    --&gt; src/main.rs:17:28
     |
  17 |             Some(e) =&gt; Ok(&amp;e),
     |                            ^ does not live long enough
  18 |             None =&gt; Err(BarErr::Nope),
  19 |         }
     |         - borrowed value only lives until here
     |
  note: borrowed value must be valid for the anonymous lifetime #1 defined on the body at 15:54...
    --&gt; src/main.rs:15:55
     |
  15 |       fn borrow(&amp;mut self) -&gt; Result&lt;&amp;Box&lt;Foo&gt;, BarErr&gt; {
     |  _______________________________________________________^ starting here...
  16 | |         match self.data {
  17 | |             Some(e) =&gt; Ok(&amp;e),
  18 | |             None =&gt; Err(BarErr::Nope),
  19 | |         }
  20 | |     }
     | |_____^ ...ending here
  
  error[E0507]: cannot move out of borrowed content
    --&gt; src/main.rs:16:15
     |
  16 |         match self.data {
     |               ^^^^ cannot move out of borrowed content
  17 |             Some(e) =&gt; Ok(&amp;e),
     |                  - hint: to prevent move, use `ref e` or `ref mut e`
  </code></pre>
  
  <p>Hm, ok. Maybe not. It looks vaguely like what I want to do is related to <a href="https://doc.rust-lang.org/std/option/enum.Option.html#method.as_ref" rel="noreferrer"><code>Option::as_ref</code></a>, like maybe I could do:</p>
  
  <pre><code>impl Bar {
      fn borrow(&amp;mut self) -&gt; Result&lt;&amp;Box&lt;Foo&gt;, BarErr&gt; {
          match self.data {
              Some(e) =&gt; Ok(self.data.as_ref()),
              None =&gt; Err(BarErr::Nope),
          }
      }
  }
  </code></pre>
  
  <p>...but, that doesn't work either. </p>
  
  <p>Full code I'm having trouble with:</p>
  
  <pre><code>#[derive(Debug)]
  struct Foo;
  
  #[derive(Debug)]
  struct Bar {
      data: Option&lt;Box&lt;Foo&gt;&gt;,
  }
  
  #[derive(Debug)]
  enum BarErr {
      Nope,
  }
  
  impl Bar {
      fn borrow(&amp;mut self) -&gt; Result&lt;&amp;Box&lt;Foo&gt;, BarErr&gt; {
          match self.data {
              Some(e) =&gt; Ok(&amp;e),
              None =&gt; Err(BarErr::Nope),
          }
      }
  }
  
  #[test]
  fn test_create_indirect() {
      let mut x = Bar { data: Some(Box::new(Foo)) };
      let mut x2 = Bar { data: None };
      {
          let y = x.borrow();
          println!("{:?}", y);
      }
      {
          let z = x2.borrow();
          println!("{:?}", z);
      }
  }
  </code></pre>
  
  <p>I'm reasonably sure what I've trying to do is valid here.</p>