Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding docs for loops and loop labels.
- Loading branch information
1 parent
a5c12f4
commit b36551b
Showing
2 changed files
with
41 additions
and
0 deletions.
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
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,40 @@ | ||
% infinite loops | ||
|
||
The infinite `loop` is the simplest form of `loop` available in Rust. Using the keyword `loop`, Rust provides a way to loop until a `break` or `return` is issued. Rust's infinite `loop`s look like this: | ||
|
||
``` | ||
loop { | ||
println!("Loop forever!"); | ||
} | ||
``` | ||
|
||
Leaving a infinite `loop` can be achieved using a break statement as follows: | ||
|
||
``` | ||
let mut i = 0; | ||
loop { | ||
if i == 10 { | ||
break; | ||
} | ||
println!("Loop number {}", i); | ||
i = i + 1; | ||
} | ||
``` | ||
|
||
## Loop labels | ||
|
||
Labels can be assigned to `loop`s to so that, in the case of nested `loop`s, an outer `loop` may be left early when certain criteria are met in an inner `loop`. | ||
|
||
``` | ||
let mut i = 0; | ||
'outer: loop { | ||
'inner: loop { | ||
if i == 10 { | ||
break 'outer; | ||
} | ||
i = i + 1; | ||
} | ||
} | ||
``` | ||
|
||
In the above example, the inner `loop` is able to cause the outer `loop` to stop. |