Skip to content

Commit 345cf64

Browse files
authored
Added loops section (google#1789)
This is a contribution of a loops section for Comprehensive Rust.
1 parent e74970a commit 345cf64

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
- [Control Flow Basics](control-flow-basics.md)
3333
- [Conditionals](control-flow-basics/conditionals.md)
3434
- [Loops](control-flow-basics/loops.md)
35+
- [`for`](control-flow-basics/loops/for.md)
36+
- [`loop`](control-flow-basics/loops/loop.md)
3537
- [`break` and `continue`](control-flow-basics/break-continue.md)
3638
- [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md)
3739
- [Functions](control-flow-basics/functions.md)

src/control-flow-basics/loops.md

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,3 @@ fn main() {
2222
println!("Final x: {x}");
2323
}
2424
```
25-
26-
## `for`
27-
28-
The [`for` loop](https://doc.rust-lang.org/std/keyword.for.html) iterates over
29-
ranges of values:
30-
31-
```rust,editable
32-
fn main() {
33-
for x in 1..5 {
34-
println!("x: {x}");
35-
}
36-
}
37-
```
38-
39-
## `loop`
40-
41-
The [`loop` statement](https://doc.rust-lang.org/std/keyword.loop.html) just
42-
loops forever, until a `break`.
43-
44-
```rust,editable
45-
fn main() {
46-
let mut i = 0;
47-
loop {
48-
i += 1;
49-
println!("{i}");
50-
if i > 100 {
51-
break;
52-
}
53-
}
54-
}
55-
```
56-
57-
<details>
58-
59-
- We will discuss iteration later; for now, just stick to range expressions.
60-
- Note that the `for` loop only iterates to `4`. Show the `1..=5` syntax for an
61-
inclusive range.
62-
63-
</details>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# `for`
2+
3+
The [`for` loop](https://doc.rust-lang.org/std/keyword.for.html) iterates over
4+
ranges of values:
5+
6+
```rust,editable
7+
fn main() {
8+
for x in 1..5 {
9+
println!("x: {x}");
10+
}
11+
}
12+
```
13+
14+
<details>
15+
16+
- We will discuss iteration later; for now, just stick to range expressions.
17+
- Note that the `for` loop only iterates to `4`. Show the `1..=5` syntax for an
18+
inclusive range.
19+
20+
</details>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# `loop`
2+
3+
The [`loop` statement](https://doc.rust-lang.org/std/keyword.loop.html) just
4+
loops forever, until a `break`.
5+
6+
```rust,editable
7+
fn main() {
8+
let mut i = 0;
9+
loop {
10+
i += 1;
11+
println!("{i}");
12+
if i > 100 {
13+
break;
14+
}
15+
}
16+
}
17+
```

0 commit comments

Comments
 (0)