2015-04-10 11:35:19 -04:00
|
|
|
|
% for Loops
|
2015-04-07 22:16:02 -04:00
|
|
|
|
|
2015-04-10 12:21:14 -04:00
|
|
|
|
The `for` loop is used to loop a particular number of times. Rust’s `for` loops
|
|
|
|
|
work a bit differently than in other systems languages, however. Rust’s `for`
|
|
|
|
|
loop doesn’t look like this “C-style” `for` loop:
|
2015-04-07 22:16:02 -04:00
|
|
|
|
|
2015-04-10 12:21:14 -04:00
|
|
|
|
```c
|
2015-04-07 22:16:02 -04:00
|
|
|
|
for (x = 0; x < 10; x++) {
|
|
|
|
|
printf( "%d\n", x );
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Instead, it looks like this:
|
|
|
|
|
|
2015-04-10 12:21:14 -04:00
|
|
|
|
```rust
|
2015-04-07 22:16:02 -04:00
|
|
|
|
for x in 0..10 {
|
|
|
|
|
println!("{}", x); // x: i32
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In slightly more abstract terms,
|
|
|
|
|
|
2015-04-10 12:21:14 -04:00
|
|
|
|
```ignore
|
2015-04-07 22:16:02 -04:00
|
|
|
|
for var in expression {
|
|
|
|
|
code
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-10 12:21:14 -04:00
|
|
|
|
The expression is an [iterator][iterator]. The iterator gives back a series of
|
|
|
|
|
elements. Each element is one iteration of the loop. That value is then bound
|
|
|
|
|
to the name `var`, which is valid for the loop body. Once the body is over, the
|
|
|
|
|
next value is fetched from the iterator, and we loop another time. When there
|
|
|
|
|
are no more values, the `for` loop is over.
|
|
|
|
|
|
|
|
|
|
[iterator]: iterators.html
|
2015-04-07 22:16:02 -04:00
|
|
|
|
|
|
|
|
|
In our example, `0..10` is an expression that takes a start and an end position,
|
|
|
|
|
and gives an iterator over those values. The upper bound is exclusive, though,
|
|
|
|
|
so our loop will print `0` through `9`, not `10`.
|
|
|
|
|
|
2015-04-10 12:21:14 -04:00
|
|
|
|
Rust does not have the “C-style” `for` loop on purpose. Manually controlling
|
2015-04-07 22:16:02 -04:00
|
|
|
|
each element of the loop is complicated and error prone, even for experienced C
|
|
|
|
|
developers.
|