mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-17 12:00:27 +02:00
Flesh out for
loop example (#1841)
* Add example of iterating over a collection. * Update speaker notes to call out the use of iterators. I think it's useful to call out that `for` loops primarily are used to iterate over a collection of objects, even though we haven't yet talked about any concrete collection types at this point. I think using the array literal syntax is simple enough to understand that it should be quick to explain when we get to this slide.
This commit is contained in:
parent
9023dd9caa
commit
d75dd5d681
@ -1,19 +1,25 @@
|
||||
# `for`
|
||||
|
||||
The [`for` loop](https://doc.rust-lang.org/std/keyword.for.html) iterates over
|
||||
ranges of values:
|
||||
ranges of values or the items in a collection:
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
for x in 1..5 {
|
||||
println!("x: {x}");
|
||||
}
|
||||
|
||||
for elem in [1, 2, 3, 4, 5] {
|
||||
println!("elem: {elem}");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
- We will discuss iteration later; for now, just stick to range expressions.
|
||||
- Under the hood `for` loops use a concept called "iterators" to handle
|
||||
iterating over different kinds of ranges/collections. Iterators will be
|
||||
discussed in more detail later.
|
||||
- Note that the `for` loop only iterates to `4`. Show the `1..=5` syntax for an
|
||||
inclusive range.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user