1
1
% while loops
2
2
3
- The other kind of looping construct in Rust is the ` while ` loop. It looks like
4
- this:
3
+ Rust also has a ` while ` loop. It looks like this:
5
4
6
5
``` {rust}
7
6
let mut x = 5; // mut x: u32
8
7
let mut done = false; // mut done: bool
9
8
10
9
while !done {
11
10
x += x - 3;
11
+
12
12
println!("{}", x);
13
- if x % 5 == 0 { done = true; }
13
+
14
+ if x % 5 == 0 {
15
+ done = true;
16
+ }
14
17
}
15
18
```
16
19
17
- ` while ` loops are the correct choice when you' re not sure how many times
20
+ ` while ` loops are the correct choice when you’ re not sure how many times
18
21
you need to loop.
19
22
20
23
If you need an infinite loop, you may be tempted to write this:
21
24
22
- ``` { rust,ignore}
25
+ ``` rust,ignore
23
26
while true {
24
27
```
25
28
26
29
However, Rust has a dedicated keyword, ` loop ` , to handle this case:
27
30
28
- ``` { rust,ignore}
31
+ ``` rust,ignore
29
32
loop {
30
33
```
31
34
32
- Rust's control-flow analysis treats this construct differently than a
33
- ` while true ` , since we know that it will always loop. The details of what
34
- that _ means_ aren't super important to understand at this stage, but in
35
- general, the more information we can give to the compiler, the better it
36
- can do with safety and code generation, so you should always prefer
37
- ` loop ` when you plan to loop infinitely.
35
+ Rust’s control-flow analysis treats this construct differently than a `while
36
+ true`, since we know that it will always loop. In general, the more information
37
+ we can give to the compiler, the better it can do with safety and code
38
+ generation, so you should always prefer ` loop ` when you plan to loop
39
+ infinitely.
38
40
39
41
## Ending iteration early
40
42
41
- Let' s take a look at that ` while ` loop we had earlier:
43
+ Let’ s take a look at that ` while ` loop we had earlier:
42
44
43
- ``` { rust}
45
+ ``` rust
44
46
let mut x = 5 ;
45
47
let mut done = false ;
46
48
47
49
while ! done {
48
50
x += x - 3 ;
51
+
49
52
println! (" {}" , x );
50
- if x % 5 == 0 { done = true; }
53
+
54
+ if x % 5 == 0 {
55
+ done = true ;
56
+ }
51
57
}
52
58
```
53
59
@@ -57,12 +63,14 @@ modifying iteration: `break` and `continue`.
57
63
58
64
In this case, we can write the loop in a better way with ` break ` :
59
65
60
- ``` { rust}
66
+ ``` rust
61
67
let mut x = 5 ;
62
68
63
69
loop {
64
70
x += x - 3 ;
71
+
65
72
println! (" {}" , x );
73
+
66
74
if x % 5 == 0 { break ; }
67
75
}
68
76
```
@@ -72,12 +80,14 @@ We now loop forever with `loop` and use `break` to break out early.
72
80
` continue ` is similar, but instead of ending the loop, goes to the next
73
81
iteration. This will only print the odd numbers:
74
82
75
- ``` { rust}
83
+ ``` rust
76
84
for x in 0 .. 10 {
77
85
if x % 2 == 0 { continue ; }
78
86
79
87
println! (" {}" , x );
80
88
}
81
89
```
82
90
83
- Both ` continue ` and ` break ` are valid in both kinds of loops.
91
+ Both ` continue ` and ` break ` are valid in both ` while ` loops and [ ` for ` loops] [ for ] .
92
+
93
+ [ for ] : for-loops.html
0 commit comments