2015-05-08 15:15:14 -05:00
|
|
|
|
% while Loops
|
2015-04-07 21:16:02 -05:00
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
Rust also has a `while` loop. It looks like this:
|
2015-04-07 21:16:02 -05:00
|
|
|
|
|
|
|
|
|
```{rust}
|
2015-05-14 09:38:22 -05:00
|
|
|
|
let mut x = 5; // mut x: i32
|
2015-04-07 21:16:02 -05:00
|
|
|
|
let mut done = false; // mut done: bool
|
|
|
|
|
|
|
|
|
|
while !done {
|
|
|
|
|
x += x - 3;
|
2015-04-10 11:25:40 -05:00
|
|
|
|
|
2015-04-07 21:16:02 -05:00
|
|
|
|
println!("{}", x);
|
2015-04-10 11:25:40 -05:00
|
|
|
|
|
|
|
|
|
if x % 5 == 0 {
|
|
|
|
|
done = true;
|
|
|
|
|
}
|
2015-04-07 21:16:02 -05:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
`while` loops are the correct choice when you’re not sure how many times
|
2015-04-07 21:16:02 -05:00
|
|
|
|
you need to loop.
|
|
|
|
|
|
|
|
|
|
If you need an infinite loop, you may be tempted to write this:
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
```rust,ignore
|
2015-04-07 21:16:02 -05:00
|
|
|
|
while true {
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
However, Rust has a dedicated keyword, `loop`, to handle this case:
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
```rust,ignore
|
2015-04-07 21:16:02 -05:00
|
|
|
|
loop {
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
Rust’s control-flow analysis treats this construct differently than a `while
|
|
|
|
|
true`, since we know that it will always loop. In general, the more information
|
|
|
|
|
we can give to the compiler, the better it can do with safety and code
|
|
|
|
|
generation, so you should always prefer `loop` when you plan to loop
|
|
|
|
|
infinitely.
|
2015-04-07 21:16:02 -05:00
|
|
|
|
|
|
|
|
|
## Ending iteration early
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
Let’s take a look at that `while` loop we had earlier:
|
2015-04-07 21:16:02 -05:00
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
```rust
|
2015-04-07 21:16:02 -05:00
|
|
|
|
let mut x = 5;
|
|
|
|
|
let mut done = false;
|
|
|
|
|
|
|
|
|
|
while !done {
|
|
|
|
|
x += x - 3;
|
2015-04-10 11:25:40 -05:00
|
|
|
|
|
2015-04-07 21:16:02 -05:00
|
|
|
|
println!("{}", x);
|
2015-04-10 11:25:40 -05:00
|
|
|
|
|
|
|
|
|
if x % 5 == 0 {
|
|
|
|
|
done = true;
|
|
|
|
|
}
|
2015-04-07 21:16:02 -05:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
We had to keep a dedicated `mut` boolean variable binding, `done`, to know
|
|
|
|
|
when we should exit out of the loop. Rust has two keywords to help us with
|
|
|
|
|
modifying iteration: `break` and `continue`.
|
|
|
|
|
|
|
|
|
|
In this case, we can write the loop in a better way with `break`:
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
```rust
|
2015-04-07 21:16:02 -05:00
|
|
|
|
let mut x = 5;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
x += x - 3;
|
2015-04-10 11:25:40 -05:00
|
|
|
|
|
2015-04-07 21:16:02 -05:00
|
|
|
|
println!("{}", x);
|
2015-04-10 11:25:40 -05:00
|
|
|
|
|
2015-04-07 21:16:02 -05:00
|
|
|
|
if x % 5 == 0 { break; }
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
We now loop forever with `loop` and use `break` to break out early.
|
|
|
|
|
|
|
|
|
|
`continue` is similar, but instead of ending the loop, goes to the next
|
|
|
|
|
iteration. This will only print the odd numbers:
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
```rust
|
2015-04-07 21:16:02 -05:00
|
|
|
|
for x in 0..10 {
|
|
|
|
|
if x % 2 == 0 { continue; }
|
|
|
|
|
|
|
|
|
|
println!("{}", x);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-10 11:25:40 -05:00
|
|
|
|
Both `continue` and `break` are valid in both `while` loops and [`for` loops][for].
|
|
|
|
|
|
|
|
|
|
[for]: for-loops.html
|