Skip to content

Commit c300a5f

Browse files
committed
Prettier
1 parent 7b53dc6 commit c300a5f

File tree

1 file changed

+8
-6
lines changed
  • content/lessons/06_closures_iterators

1 file changed

+8
-6
lines changed

content/lessons/06_closures_iterators/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ We will often use `impl` keyword with closure traits (e.g., `impl Fn`) - those t
2323
## Closures capture environment
2424

2525
Closures can capture variables from the environment where they are defined. They can do that in two ways:
26+
2627
- Capturing References (borrowing), or
2728
- Moving Ownership.
2829

@@ -40,35 +41,36 @@ Based on **WHAT** a closure does with its captures, it implements closure traits
4041
- `Fn` - closures that do not mutate their captures (so can be called multiple times through an immutable reference).
4142

4243
For completeness, there is a (concrete) type of function pointers:
44+
4345
- `fn` - functions, closures with no captures.
4446

4547
Those traits and the `fn` type form a hierarchy: `fn` < `Fn` < `FnMut` < `FnOnce`
4648

47-
$$ fn \subseteq Fn \subseteq FnMut \subseteq FnOnce $$
49+
$$ fn \subseteq Fn \subseteq FnMut \subseteq FnOnce $$
4850

4951
## Examples
5052

5153
We'll go through the examples from [Rust by Example](https://doc.rust-lang.org/rust-by-example/fn/closures.html).
5254
More examples will be seen when working with iterators.
5355

54-
5556
# Iterators
5657

5758
In Rust, there is no hierarchy of types for collections (because there is no inheritance in general).
5859
Instead, what makes a collection is that it can be iterated over.
5960

60-
A usual way in Rust to perform an iteration over something, be it a range of values or items in a collection, is creating a (lazy) iterator over it and transforming it using *iterator adaptors*. For example, if `T: Iterator`, then `T::map()` creates a `Map<T>` adaptor. Once a final iterator is created, it has to be actually activated, which is most commonly done by:
61+
A usual way in Rust to perform an iteration over something, be it a range of values or items in a collection, is creating a (lazy) iterator over it and transforming it using _iterator adaptors_. For example, if `T: Iterator`, then `T::map()` creates a `Map<T>` adaptor. Once a final iterator is created, it has to be actually activated, which is most commonly done by:
62+
6163
- exhausting it with the `for` loop,
6264
- manually iterating over it using `next()` calls,
6365
- collecting its contents into inferred collection (`collect()`),
64-
- consuming it with a *consuming adaptor* (e.g., `sum()`, `count`),
66+
- consuming it with a _consuming adaptor_ (e.g., `sum()`, `count`),
6567

6668
{{ include_code_sample(path="lessons/06_closures_iterators/iterator_exhaustion.rs", language="rust") }}
6769

68-
6970
Iterators are highly optimised, so they are high-level code that compiles down to simple and optimised machine code (intended as _zero-cost abstractions_).
7071

7172
We'll go through the official [docs](https://doc.rust-lang.org/stable/std/iter/).
73+
7274
- Most methods are defined in the [Iterator trait](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html).
7375
- [IntoIterator](https://doc.rust-lang.org/stable/std/iter/trait.IntoIterator.html) is also worth noting, because it makes types work with the `for` loop.
7476
- For completeness, there is [FromIterator](https://doc.rust-lang.org/stable/std/iter/trait.FromIterator.html), which is required for `collect()` to work.
@@ -85,4 +87,4 @@ We'll go through the official [docs](https://doc.rust-lang.org/stable/std/iter/)
8587

8688
[Lazy](https://classroom.github.com/a/9aJix-LK)
8789

88-
Deadline: 06.11.2024 23:59
90+
Deadline: 06.11.2024 23:59

0 commit comments

Comments
 (0)