Hey guys, I am learning rust and i create a comprehensive list of iterators in rust. I really do think this would be helpful for you as well.
Rust provides a rich set of iterator combinators and methods. Here’s a list of some common iterator methods and combinators, along with brief descriptions:
- Adapter Methods:
map
: Transforms each element using a closure.filter
: Filters elements based on a predicate.enumerate
: Adds an index to each element.skip
: Skips a specified number of elements.take
: Takes a specified number of elements.skip_while
: Skips elements until a predicate is false.take_while
: Takes elements until a predicate is false.peekable
: Wraps an iterator to allow peeking at the next element.rev
: Reverses the iterator.
- Consumer Methods:
fold
: Accumulates elements into a single value.for_each
: Applies a closure to each element.collect
: Converts the iterator into a collection.count
: Counts the number of elements.last
: Returns the last element.
- Predicate Methods:
all
: Checks if all elements satisfy a predicate.any
: Checks if any element satisfies a predicate.find
: Finds the first element satisfying a predicate.position
: Finds the index of the first element satisfying a predicate.
- Combining Iterators:
chain
: Concatenates two iterators.zip
: Combines two iterators into pairs.flat_map
: Maps each element to an iterator and flattens the result.
- Creating Iterators:
iter
: Creates an iterator over a collection.iter_mut
: Creates a mutable iterator over a collection.range
/range_inclusive
: Creates an iterator over a range of values.
- Other:
cloned
: Produces a new iterator with cloned elements.cycle
: Creates an iterator that repeats indefinitely.
chain
andchain
like:chain
: Chains two iterators together.flat_map
: Maps each element to an iterator and flattens the result.zip
: Combines two iterators into pairs.interleave
: Interleaves elements from two iterators.
peeking_take_while
andpeeking_take_until
:peeking_take_while
: Takes elements while a predicate is true, allowing peeking at the next element.peeking_take_until
: Takes elements until a predicate is true, allowing peeking at the next element.
by_ref
:by_ref
: Borrows an iterator, allowing it to be used by multiple consumers.
step_by
:step_by
: Skips a specified number of elements between each yielded element.
dedup
anddedup_by
:dedup
: Removes consecutive duplicates from an iterator.dedup_by
: Removes consecutive duplicates based on a predicate.
position
related:rposition
: Finds the index of the last element satisfying a predicate.find_map
: Finds the first element satisfying a predicate and applies a transformation.
min
andmax
:min
: Finds the minimum element.max
: Finds the maximum element.
windows
andchunks
:windows
: Produces overlapping windows of a specified size.chunks
: Produces non-overlapping chunks of a specified size.
unzip
:unzip
: Transposes a sequence of pairs into a pair of sequences.
Conclusion:
Rust Provide enough methods for functional programming.