It seems that a for loop takes ownership of the thing it's iterating over. For example, the following code won't compile:
let r = 1..4;
for i in r {
println!("{}", i);
}
for i in r {
println!("{}", i);
}
The second for loop causes an error use of moved value: r.
But I don't really understand why it has to be this way. This code doesn't modify the range itself or any of the integers it yields to the for loop. In practice I can work around it by using r.clone() but this seems ugly. Is there some way to do this with immutable references instead?
Ah, that also explains why I have call .iter() on vectors.
But this still seems like an odd design to me. In many other languages, a for loop expects an iterable object, which can produce an iterator for the loop. So the argument to the for loop is not itself consumed during iteration.
Thanks, that clears things up. I didn't realize that for i in vec was allowed since I've mostly been writing code with map and filter, where I do need vec.iter().