Module itertools
Extra iterator methods and utilities.
- Crate
::itertools. - docs.rs
- crates.io
- GitHub
itertools extends Rust's iterator functionality with a rich collection
of additional iterator methods and utilities.
The crate provides the Itertools trait that adds dozens of useful methods
to any iterator, enabling powerful functional programming patterns.
It includes methods for grouping, batching, sorting, deduplication,
and complex iteration patterns that would otherwise require verbose loops.
Key features include chunk_by for grouping consecutive elements,
chunk for batching elements into fixed-size groups,
sorted for sorting without collecting first,
dedup for removing duplicates,
intersperse for inserting separators,
and cartesian_product for combining iterators.
The crate also provides standalone functions like zip_eq for
strict zipping that panics on length mismatches,
and repeat_n for creating repeating sequences.
Examples
Grouping consecutive elements:
use Itertools;
let data = vec!;
let groups: = data
.into_iter
.chunk_by
.into_iter
.map
.collect;
assert_eq!;
Batching and processing in chunks:
use Itertools;
let data = 1..=10;
let chunks: = data.chunks.into_iter.map.collect;
assert_eq!;
Combining iterators with cartesian product:
use Itertools;
let coords: =
.cartesian_product
.collect;
assert_eq!;
assert_eq!;
assert_eq!;
Modules
Enums
-
FoldWhile
An enum used for controlling the execution of
fold_while.
Traits
Functions
-
assert_equal
Assert that two iterables produce equal sequences, with the same
semantics as
equal(a, b). -
equal
Return
trueif both iterables produce equal sequences (elements pairwise equal and sequences of the same length),falseotherwise. -
partition
Partition a sequence using predicate
predso that elements that map totrueare placed before elements which map tofalse.