about
06/03/2023
RustBites - Iterators
Rust Bite - Iterators
stepping through collections
Introduction
Functions that accept and return Iterators
While there are items it returns
appropriate iterator.
Iterating over Collections
invoke | operation | signature | returned iterator implements |
---|---|---|---|
iter() | Iterates over &T | fn iter(&self) -> Iter<'a, T> | fn next(&self) -> Option<&T> |
iter_mut() | Iterates over &mut T | fn iter_mut(&self) -> IterMut<'a, T> | fn next(&mut self) -> Option<&mut T> |
into_iter() | Iterates over, consumes T | into_iter(self) -> IntoIterator<T> | fn next(self) -> Option<T> |
Iterating over Strings
Table 1. utf-8 character boundaries
char size | indicator |
---|---|
1 byte, e.g. ASCII | byte starts with bit 0 |
2 bytes | First byte starts with bits 110 |
3 bytes | First byte starts with bits 1110 |
4 bytes | First byte starts with bits 11110 |
not first byte | byte starts with bits 10 |
-
chars(&self) -> Chars<'_> Chars<'_> implementsnext(&self) -> Option<char> -
char_indices(&self) -> CharIndices<'_> CharIndices<'_> implementsnext(&self) -> Option<(usize, char)> -
bytes(&self) -> Bytes<'_> Bytes<'_> implementsnext(&self) -> Option<u8>
Iterator Adapters
Table 2. - Iterator Methods
Member function | Operation | Note |
---|---|---|
next(&mut self) -> Option<Self::Item> | Return next element in collection via Option | |
count(self) -> usize | Returns number of iterations | Terminal, consumes iterator |
last(self) -> Option<Self::Item> | Returns last item via Option | Terminal, consumes iterator |
nth(&mut self, n: usize) -> Option<Self::Item> | Returns nth element | |
step-by(self, step: usize) ->StepBy<Self> | Creates new iterator starting at same point, but stepping by given amount at each iteration. | Consumes original iterator |
skip(self, n: usize) -> Skip<Self> | Creates an iterator that skips the first n elements | Consumes original iterator |
skip_while
<P>(self, p: P) -> Skip_while<Self, P> where P: FnMut(&Self::Item) -> bool |
Creates an iterator that skips elements if predicate is true. Returns after first false. | Consumes original iterator |
take(self, n:usize) -> Take<Self> | Creates an iterator that returns first n elements | Consumes original iterator |
find<P>(&mut self, predicate: P) -> Option<Self::Item> where P: FnMut(&Self::Item) -> bool |
Searches for first element that satisfies predicate. | |
enumerate(self) -> Enumerate<Self> <=> Option<(usize, &Item)> |
Returns (i, val) where i is current index and val is the value of the current item. | Consumes original iterator |
map<B, F>(self, f: F) -> Map<Self, F> | Takes a closure and creates an iterator that calls the closure on each element | Consumes original iterator |
filter<P>(self, predicate: P) - > Filter<Self, P> where P: FnMut(&Self::Item) -> bool |
Creates an iterator which takes a closure to decide if element should be included in result | Consumes original iterator |
collect<B>(self) -> B | Transforms iterator into collection | Terminal, consumes iterator |
any<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool |
Tests if any element of iterator matches a predicate | Terminal |
all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool |
Tests if every element of iterator matches a predicate | Terminal |
for_each<F>(self, f: F) where F: FnMut(Self::Item) |
Calls closure on each element of iterator | Terminal, consumes iterator |
position<P>(&mut self, p: P) -> Option<usize> where F: FnMut(Self::Item) -> bool |
Returns index of first match | Terminal |
product<P>(self) -> P where P: Product<Self::Item> |
Returns product of all elements, else 1 if empty | Terminal |
sum<S>(self) -> S where S: Sum<Self::Item> |
Returns sum of all elements, else 0 if empty | Terminal |
by_ref(&mut self) -> &mut Self | Supports using these adapters while retaining ownership of the original iterator | Avoids consuming original |
copied<'a, T>(self) -> Copied<Self> where Self: Itertor<Item = &'a T>, T: 'a + Copy; |
Creates iterator which copies all of its elements | Consumes original iterator |
cloned<'a, T>(self) -> Cloned<Self> where Self: Itertor<Item = &'a T>, T: 'a + Clone; |
Creates iterator which clones all of its elements | Consumes original iterator |
More adapter functions ... | std::iter::Iterator | |
For loops
Ranges
Slices
References
Link | Description |
---|---|
std::iter::Iterator | Documentation for Iterator from the std library |
std::iter::IntoIterator | Documentation for IntoIterator from the std library |
Iterators in Rust - Thoughtram | Article with clear descriptions and example code |
rustomax/rust-iterators | Github readme file with examples of ranges and iterator adapters |
Yet Another rust iterators tutorial | Article with clear basics and examples of custom iterators |
Rust iterator cheat sheet | Lots of details |
keyword for | Rust documentation on for loops |
std::ops::Range | Rust documentation for ranges |
std::slice | Rust documentation for slices |