Skip to content

Commit

Permalink
Address loop labels and continue. Fixes #1392.
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Jul 21, 2021
1 parent f40c71f commit 82d3b82
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "loops"
version = "0.1.0"
edition = "2018"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Compiling loops v0.1.0 (file:///projects/loops)
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
Running `target/debug/loops`
count = 0
remaining = 10
remaining = 9
count = 1
remaining = 10
remaining = 9
count = 2
remaining = 10
End count = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {}", count);
let mut remaining = 10;

loop {
println!("remaining = {}", remaining);
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}

count += 1;
}
println!("End count = {}", count);
}
29 changes: 26 additions & 3 deletions src/ch03-05-control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,36 @@ The symbol `^C` represents where you pressed <span class="keystroke">ctrl-c
depending on where the code was in the loop when it received the interrupt
signal.

Fortunately, Rust provides another, more reliable way to break out of a loop.
You can place the `break` keyword within the loop to tell the program when to
stop executing the loop. Recall that we did this in the guessing game in the
Fortunately, Rust provides a way to break out of a loop from code. You can
place the `break` keyword within the loop to tell the program when to stop
executing the loop. Recall that we did this in the guessing game in the
[“Quitting After a Correct Guess”][quitting-after-a-correct-guess]<!-- ignore
--> section of Chapter 2 to exit the program when the user won the game by
guessing the correct number.

We also used `continue` in the guessing game. The `continue` keyword within a
loop tells the program to skip over any remaining code in this iteration of the
loop and go to the next iteration.

If you have loops within loops, `break` and `continue` apply to the innermost
loop at that point. You can optionally specify a *loop label* on a loop and
then use the label with `break` or `continue` to have those keywords applied to
the labeled loop instead of the innermost loop. Here's an example with two
nested loops:

```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs}}
```

The outer loop has the label `'counting_up`, and it will count up from 0 to 2.
The inner loop without a label counts down from 10 to 9. The first `break` that
doesn't specify a label will exit the inner loop only. The `break
'counting_up;` statement will exit the outer loop. This code prints:

```console
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt}}
```

#### Returning Values from Loops

One of the uses of a `loop` is to retry an operation you know might fail, such
Expand Down

0 comments on commit 82d3b82

Please sign in to comment.