What is the difference between .iter() and .into_iter()?
iter yields &T
into_iter may yield any of T, &T or &mut T based on context.
1
2
3
4
5
6
7
8
9
let cars = vec!["Skoda", "Ferrari", "Ford"];
for car in cars.iter() {
println!("{car}");
}
for car in cars.into_iter() {
println!("{car}");
}
this works but …
This does not. This results in compile error because cars are moved due to into_iter call.
1
2
3
4
5
6
7
8
9
let cars = vec!["Skoda", "Ferrari", "Ford"];
for car in cars.into_iter() {
println!("{car}");
}
for car in cars.iter() {
println!("{car}");
}