-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e902b1e
commit 7f0c591
Showing
5 changed files
with
152 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
minutes: 5 | ||
--- | ||
|
||
# `Iterator` Helper Methods | ||
|
||
In addition to the `next` method that defines how an iterator behaves, the | ||
`Iterator` trait provides 70+ helper methods that can be used to build | ||
customized iterators. | ||
|
||
```rust,editable | ||
let result: i32 = (1..=10) // Create a range from 1 to 10 | ||
.filter(|&x| x % 2 == 0) // Keep only even numbers | ||
.map(|x| x * x) // Square each number | ||
.sum(); // Sum up all the squared numbers | ||
println!("The sum of squares of even numbers from 1 to 10 is: {}", result); | ||
``` | ||
|
||
<details> | ||
|
||
- The `Iterator` trait implements many common functional programming operations | ||
over collections (e.g. `map`, `filter`, `reduce`, etc). This is the trait | ||
where you can find all the documentation about them. | ||
|
||
- Many of these helper methods take the original iterator and produce a new | ||
iterator with different behavior. These are know as "iterator adapter | ||
methods". | ||
|
||
- Some methods, like `sum` and `count`, consume the iterator and pull all of the | ||
elements out of it. | ||
|
||
- These methods are designed to be chained together so that it's easy to build a | ||
custom iterator that does exactly what you need. | ||
|
||
## More to Explore | ||
|
||
- Rust's iterators are extremely efficient and highly optimizable. Even complex | ||
iterators made by combining many adapter methods will still result in code as | ||
efficient as equivalent imperative implementations. | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
minutes: 3 | ||
--- | ||
|
||
# Motivating Iterators | ||
|
||
If you want to iterate over the contents of an array, you'll need to define: | ||
|
||
- Some state to keep track of where you are in the iteration process, e.g. an | ||
index. | ||
- A condition to determine when iteration is done. | ||
- Logic for updating the state of iteration each loop. | ||
- Logic for fetching each element using that iteration state. | ||
|
||
In a C-style for loop you declare these things directly: | ||
|
||
```c,editable | ||
for (int i = 0; i < array_len; i += 1) { | ||
int elem = array[i]; | ||
} | ||
``` | ||
|
||
In Rust we bundle this state and logic together into an object known as an | ||
"iterator". | ||
|
||
<details> | ||
|
||
- This slide provides context for what Rust iterators do under the hood. We use | ||
the (hopefully) familiar construct of a C-style `for` loop to show how | ||
iteration requires some state and some logic, that way on the next slide we | ||
can show how an iterator bundles these together. | ||
|
||
- Rust doesn't have a C-style `for` loop, but we can express the same thing with | ||
`while`: | ||
```rust,editable | ||
let array = [2, 4, 6, 8]; | ||
let mut i = 0; | ||
while i < array.len() { | ||
let elem = array[i]; | ||
i += 1; | ||
} | ||
``` | ||
|
||
## More to Explore | ||
|
||
There's another way to express array iteration using `for` in C and C++: You can | ||
use a pointer to the front and a pointer to the end of the array and then | ||
compare those pointers to determine when the loop should end. | ||
|
||
```c,editable | ||
for (int *ptr = array; ptr < array + len; ptr += 1) { | ||
int elem = *ptr; | ||
} | ||
``` | ||
|
||
If students ask, you can point out that this is how Rust's slice and array | ||
iterators work under the hood (though implemented as a Rust iterator). | ||
|
||
</details> |