title: How to extract elements of a vector of strings in Rust?
body: <p>Let's say I have the following <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=699e45494bb6578f5ca9ee10cae4117b" rel="nofollow noreferrer">code</a>:</p>
  
  <pre><code>fn extract() -&gt; Vec&lt;String&gt; {
      let data = vec!["aaa".to_string(), "bbb".to_string(), "ccc".to_string()];
      vec![data[0], data[2]]
  }
  </code></pre>
  
  <p>In practice, I read <code>data</code> from a file.</p>
  
  <p>Obviously, this doesn't compile because I'm pulling strings out of the vector <code>data</code>, leaving the vector in an undefined state. But, conceptually, it should work, because I'm not using <code>data</code> afterwards anyway.</p>
  
  <p>I can use <code>mem::replace</code>, but this seems crazy:</p>
  
  <pre><code>fn extract() -&gt; Vec&lt;String&gt; {
      let mut data = vec!["aaa".to_string(), "bbb".to_string(), "ccc".to_string()];
      let a = mem::replace(&amp;mut data[0], "".to_string());
      let c = mem::replace(&amp;mut data[2], "".to_string());
      vec![a, c]
  }
  </code></pre>
  
  <p>How do I go about extracting specific elements from the vector without having to clone the strings?</p>