Skip to content

Commit

Permalink
Added loops and break-continue sections (google#1789)
Browse files Browse the repository at this point in the history
  • Loading branch information
mani-chand authored and djmitche committed Feb 15, 2024
1 parent e74970a commit 5d5dc97
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 80 deletions.
3 changes: 3 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
- [Control Flow Basics](control-flow-basics.md)
- [Conditionals](control-flow-basics/conditionals.md)
- [Loops](control-flow-basics/loops.md)
- [`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)
Expand Down
52 changes: 11 additions & 41 deletions src/control-flow-basics/break-continue.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<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>
29 changes: 29 additions & 0 deletions src/control-flow-basics/break-continue/labels.md
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>
39 changes: 0 additions & 39 deletions src/control-flow-basics/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,3 @@ fn main() {
println!("Final x: {x}");
}
```

## `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}");
}
}
```

## `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;
}
}
}
```

<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>
20 changes: 20 additions & 0 deletions src/control-flow-basics/loops/for.md
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>
17 changes: 17 additions & 0 deletions src/control-flow-basics/loops/loop.md
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;
}
}
}
```

0 comments on commit 5d5dc97

Please sign in to comment.