]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0571.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0571.md
1 A `break` statement with an argument appeared in a non-`loop` loop.
2
3 Example of erroneous code:
4
5 ```compile_fail,E0571
6 # let mut i = 1;
7 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
8 let result = while true {
9     if satisfied(i) {
10         break 2 * i; // error: `break` with value from a `while` loop
11     }
12     i += 1;
13 };
14 ```
15
16 The `break` statement can take an argument (which will be the value of the loop
17 expression if the `break` statement is executed) in `loop` loops, but not
18 `for`, `while`, or `while let` loops.
19
20 Make sure `break value;` statements only occur in `loop` loops:
21
22 ```
23 # let mut i = 1;
24 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
25 let result = loop { // This is now a "loop" loop.
26     if satisfied(i) {
27         break 2 * i; // ok!
28     }
29     i += 1;
30 };
31 ```