Edit: The question is about, mainly, why the iterator did not bind elem to f64 in the first loop, or did it?
Edit #2: Type inference is clear. The question is about associated type of Iterator trait.
I thought elem would bind to f64, because it's the first, but no. Why iterators behave this way, what feature is it? The first elem is reference, the second was bound through dereferencing, I thought that's the difference, but what exactly happend and what about the cloned() version?
fn main() {
let a = [1., 2., 3., 4., 5.];
let epsilon = 1e-8;
for elem in a.iter() {
let diff: f64 = elem - 6.;
assert!(diff.abs() < epsilon);
}
let epsilon = 1e-8;
for elem in a.iter() {
let diff: f32 = *elem - 6.;
assert!(diff.abs() < epsilon);
}
let epsilon = 1e-8;
for elem in a.iter().cloned() {
let diff: f32 = elem - 6.;
assert!(diff.abs() < epsilon);
}
}