for
The for loop iterates over ranges of values or the items in a collection:
1. ```
fn main() {
```
2. ```
for x in 1..5 {
```
3. ```
println!("x: {x}");
```
4. ```
}
```
6. ```
for elem in [1, 2, 3, 4, 5] {
```
7. ```
println!("elem: {elem}");
```
8. ```
}
```
9. ```
}
```
- Under the hood
forloops use a concept called “iterators” to handle iterating over different kinds of ranges/collections. Iterators will be discussed in more detail later. - Note that the first
forloop only iterates to4. Show the1..=5syntax for an inclusive range.
