From f391e863aba029a50086d1d3b6cc2d9b9a62f180 Mon Sep 17 00:00:00 2001 From: Manichand Kondapaka Date: Fri, 16 Feb 2024 03:49:06 +0530 Subject: [PATCH] Added break-continue section. (#1793) This is a contribution of a break-continue section for Comprehensive Rust. ![image](https://github.com/google/comprehensive-rust/assets/65899331/e68bf439-bcbd-43c9-88bd-f66470a5956f) ![image](https://github.com/google/comprehensive-rust/assets/65899331/1644a460-8373-4878-b6b9-0ce498e8c95d) --------- Co-authored-by: Dustin J. Mitchell --- src/SUMMARY.md | 1 + src/control-flow-basics/break-continue.md | 52 ++++--------------- .../break-continue/labels.md | 29 +++++++++++ 3 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 src/control-flow-basics/break-continue/labels.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index e6237fc9..9f64e433 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -35,6 +35,7 @@ - [`for`](control-flow-basics/loops/for.md) - [`loop`](control-flow-basics/loops/loop.md) - [`break` and `continue`](control-flow-basics/break-continue.md) + - [Labels](control-flow-basics/break-continue/labels.md) - [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md) - [Functions](control-flow-basics/functions.md) - [Macros](control-flow-basics/macros.md) diff --git a/src/control-flow-basics/break-continue.md b/src/control-flow-basics/break-continue.md index e46a3382..b844b69a 100644 --- a/src/control-flow-basics/break-continue.md +++ b/src/control-flow-basics/break-continue.md @@ -4,56 +4,26 @@ minutes: 4 # `break` and `continue` +If you want to immediately start the next iteration use +[`continue`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#continue-expressions). + If you want to exit any kind of loop early, use [`break`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#break-expressions). For `loop`, this can take an optional expression that becomes the value of the `loop` expression. -If you want to immediately start the next iteration use -[`continue`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#continue-expressions). - ```rust,editable fn main() { - let (mut a, mut b) = (100, 52); - let result = loop { - if a == b { - break a; + let mut i = 0; + loop { + i += 1; + if i > 5 { + break; } - if a < b { - b -= a; - } else { - a -= b; - } - }; - println!("{result}"); -} -``` - -Both `continue` and `break` can optionally take a label argument which is used -to break out of nested loops: - -```rust,editable -fn main() { - 'outer: for x in 1..5 { - println!("x: {x}"); - let mut i = 0; - while i < x { - println!("x: {x}, i: {i}"); - i += 1; - if i == 3 { - break 'outer; - } + if i % 2 == 0 { + continue; } + println!("{}", i); } } ``` - -In this case we break the outer loop after 3 iterations of the inner loop. - -
- -- Note that `loop` is the only looping construct which returns a non-trivial - value. This is because it's guaranteed to be entered at least once (unlike - `while` and `for` loops). - -
diff --git a/src/control-flow-basics/break-continue/labels.md b/src/control-flow-basics/break-continue/labels.md new file mode 100644 index 00000000..af198c4e --- /dev/null +++ b/src/control-flow-basics/break-continue/labels.md @@ -0,0 +1,29 @@ +# Labels + +Both `continue` and `break` can optionally take a label argument which is used +to break out of nested loops: + +```rust,editable +fn main() { + let s = [[5, 6, 7], [8, 9, 10], [21, 15, 32]]; + let mut found = false; + let target_value = 10; + 'outer: for i in 0..=2 { + for j in 0..=2 { + if s[i][j] == target_value { + found = true; + break 'outer; + } + } + } + print!("{}", found) +} +``` + +
+ +- Note that `loop` is the only looping construct which returns a non-trivial + value. This is because it's guaranteed to be entered at least once (unlike + `while` and `for` loops). + +