iter_cartesian
A chainable .cartesian() adapter that yields every (a, b) pair from two iterators, the iterator equivalent of a nested loop.
Usage
use CartesianExt;
// collect every (a, b) pair
let pairs: = .cartesian.collect;
// iterate in reverse
let rev_pairs: = .cartesian.rev.collect;
// O(1) overrides
let item = .cartesian.nth;
let last = .cartesian.last;
let total = .cartesian.count;
Features
- Chainable
.cartesian()method on any double-ended exact-size iterator - Full
DoubleEndedIteratorsupport, interleavenext()andnext_back()correctly - O(1)
len,nth, andcountvia overridden iterator methods TrustedLenandFusedIteratorimplementations
Benchmarks
All O(1) operations confirmed flat across input sizes. Collection is ~54% faster than flat_map, with significantly lower variance.
| Benchmark | Time |
|---|---|
len, 10 × 10 |
455 ps |
len, 1000 × 1000 |
449 ps |
len, 100000 × 100000 |
447 ps |
nth, jump 10 |
16.6 ns |
nth, jump 100,000 |
16.9 ns |
nth, jump 1,000,000 |
16.6 ns |
collect, 1000 × 1000 |
2.48 ms |
flat_map equivalent |
5.44 ms (±1.54 ms) |
Alternatives
itertools::iproduct!, macro-based, noDoubleEndedIteratorflat_map, norev(), no O(1)len, ~54% slower to collect
License
MIT OR Apache-2.0