🔃 Rust Iterators
= a value that produces a sequence of values, typically for a loop to operate on1
Usage
1. Iterator creation
Structs
std::iter::IntoIter:.into_iter()-> returns an Iterator- takes collection by value and consumes it
.drain()- borrows mut ref to collection, removes remaining entries on exit
Ranges
(1..=n)
Functions
- Specify function
from_fn(f): creates an iterator from a functionsuccessors(Some(start_val), f): likereducefrom JS for functions
2. Value processing
To avoid value consumption (which is standard for most adapters):
by_ref()-> returns iterator as a reference
Adapters
Adapters are functions, which can be called on an iterator and return a new iterator
.map(f): maps each value usingffilter(f): filters each value usingffilter_map(f): like.map()but also allows returning zero itemsflat_map(f): like.filter_map(f), but allows returning multiple itemsflatten(): concatenates all iterable items- e.g: [[1, 1.1], [2, 2.2]] -> [1, 1.1, 2, 2.2]
- used to get all Some(val) from an array of None/Some
Other
fuse()-> transforms iterator into one that definitely returns None when last element reachedinspect(f): doesn’t do anything other than calling f, used for debuggingchain(i): chains two iterators together- e.g.:
i1.chain(i2)-> iterator that iterates over i1 and then i2
- e.g.:
zip(i):: zips two iterators together- e.g.:
i1.zip(i2)-> returns pairs with(item1, item2)
- e.g.:
repeat(item): iterator that always returnsitemcycle(iterable): iterator that always iterates overiterableenumerate(): return pairs with(index, item)instead of justitem
Iteration
.take(n): limits iterator calls to ntake_while(f): end iteration when f = trueskip(n): skips first n itemsskip_while(f): skips items while f = truepeek(): returns Option containing next item (or None)- doesn’t take ownership
3. Value consumption
Called at the end of an iterator process, this is the value that’s returned
count(): returns the count of items in the iteratorsum(),product(): returns the sum/product of all items in the iterator (must be integers/floats)max(),min(): returns max/min item (implementingstd::cmp::Ord)max_by(f),min_by(f): returns max/min as determined by f, comparator function returningstd::cmp::Orderingmax_by_key(f),min_by_key(f): returns max/min as determined by f, a closure returning an optionfold(): like.reducefrom JS- e.g.:
y = (1..=n).fold(0, |x| ...)- start with 0 as running total
- continue value of closure, until end reached
- returns final value of closure
- e.g.:
nth(n),nth_back(n): skipos n items, returns next itemcollect(): returns a collections from the iterator (e.g. Vector)
Definitions
Traits
std::iter::Iterator: any value that implements this trait is an iterator- -> produces values = items
- -> items are recieved by consumers
std::iter::IntoIter: value that implements this trait is an iterableforis just shorthand for into_iter
Functions
-
.fold: like.reducefrom JS- e.g.:
y = (1..=n).fold(0, |x| ...)- start with 0 as running total
- continue value of closure, until end reached
- returns final value of closure
- e.g.:
-
from_fn(f): creates an iterator from a function -
successors(Some(start_val), f): likereducefrom JS -
.take(n): limits iterator calls to n -
.collect(): builds a vector from the iterator results
Footnotes
-
Programming Rust, 2nd Edition. Accessed April 5, 2022. https://learning.oreilly.com/library/view/programming-rust-2nd/9781492052586/. ↩