title: How do I partially fold a Vec in place?
body: <p>I would like to walk through a Vec and combine some elements of it. How do I do that in <strong>idiomatic</strong> Rust?</p>
  
  <h3>Example:</h3>
  
  <pre><code>#[derive(PartialEq, Debug)]
  enum Thing { A, B, AandB }
  fn combine(v: Vec&lt;Thing&gt;) -&gt; Vec&lt;Thing&gt; {
      // idiomatic code here
  }
  
  fn main() {
       let v = vec![Thing::A, Thing::B];
       assert_eq!(vec![Thing::AandB], combine(v));
  }
  </code></pre>
  
  <h3>How I would do it:</h3>
  
  <p>Traverse the Vec with Iterator::scan and replace all occurrences of Thing::B with Thing::AandB if Thing::A was the element before. Then I would traverse it again and remove all Thing::As before Thing::AandB.</p>
  
  <p>This seems super complicated and inelegant.</p>