1
0
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:
Manichand Kondapaka 2024-02-08 01:03:49 +05:30 committed by GitHub
parent e74970acbc
commit 345cf646e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 39 deletions

View File

@ -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)

View File

@ -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>

View 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>

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