forked from google/comprehensive-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added loops and break-continue sections (google#1789)
- Loading branch information
1 parent
e74970a
commit 5d5dc97
Showing
6 changed files
with
80 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
``` | ||
|
||
<details> | ||
|
||
- 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). | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# `for` | ||
|
||
The [`for` loop](https://doc.rust-lang.org/std/keyword.for.html) iterates over | ||
ranges of values: | ||
|
||
```rust,editable | ||
fn main() { | ||
for x in 1..5 { | ||
println!("x: {x}"); | ||
} | ||
} | ||
``` | ||
|
||
<details> | ||
|
||
- We will discuss iteration later; for now, just stick to range expressions. | ||
- Note that the `for` loop only iterates to `4`. Show the `1..=5` syntax for an | ||
inclusive range. | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# `loop` | ||
|
||
The [`loop` statement](https://doc.rust-lang.org/std/keyword.loop.html) just | ||
loops forever, until a `break`. | ||
|
||
```rust,editable | ||
fn main() { | ||
let mut i = 0; | ||
loop { | ||
i += 1; | ||
println!("{i}"); | ||
if i > 100 { | ||
break; | ||
} | ||
} | ||
} | ||
``` |