mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-27 23:35:05 +02:00
Added loops section (#1789)
This is a contribution of a loops section for Comprehensive Rust.
This commit is contained in:
parent
e74970acbc
commit
345cf646e4
@ -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)
|
||||
|
@ -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
src/control-flow-basics/loops/for.md
Normal file
20
src/control-flow-basics/loops/for.md
Normal file
@ -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
src/control-flow-basics/loops/loop.md
Normal file
17
src/control-flow-basics/loops/loop.md
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user