Skip to content

Commit

Permalink
Added loops section (#1789)
Browse files Browse the repository at this point in the history
This is a contribution of a loops section for Comprehensive Rust.
  • Loading branch information
mani-chand authored Feb 7, 2024
1 parent e74970a commit 345cf64
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- [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)
- [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md)
- [Functions](control-flow-basics/functions.md)
Expand Down
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 345cf64

Please sign in to comment.