title: Is it possible to use fold in Rust if the accumulator is neither copy type nor mutable
body: <p>I am trying to make a fold work and encountered with lifetime issues. Here is the simplified version of my code:</p>
  
  <pre><code>struct A {
    v: i32,
  }
  let v = vec![A { v: 1 }, A { v: 2 }, A { v: 3 }];
  let max = v.iter().fold(None, |res: Option&lt;A&gt;, &amp;item| { match res {
      Some(a) =&gt; if a.v &gt;= item.v { Some(a) } else { Some(item) },
      None =&gt; Some(item)
  }});
  </code></pre>
  
  <p>This results in <code>error[E0507]: cannot move out of borrowed content</code>.</p>
  
  <p>If I change the closure to <code>|res: Option&lt;A&gt;, ref item| ...</code> as adviced by the previous error, I get a type mismatch at <code>Some(item)</code> and solving that dereference gets back to the original error.</p>
  
  <p>I tried to have a reference in the option but then I get lifetime issues:</p>
  
  <pre><code>error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
   --&gt; &lt;anon&gt;:6:67
    |
  6 |   let maxV = v.iter().fold(None, |res: Option&lt;&amp;A&gt;, &amp;item| { match res {
    |                                                                   ^^^
    |
  note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 6:58...
   --&gt; &lt;anon&gt;:6:59
    |
  6 |     let maxV = v.iter().fold(None, |res: Option&lt;&amp;A&gt;, &amp;item| { match res {
    |  ___________________________________________________________^ starting here...
  7 | |       Some(a) =&gt; if a.v &gt;= item.v { Some(a) } else { Some(&amp;item) },
  8 | |       None =&gt; Some(&amp;item)
  9 | |   }});
    | |____^ ...ending here
  note: ...so that types are compatible (expected std::option::Option&lt;&amp;main::A&gt;, found std::option::Option&lt;&amp;main::A&gt;)
   --&gt; &lt;anon&gt;:6:67
    |
  6 |   let maxV = v.iter().fold(None, |res: Option&lt;&amp;A&gt;, &amp;item| { match res {
    |                                                                   ^^^
  note: but, the lifetime must be valid for the method call at 6:13...
   --&gt; &lt;anon&gt;:6:14
    |
  6 |     let maxV = v.iter().fold(None, |res: Option&lt;&amp;A&gt;, &amp;item| { match res {
    |  ______________^ starting here...
  7 | |       Some(a) =&gt; if a.v &gt;= item.v { Some(a) } else { Some(&amp;item) },
  8 | |       None =&gt; Some(&amp;item)
  9 | |   }});
    | |_____^ ...ending here
  note: ...so that argument is valid for the call
   --&gt; &lt;anon&gt;:6:28
    |
  6 |   let maxV = v.iter().fold(None, |res: Option&lt;&amp;A&gt;, &amp;item| { match res {
    |                            ^^^^
  </code></pre>
  
  <p>The first version works if I make A a copy type by adding <code>#[derive (Copy, Clone)]</code> but that is not always an option.</p>
  
  <p>I tried to search for fold examples in rust, but I either found ones where the accumulator is a copy type (i32 sums implemented with fold) or where the accumulator is a container and the fold is manipulating the content (extending a vector or similar).</p>
  
  <p>I also found <a href="https://github.com/rust-lang/rust/blob/e1cb9ba221e5cb0070ac82c6a234af11e4240680/src/libsyntax/attr.rs#L421" rel="nofollow noreferrer">an example that folds into an Option</a> but that does not match on the current value of the accumulator.</p>
  
  <p>I can do it with a for loop but would prefer the fold syntax.</p>