@@ -1,7 +1,7 @@
|
||||
# Combinators
|
||||
|
||||
Iterators can do so much more than `for` loops!
|
||||
If you look at the documentation for the `Iterator` trait, you'll find a **vast** collections of
|
||||
Iterators can do so much more than `for` loops!\
|
||||
If you look at the documentation for the `Iterator` trait, you'll find a **vast** collections of
|
||||
methods that you can leverage to transform, filter, and combine iterators in various ways.
|
||||
|
||||
Let's mention the most common ones:
|
||||
@@ -15,7 +15,7 @@ Let's mention the most common ones:
|
||||
- `take` stops the iterator after `n` elements.
|
||||
- `chain` combines two iterators into one.
|
||||
|
||||
These methods are called **combinators**.
|
||||
These methods are called **combinators**.\
|
||||
They are usually **chained** together to create complex transformations in a concise and readable way:
|
||||
|
||||
```rust
|
||||
@@ -29,10 +29,10 @@ let outcome: u32 = numbers.iter()
|
||||
|
||||
## Closures
|
||||
|
||||
What's going on with the `filter` and `map` methods above?
|
||||
What's going on with the `filter` and `map` methods above?\
|
||||
They take **closures** as arguments.
|
||||
|
||||
Closures are **anonymous functions**, i.e. functions that are not defined using the `fn` syntax we are used to.
|
||||
Closures are **anonymous functions**, i.e. functions that are not defined using the `fn` syntax we are used to.\
|
||||
They are defined using the `|args| body` syntax, where `args` are the arguments and `body` is the function body.
|
||||
`body` can be a block of code or a single expression.
|
||||
For example:
|
||||
@@ -70,10 +70,10 @@ let add_one: fn(i32) -> i32 = |x| x + 1;
|
||||
|
||||
## `collect`
|
||||
|
||||
What happens when you're done transforming an iterator using combinators?
|
||||
What happens when you're done transforming an iterator using combinators?\
|
||||
You either iterate over the transformed values using a `for` loop, or you collect them into a collection.
|
||||
|
||||
The latter is done using the `collect` method.
|
||||
The latter is done using the `collect` method.\
|
||||
`collect` consumes the iterator and collects its elements into a collection of your choice.
|
||||
|
||||
For example, you can collect the squares of the even numbers into a `Vec`:
|
||||
@@ -86,7 +86,7 @@ let squares_of_evens: Vec<u32> = numbers.iter()
|
||||
.collect();
|
||||
```
|
||||
|
||||
`collect` is generic over its **return type**.
|
||||
`collect` is generic over its **return type**.\
|
||||
Therefore you usually need to provide a type hint to help the compiler infer the correct type.
|
||||
In the example above, we annotated the type of `squares_of_evens` to be `Vec<u32>`.
|
||||
Alternatively, you can use the **turbofish syntax** to specify the type:
|
||||
@@ -104,4 +104,4 @@ let squares_of_evens = numbers.iter()
|
||||
|
||||
- [`Iterator`'s documentation](https://doc.rust-lang.org/std/iter/trait.Iterator.html) gives you an
|
||||
overview of the methods available for iterators in `std`.
|
||||
- [The `itertools` crate](https://docs.rs/itertools/) defines even **more** combinators for iterators.
|
||||
- [The `itertools` crate](https://docs.rs/itertools/) defines even **more** combinators for iterators.
|
||||
|
||||
Reference in New Issue
Block a user