I'm trying to solve an exercise at the end of this chapter in the Rust Book.
Here is a code sample:
fn mean(v: &Vec<i32>) -> f64 {
let mut sum = 0.0;
let mut count = 0.0;
for val in v {
sum += &f64::from(val);
count += 1.0;
}
sum / count
}
fn main() {
let v = vec![1, 2, 3, 4];
println!("The mean is {}", mean(&v));
}
The error is:
error[E0277]: the trait bound `f64: std::convert::From<&i32>` is not satisfied
--> src/main.rs:6:17
|
6 | sum += &f64::from(val);
| ^^^^^^^^^ the trait `std::convert::From<&i32>` is not implemented for `f64`
|
= help: the following implementations were found:
<f64 as std::convert::From<f32>>
<f64 as std::convert::From<i16>>
<f64 as std::convert::From<i32>>
<f64 as std::convert::From<i8>>
and 3 others
= note: required by `std::convert::From::from`
I also tried using the as
keyword but it didn't help.