title: Is there a way to drain parts of a vector based on a predicate?
body: <p>I'm trying to remove some elements from a vector, based on a predicate, and collecting the result. Here's a (not working) example with an expected result:</p>
  
  <pre><code>let mut v: Vec&lt;i32&gt; = vec![1, 2, 3, 4, 5, 6];
  
  let drained: Vec&lt;i32&gt; = v.iter().filter(|e| (*e) % 2 == 0).drain(..).collect();
  
  assert_eq!(v, vec![1, 3, 5]);
  assert_eq!(drained, vec![2, 4, 6]);
  </code></pre>
  
  <p>This results in the error</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0599]: no method named `drain` found for type `std::iter::Filter&lt;std::slice::Iter&lt;'_, i32&gt;, [closure@src/main.rs:4:45: 4:62]&gt;` in the current scope
   --&gt; src/main.rs:4:64
    |
  4 |     let drained: Vec&lt;i32&gt; = v.iter().filter(|e| (*e) % 2 == 0).drain(..).collect();
    |                                                                ^^^^^
  </code></pre>
  
  <p>There are several alternatives I looked at, none of them seem to be doing what I want:</p>
  
  <ul>
  <li><p><code>Vec::retain</code> removes the elements from the vector, but doesn't give back ownership of the removed elements.</p></li>
  <li><p><code>v.drain(..).filter(condition).collect()</code> returns the correct value for <code>drained</code> but empties the whole vector.</p></li>
  </ul>