mirror of
https://github.com/rust-lang/book.git
synced 2026-09-15 07:49:25 -04:00
Merge pull request #699 from pietroalbini/loop_break_value
Document the loop_break_value feature
This commit is contained in:
@@ -29,7 +29,31 @@ fn main() {
|
||||
|
||||
// Using field init shorthand:
|
||||
let portia = Person { name, age };
|
||||
|
||||
|
||||
println!("{:?}", portia);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Returning from loops
|
||||
|
||||
One of the uses of a `loop` is to retry an operation you know can fail, such as
|
||||
checking if a thread completed its job. However, you might need to pass the
|
||||
result of that operation to the rest of your code. If you add it to the `break`
|
||||
expression you use to stop the loop, it will be returned by the broken loop:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut counter = 0;
|
||||
|
||||
let result = loop {
|
||||
counter += 1;
|
||||
|
||||
if counter == 10 {
|
||||
break counter * 2;
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(result, 20);
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user