Having to Borrow a "0" inside an if-statment
Im currently trying to wrap my Head around this compilation error that im getting:
My code:
fn plusMinus(arr: &[i32]) {
let mut res = (0,0,0);
for i in arr.iter(){
if i>0 {
res.0 +=1;
}else if i<0{
res.1 +=1;
}else{
res.2 +=1;
}
}
println!("{}", res.0 as f64/arr.len() as f64);
println!("{}", res.1 as f64/arr.len() as f64);
println!("{}", res.2 as f64/arr.len() as f64);
}
The error (and the fix that works):
error[E0308]: mismatched types
--> main.rs:12:14
|
12 | if i>0 {
| ^
| |
| expected `&i32`, found integer
| help: consider borrowing here: `&0`
My question:
Why do i have to borrow the "0" here? Isnt it just a value to compare against and not a variable that has to be owned by something? Is it because my function has no return?