title: How to avoid mutable and immutable borrow with IntoParallelIterator bound
body: <p>I have a function that operates on a <code>Vec&lt;T&gt;</code> which purpose is to extend the vector with new items generated using reference to existing items. I'm trying to run the generation of new data in parallel using <code>rayon</code>.</p>
  <p>This is a minimal example:</p>
  <pre class="lang-rust prettyprint-override"><code>use itertools::Itertools;
  use rayon::prelude::*;
  
  fn main() {
      let mut foo = Foo {
          data: (0..1000).into_iter().collect(),
      };
      foo.run();
  }
  
  struct Foo&lt;T&gt; {
      data: Vec&lt;T&gt;,
  }
  
  type Pair&lt;'a, T&gt; = (&amp;'a T, &amp;'a T);
  
  impl&lt;'a, T: Clone + 'a&gt; Foo&lt;T&gt;
  where
      Vec&lt;Pair&lt;'a, T&gt;&gt;: IntoParallelIterator&lt;Item = Pair&lt;'a, T&gt;&gt;,
      [T; 2]: IntoParallelIterator,
      Vec&lt;T&gt;: FromParallelIterator&lt;&lt;[T; 2] as IntoParallelIterator&gt;::Item&gt;,
  {
      fn run(&amp;'a mut self) {
          let combinations: Vec&lt;Pair&lt;'a, T&gt;&gt; = self
              .data
              .iter()
              .combinations(2)
              .map(|x| (x[0], x[1]))
              .collect();
  
          let mut new_combinations: Vec&lt;T&gt; = combinations
              .into_par_iter()
              .flat_map(|(a, b)| bar(a, b))
              .collect();
  
          self.data.append(&amp;mut new_combinations);
      }
  }
  
  
  fn bar&lt;T: Clone&gt;(a: &amp;T, b: &amp;T) -&gt; [T; 2] {
      [a.clone(), b.clone()]
  }
  </code></pre>
  <p>You can find a link to Playground <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=91f5b11f9469a2356853d4f84e7ee58c" rel="nofollow noreferrer">here</a>.</p>
  <p>Building the above example raises this error:</p>
  <pre><code>error[E0502]: cannot borrow `self.data` as mutable because it is also borrowed as immutable
    --&gt; src/main.rs:36:9
     |
  17 |   impl&lt;'a, T: Clone + 'a&gt; Foo&lt;T&gt;
     |        -- lifetime `'a` defined here
  ...
  24 |           let combinations: Vec&lt;Pair&lt;'a, T&gt;&gt; = self
     |  ___________________________----------------___-
     | |                           |
     | |                           type annotation requires that `self.data` is borrowed for `'a`
  25 | |             .data
  26 | |             .iter()
     | |___________________- immutable borrow occurs here
  ...
  36 |           self.data.append(&amp;mut new_combinations);
     |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
  </code></pre>
  <p>As far as I understand since I am collecting into a <code>let new_combinations: Vec&lt;T&gt;</code> there should be no immutable references to <code>self.data</code> and I should be able in theory to borrow it mutably to append the new combinations. However, it seems that <code>self.data</code> is borrowed for <code>'a</code> which extends beyond the scope of this method. I cannot find a way to avoid specifying the lifetime <code>fn run(&amp;'a mut self)</code> since I need to specify that the lifetimes of the references to the items of <code>self.data</code> cannot outlive <code>self</code> when creating the combinations.</p>
  <p>Is there a way to allow this method to operate as expected, that is: 1) select a list of references to the items in <code>self.data</code>, 2) apply a function that creates new items <code>T</code> in parallel and finally 3) update <code>self.data</code> with the new items.</p>
  <p>Note that as a workaround one could return the <code>new_combinations</code> from the method and append them to <code>self.data</code> separately.</p>
  <p>Would be great if all of this would be possible by avoiding as many <code>collect()</code> as possible while operating directly with iterators only.</p>