title: Rust's Reduce Method on Vec Reference
body: <p>I am trying to reduce a reference of a Vec to its sum so I can calculate its mean. I am running into complier issues though and I am not following how things are not being borrowed/referenced correctly.</p>
  <pre><code>// Given a list of integers, use a vector and return the mean (the average value), median (when sorted, the value in the middle position), and mode (the value that occurs most often; a hash map will be helpful here) of the list.
  fn main() {
      let list_of_integers = vec![200, -6_000, 3, 0, 23, 99, -1];
  
      let mean_ans = mean(&amp;list_of_integers);
      // Other code that will also use list_of_integers hence why I want to reference list_of_integers so it doesn't get removed from memory
  
      println!(&quot;mean is {}&quot;, mean_ans);
  }
  
  fn mean(integers: &amp;Vec&lt;i32&gt;) -&gt; i32 {
      let length = integers.len() as i32;
      let sum = integers.iter().reduce(|&amp;a, &amp;b| &amp;(a + b));
  
      match sum {
          Some(v) =&gt; v / length,
          None =&gt; 0,
      }
  }
  </code></pre>
  <p>I'm receiving a complier error when I run cargo run and rust-analyzer also highlights the &amp;(a + b) of the reduce method as wrong too. The text of the error is below but I've also attached the image to clearly show what it is referencing too.</p>
  <pre class="lang-none prettyprint-override"><code>error[E0515]: cannot return reference to temporary value
    --&gt; src\main.rs:13:47
     |
  13 |     let sum = integers.iter().reduce(|&amp;a, &amp;b| &amp;(a + b));
     |                                               ^-------
     |                                               ||
     |                                               |temporary value created here
     |                                               returns a reference to data owned by the current function
  
  error: aborting due to previous error
  </code></pre>
  <p><a href="https://i.stack.imgur.com/IuaVl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IuaVl.png" alt="Error saying temporary value created here but returns a reference to data owner by the current function" /></a></p>
  <p>I am unsure what is wrong here as I understand .iter() returns an Iter reference to the Vec so shouldn't its reduced values of a and b already be &amp;i32? When I remove &amp; from &amp;(a + b), I get the following complier error &quot;expected <code>&amp;i32</code>, found <code>i32</code> help: consider borrowing here: <code>&amp;(a + b)</code>&quot;.</p>
  <p>Note I am just learning Rust and I am less than halfway through its tutorial so please feel free to explain the solution as if I'm a newbie (since I am).</p>
  <p>I am using rust version 1.52.1 with rustup version 1.24.1 on windows 10 in VSCode.</p>