I'm new to rust and just played around with some code:
fn array_leaders(arr: &[i32]) -> Vec<i32> {
let mut res = Vec::new();
for (i, left) in arr.iter().enumerate() {
let right = arr.iter().skip(i+1).sum();
if left > right { res.push(*left) };
}
res
}
which gives the following error:
error[E0277]: the trait bound `&i32: Sum<&i32>` is not satisfied
--> src/lib.rs:5:42
|
5 | let right = arr.iter().skip(i+1).sum();
| ^^^ the trait `Sum<&i32>` is not implemented for `&i32`
Now, I've figured out that i could just add an & to the right variable in the if statement:
if left > &right { res.push(*left) };
which solves the problem. I would like to understand why though