title: How do I convert a list of Option<T> to a list of T when T cannot be copied?
body: <p>How do I take a <code>Vec&lt;Option&lt;T&gt;&gt;</code>, where <code>T</code> cannot be copied, and unwrap all the <code>Some</code> values?</p>
  
  <p>I run into an error in the <code>map</code> step. I'm happy to move ownership of the original list and "throw away" the <code>None</code>s.</p>
  
  <pre><code>#[derive(Debug)]
  struct Uncopyable {
      val: u64,
  }
  
  fn main() {
      let num_opts: Vec&lt;Option&lt;Uncopyable&gt;&gt; = vec![
          Some(Uncopyable { val: 1 }),
          Some(Uncopyable { val: 2 }),
          None,
          Some(Uncopyable { val: 4 }),
      ];
  
      let nums: Vec&lt;Uncopyable&gt; = num_opts
          .iter()
          .filter(|x| x.is_some())
          .map(|&amp;x| x.unwrap())
          .collect();
      println!("nums: {:?}", nums);
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=61e0b944a4930451d4f6501f3ff966ce" rel="noreferrer">Playground</a></p>
  
  <p>Which gives the error</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0507]: cannot move out of borrowed content
    --&gt; src/main.rs:17:15
     |
  17 |         .map(|&amp;x| x.unwrap())
     |               ^-
     |               ||
     |               |hint: to prevent move, use `ref x` or `ref mut x`
     |               cannot move out of borrowed content
  </code></pre>