mirror of
https://github.com/rust-lang/book.git
synced 2026-09-15 10:10:42 -04:00
@@ -81,8 +81,8 @@ for var in expression {
|
||||
```
|
||||
|
||||
The expression is an item that can be converted into an [iterator] using
|
||||
[`IntoIterator`]. The iterator gives back a series of elements. Each element is
|
||||
one iteration of the loop. That value is then bound to the name `var`, which is
|
||||
[`IntoIterator`]. The iterator gives back a series of elements, one element per
|
||||
iteration of the loop. That value is then bound to the name `var`, which is
|
||||
valid for the loop body. Once the body is over, the next value is fetched from
|
||||
the iterator, and we loop another time. When there are no more values, the `for`
|
||||
loop is over.
|
||||
@@ -194,11 +194,15 @@ 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 situation where you would like to `break` or `continue` for one
|
||||
other languages, Rust's `break` or `continue` apply to the innermost loop.
|
||||
In a situation where you would like to `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:
|
||||
`continue` statement applies to.
|
||||
|
||||
In the example below, we `continue` to the next iteration of `outer` loop
|
||||
when `x` is even, while we `continue` to the next iteration of `inner`
|
||||
loop when y is even. So it will execute the `println!` when both `x` and
|
||||
`y` are odd.
|
||||
|
||||
```rust
|
||||
'outer: for x in 0..10 {
|
||||
|
||||
Reference in New Issue
Block a user