1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-22 14:59:37 +02:00

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.
This commit is contained in:
Igor Petruk 2023-01-23 12:04:42 +00:00 committed by GitHub
parent 581f5a4008
commit 66509f2f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,19 @@ fn main() {
for x in v {
println!("x: {x}");
}
for i in (0..10).step_by(2) {
println!("i: {i}");
}
}
```
You can use `break` and `continue` here as usual.
<details>
* Index iteration is not a special syntax in Rust for just that case.
* `(0..10)` is a range that implements an `Iterator` trait.
* `step_by` is a method that returns another `Iterator` that skips every other element.
</details>