Skip to content

Commit 38cb0d7

Browse files
committed
Add a section about the loop_break_value feature
This commit adds a section to the newest features appendix about the loop_break_value feature, which should be stabilized soon.
1 parent 5a93856 commit 38cb0d7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

second-edition/src/appendix-07-newest-features.md

+24
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,27 @@ fn main() {
3333
println!("{:?}", portia);
3434
}
3535
```
36+
37+
38+
## Returning from loops
39+
40+
One of the uses of a `loop` is to retry an operation you know can fail, such as
41+
checking if a thread completed its job. However, you might need to pass the
42+
result of that operation to the rest of your code. If you add it to the `break`
43+
expression you use to stop the loop, it will be returned by the broken loop:
44+
45+
```rust
46+
fn main() {
47+
let mut counter = 0;
48+
49+
let result = loop {
50+
counter += 1;
51+
52+
if counter == 10 {
53+
break counter * 2;
54+
}
55+
};
56+
57+
assert_eq!(result, 20);
58+
}
59+
```

0 commit comments

Comments
 (0)