title: Rust error[E0373]: closure may outlive the current function, but it borrows `iteration_index`
body: <p>I have two vectors of <a href="https://docs.rs/itertools/0.8.0/itertools/enum.MinMaxResult.html" rel="nofollow noreferrer"><code>itertools::MinMaxResult</code></a>. I need to iterate through the first vector, and for each element, iterate through the second vector, checking if the minimum of the first vector is equal to the maximum of any elements of the second vector, and vice versa. Here is an MCVE of what I tried:</p>
  
  <pre class="lang-rust prettyprint-override"><code>use itertools::MinMaxResult; // itertools = "0.8.0"
  use itertools::MinMaxResult::*;
  
  pub fn mcve() -&gt; Vec&lt;(usize, usize)&gt; {
  
      // dummy variables to make the MCVE compile
      let num_rows = 0;
      let num_cols = 0;
      let row_minmax: Vec&lt;MinMaxResult&lt;&amp;u64&gt;&gt; = vec![];
      let col_minmax: Vec&lt;MinMaxResult&lt;&amp;u64&gt;&gt; = vec![];
  
      // Problematic code:
      (0..num_rows)
              .flat_map(|row_index| {
                  (0_usize..num_cols).filter_map(|col_index| {
                      match (row_minmax[row_index], col_minmax[col_index]) {
                          (MinMax(a, _b), MinMax(_c, d)) if a == d =&gt;
                              Some((row_index, col_index)),
                          (MinMax(_a, b), MinMax(c, _d)) if b == c =&gt;
                              Some((row_index, col_index)),
                          _ =&gt; None,
                      }
                  })
              })
              .collect::&lt;Vec&lt;(usize, usize)&gt;&gt;()
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c67c37b5172388c1f8830ed871b4397d" rel="nofollow noreferrer">Link to the Playground with full code</a></p>
  
  <p>I'm getting the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0373]: closure may outlive the current function, but it borrows `row_index`,
  which is owned by the current function
    --&gt; src/main.rs:15:48
     |
  15 |                 (0_usize..num_cols).filter_map(|col_index| {
     |                                                ^^^^^^^^^^^ may outlive 
                                                      borrowed value `row_index`
  16 |                     match (row_minmax[row_index], col_minmax[col_index]) {
     |                                       --------- `row_index` is borrowed here
     |
  note: closure is returned here
    --&gt; src/main.rs:15:17
     |
  15 | /                 (0_usize..num_cols).filter_map(|col_index| {
  16 | |                     match (row_minmax[row_index], col_minmax[col_index]) {
  17 | |                         (MinMax(a, _b), MinMax(_c, d)) if a == d =&gt; 
                                     Some((row_index, col_index)),
  18 | |                         (MinMax(_a, b), MinMax(c, _d)) if b == c =&gt; 
                                     Some((row_index, col_index)),
  19 | |                         _ =&gt; None,
  20 | |                     }
  21 | |                 })
     | |__________________^
  help: to force the closure to take ownership of `row_index` 
  (and any other referenced variables), use the `move` keyword
     |
  15 |                 (0_usize..num_cols).filter_map(move |col_index| {
     |                                                ^^^^^^^^^^^^^^^^
  
  </code></pre>
  
  <p>If I add a <code>move</code>, as the compiler suggests, I get <em>twice as many</em> errors, so that doesn't help. How do I get rid of this error?</p>