]> git.lizzy.rs Git - rust.git/commitdiff
Added how to use labels to break nested loops to trpl.
authorJason Schein <Jasonschein@gmail.com>
Tue, 21 Jul 2015 06:18:59 +0000 (23:18 -0700)
committerJason Schein <Jasonschein@gmail.com>
Wed, 22 Jul 2015 20:00:51 +0000 (13:00 -0700)
src/doc/trpl/while-loops.md

index 0f5c3c64a4b17e1665b9b0a7d0941360e684785e..124ebc7d69ddcb5343a708ad50b26a413cf7783a 100644 (file)
@@ -88,6 +88,24 @@ for x in 0..10 {
 }
 ```
 
+You may also encounter situations where you have nested loops and need to 
+specify which one your `break` or `continue` statement is for. Like most 
+other languages, by default a `break` or `continue` will apply to innermost 
+loop. In a sitation where you would like to a `break` or `continue` for one 
+of the outer loops, you can use labels to specify which loop the `break` or
+ `continue` statement applies to. This will only print when both `x` and `y` are
+ odd:
+
+```rust
+'outer: for x in 0..10 {
+    'inner: for y in 0..10 {
+        if x % 2 == 0 { continue 'outer; } // continues the loop over x
+        if y % 2 == 0 { continue 'inner; } // continues the loop over y
+        println!("x: {}, y: {}", x, y);
+    }
+}
+```
+
 Both `continue` and `break` are valid in both `while` loops and [`for` loops][for].
 
 [for]: for-loops.html