Let me give the code first
fn dump(iter: &mut dyn Iterator<Item=String>) {
for (i, v) in iter.enumerate() {
println!("{} {}", i, v);
}
for v in iter {
println!("{}", v);
}
}
#[test]
fn test_trait() {
let v = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let mut iter = v.into_iter();
dump(&mut iter);
}
Here is the output when I run this test
running 1 test
0 a
1 b
2 c
test test_trait ... ok
Why the iter not moved when calling enumerate?
The enumerate is accepting self as its first argument, so I think it should move the iter, but it doesn't! The second for-loop can still run, without any compilation error.