title: How can I retain vector elements with their original index?
body: <p>If I have a <code>Vec</code> I can iterate over elements using an index via <code>v.iter().enumerate()</code>, and I can remove elements via <code>v.retain()</code>. Is there a way to do both at once? </p>
  
  <p>In this case the index could no longer be used to access the element - it would be the index of the element before the loop was started.</p>
  
  <p>I can implement this myself but to be as efficient as <code>.retain()</code> I'd need to use <code>unsafe</code>, which I'd like to avoid.</p>
  
  <p>This is the result I want:</p>
  
  <pre><code>let mut v: Vec&lt;i32&gt; = vec![1, 2, 3, 4, 5, 4, 7, 8];
  
  v.iter()
      .retain_with_index(|(index, item)| (index % 2 == 0) || item == 4);
  
  assert(v == vec![1, 3, 4, 5, 4, 7]);
  </code></pre>