Skip to content

Commit

Permalink
Adding docs for loops and loop labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
KieranHunt committed Jul 26, 2015
1 parent a5c12f4 commit b36551b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/doc/trpl/SUMMARY.md
Expand Up @@ -26,6 +26,7 @@
* [Primitive Types](primitive-types.md)
* [Comments](comments.md)
* [if](if.md)
* [infinite loops](infinite-loops.md)
* [for loops](for-loops.md)
* [while loops](while-loops.md)
* [Ownership](ownership.md)
Expand Down
40 changes: 40 additions & 0 deletions src/doc/trpl/infinite-loops.md
@@ -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.

0 comments on commit b36551b

Please sign in to comment.