-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flow_control/loop: add example about returning values with break
This commit adds an example for the loop_break_value feature.
- Loading branch information
1 parent
d0b51d4
commit 4711ba7
Showing
3 changed files
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
One of the uses of a `loop` is to retry an operation until it succeded. If the | ||
operation returns a value though, you might need to pass it to the rest of the | ||
code: put it after the `break`, and it will be returned by the `loop` | ||
expression. | ||
|
||
{return.play} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fn main() { | ||
let mut counter = 0; | ||
|
||
let result = loop { | ||
counter += 1; | ||
|
||
if counter == 10 { | ||
break counter * 2; | ||
} | ||
}; | ||
|
||
assert_eq!(result, 20); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters