The for loop, or for-in loop, or iterator loop is used when we want to go over the elements of an iterator. This can be a fixed set of elements, but there can be also iterators where we don't know the number of elements up-front, and even iteartors that, at least theoretically, could go on indefinitely.
In this example we see two simple cases.
In the first case we iterate over the elements of a range.
In the second example we iterate over the elements of a vector.
The code
fn main() {
for i in 1..=3 {
println!("{i}");
}
let animals = vec!["cat", "dog", "mouse"];
for animal in animals {
println!("{animal}");
}
}
The output
$ cargo -q run
1
2
3
cat
dog
mouse