Iterator methods and combinators in Rust

Iterator methods and combinators in Rust

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:

  1. 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.
  2. 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.
  3. 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.
  4. Combining Iterators:
    • chain: Concatenates two iterators.
    • zip: Combines two iterators into pairs.
    • flat_map: Maps each element to an iterator and flattens the result.
  5. 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.
  6. Other:
    • cloned: Produces a new iterator with cloned elements.
    • cycle: Creates an iterator that repeats indefinitely.
  7. chain and chainlike:
    • 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.
  8. peeking_take_while and peeking_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.
  9. 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 and dedup_by:
    • dedup: Removes consecutive duplicates from an iterator.
    • dedup_by: Removes consecutive duplicates based on a predicate.
    • positionrelated:
    • 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 and max:
    • min: Finds the minimum element.
    • max: Finds the maximum element.
    • windows and chunks:
    • 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.

Did you find this article valuable?

Support Pratik Sharma by becoming a sponsor. Any amount is appreciated!