title: How can I return None from a function that borrows from it's argument, or avoid needing to?
body: <p>I have a slice of Option, and given a value, if that value is a valid index in the slice I want to use the value at that index, otherwise use <code>None</code>.</p>
  
  <p>Now, values need to be able to be reused, so I think I want to borrow from my slice, not move... Note that <code>Foo</code> does not implement the <code>Copy</code> trait and I'd prefer to keep it that way.</p>
  
  <p>I need to do this often, so a function returning <code>&amp;Option&lt;Foo&gt;</code> seemed appropriate, with the addition of a lifetime specifier since obviously the return value shouldn't outlive the slice it is borrowed from. That leads me to:</p>
  
  <pre class="lang-rust prettyprint-override"><code>fn get_or_none&lt;'a&gt;(data: &amp;'a [Option&lt;Foo&gt;], bar: u8) -&gt; &amp;'a Option&lt;Foo&gt; {
    match bar as usize {
      idx if idx &lt; data.len() =&gt; &amp;data[idx],
      _ =&gt; &amp;None // obviously can't work
    }
  }
  </code></pre>
  
  <p>This is clearly wrong. I can cheat, for now, because I know for this particular application the first value in the slice will always be None (it's a property of my data, so to speak), but that's just avoiding the problem.</p>
  
  <p>What should I do instead?</p>