-
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.
Merge pull request #867 from pietroalbini/loop_break_value
Add an example about returning values with break
- Loading branch information
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