If I want to use the iterator manually, it must be changed:
let test = vec![1,2,3]; let mut test_mut = test.iter(); while let Some(val) = test_mut.next() { println!("{:?}",val); }
But I can use it with a for loop with pleasure, even if it is immutable.
let test_imm = test.iter(); for val in test_imm { println!("{:?}",val); }
I think this roughly works because test_imm is moved to the for loop block, so test_imm can no longer be used by the outer block and (from the point of view of the outer block) is unchanged until the for loop, and then it is unavailable, so everything is fine.
It is right? Is there any other explanation?
Ed: Yeah, this is more or less explained here .
source share