]> git.lizzy.rs Git - rust.git/blob - src/doc/trpl/looping.md
Rollup merge of #21357 - kimroen:patch-1, r=sanxiyn
[rust.git] / src / doc / trpl / looping.md
1 % Looping
2
3 Looping is the last basic construct that we haven't learned yet in Rust. Rust has
4 two main looping constructs: `for` and `while`.
5
6 ## `for`
7
8 The `for` loop is used to loop a particular number of times. Rust's `for` loops
9 work a bit differently than in other systems languages, however. Rust's `for`
10 loop doesn't look like this "C-style" `for` loop:
11
12 ```{c}
13 for (x = 0; x < 10; x++) {
14     printf( "%d\n", x );
15 }
16 ```
17
18 Instead, it looks like this:
19
20 ```{rust}
21 for x in 0..10 {
22     println!("{}", x); // x: i32
23 }
24 ```
25
26 In slightly more abstract terms,
27
28 ```{ignore}
29 for var in expression {
30     code
31 }
32 ```
33
34 The expression is an iterator, which we will discuss in more depth later in the
35 guide. The iterator gives back a series of elements. Each element is one
36 iteration of the loop. That value is then bound to the name `var`, which is
37 valid for the loop body. Once the body is over, the next value is fetched from
38 the iterator, and we loop another time. When there are no more values, the
39 `for` loop is over.
40
41 In our example, `0..10` is an expression that takes a start and an end position,
42 and gives an iterator over those values. The upper bound is exclusive, though,
43 so our loop will print `0` through `9`, not `10`.
44
45 Rust does not have the "C-style" `for` loop on purpose. Manually controlling
46 each element of the loop is complicated and error prone, even for experienced C
47 developers.
48
49 We'll talk more about `for` when we cover *iterators*, later in the Guide.
50
51 ## `while`
52
53 The other kind of looping construct in Rust is the `while` loop. It looks like
54 this:
55
56 ```{rust}
57 let mut x = 5; // mut x: u32
58 let mut done = false; // mut done: bool
59
60 while !done {
61     x += x - 3;
62     println!("{}", x);
63     if x % 5 == 0 { done = true; }
64 }
65 ```
66
67 `while` loops are the correct choice when you're not sure how many times
68 you need to loop.
69
70 If you need an infinite loop, you may be tempted to write this:
71
72 ```{rust,ignore}
73 while true {
74 ```
75
76 However, Rust has a dedicated keyword, `loop`, to handle this case:
77
78 ```{rust,ignore}
79 loop {
80 ```
81
82 Rust's control-flow analysis treats this construct differently than a
83 `while true`, since we know that it will always loop. The details of what
84 that _means_ aren't super important to understand at this stage, but in
85 general, the more information we can give to the compiler, the better it
86 can do with safety and code generation, so you should always prefer
87 `loop` when you plan to loop infinitely.
88
89 ## Ending iteration early
90
91 Let's take a look at that `while` loop we had earlier:
92
93 ```{rust}
94 let mut x = 5;
95 let mut done = false;
96
97 while !done {
98     x += x - 3;
99     println!("{}", x);
100     if x % 5 == 0 { done = true; }
101 }
102 ```
103
104 We had to keep a dedicated `mut` boolean variable binding, `done`, to know
105 when we should exit out of the loop. Rust has two keywords to help us with
106 modifying iteration: `break` and `continue`.
107
108 In this case, we can write the loop in a better way with `break`:
109
110 ```{rust}
111 let mut x = 5;
112
113 loop {
114     x += x - 3;
115     println!("{}", x);
116     if x % 5 == 0 { break; }
117 }
118 ```
119
120 We now loop forever with `loop` and use `break` to break out early.
121
122 `continue` is similar, but instead of ending the loop, goes to the next
123 iteration. This will only print the odd numbers:
124
125 ```{rust}
126 for x in 0..10 {
127     if x % 2 == 0 { continue; }
128
129     println!("{}", x);
130 }
131 ```
132
133 Both `continue` and `break` are valid in both kinds of loops.