diff --git a/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock new file mode 100644 index 0000000000..f738673159 --- /dev/null +++ b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "loops" +version = "0.1.0" diff --git a/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml new file mode 100644 index 0000000000..9a198d7e1c --- /dev/null +++ b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2018" + +[dependencies] diff --git a/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt new file mode 100644 index 0000000000..cee7201e37 --- /dev/null +++ b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt @@ -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 diff --git a/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs new file mode 100644 index 0000000000..b855d75769 --- /dev/null +++ b/listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs @@ -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); +} diff --git a/src/ch03-05-control-flow.md b/src/ch03-05-control-flow.md index 0c2134a00c..90f8c51f5b 100644 --- a/src/ch03-05-control-flow.md +++ b/src/ch03-05-control-flow.md @@ -220,13 +220,36 @@ The symbol `^C` represents where you pressed 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] 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