Skip to content

Commit 66509f2

Browse files
authored
Update for-expressions.md (#238)
If we are introducing `for` loops, I think it is beneficial to show a very basic `for i=0; i < 10; i+=2` too.
1 parent 581f5a4 commit 66509f2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/control-flow/for-expressions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ fn main() {
1010
for x in v {
1111
println!("x: {x}");
1212
}
13+
14+
for i in (0..10).step_by(2) {
15+
println!("i: {i}");
16+
}
1317
}
1418
```
1519

1620
You can use `break` and `continue` here as usual.
21+
22+
<details>
23+
24+
* Index iteration is not a special syntax in Rust for just that case.
25+
* `(0..10)` is a range that implements an `Iterator` trait.
26+
* `step_by` is a method that returns another `Iterator` that skips every other element.
27+
28+
</details>

0 commit comments

Comments
 (0)