You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We'll go through the examples from [Rust by Example](https://doc.rust-lang.org/rust-by-example/fn/closures.html).
52
54
More examples will be seen when working with iterators.
53
55
54
-
55
56
# Iterators
56
57
57
58
In Rust, there is no hierarchy of types for collections (because there is no inheritance in general).
58
59
Instead, what makes a collection is that it can be iterated over.
59
60
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
+
61
63
- exhausting it with the `for` loop,
62
64
- manually iterating over it using `next()` calls,
63
65
- 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`),
Iterators are highly optimised, so they are high-level code that compiles down to simple and optimised machine code (intended as _zero-cost abstractions_).
70
71
71
72
We'll go through the official [docs](https://doc.rust-lang.org/stable/std/iter/).
73
+
72
74
- Most methods are defined in the [Iterator trait](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html).
73
75
-[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.
74
76
- 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/)
0 commit comments