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.
// 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(&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!("mean is {}", mean_ans);
}
fn mean(integers: &Vec<i32>) -> i32 {
let length = integers.len() as i32;
let sum = integers.iter().reduce(|&a, &b| &(a + b));
match sum {
Some(v) => v / length,
None => 0,
}
}
I'm receiving a complier error when I run cargo run and rust-analyzer also highlights the &(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.
error[E0515]: cannot return reference to temporary value
--> src\main.rs:13:47
|
13 | let sum = integers.iter().reduce(|&a, &b| &(a + b));
| ^-------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
error: aborting due to previous error
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 &i32? When I remove & from &(a + b), I get the following complier error "expected &i32
, found i32
help: consider borrowing here: &(a + b)
".
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).
I am using rust version 1.52.1 with rustup version 1.24.1 on windows 10 in VSCode.