| Crates.io | iter-merge |
| lib.rs | iter-merge |
| version | 1.0.0 |
| created_at | 2025-09-07 17:24:52.006611+00 |
| updated_at | 2025-10-23 12:32:34.702507+00 |
| description | A high-performance iterator merging library |
| homepage | https://github.com/Andrew-Morozko/iter-merge |
| repository | https://github.com/Andrew-Morozko/iter-merge |
| max_upload_size | |
| id | 1828353 |
| size | 155,034 |
A high-performance iterator merging library for Rust that efficiently combines multiple iterators into a single iterator, yielding smallest item first.
Note: only compares the first items across provided iterators. The output would be sorted only if the iterators themselves are sorted.
.next() advances only one iteratorVec and Array (for no-std and no-alloc compatibility)Peekable methodsuse iter_merge::merge;
let iter1 = vec![1, 3, 5, 7];
let iter2 = vec![2, 4, 6, 8];
let result = merge([iter1, iter2]).into_vec();
assert_eq!(result, vec![1, 2, 3, 4, 5, 6, 7, 8]);
use iter_merge::{VecStorage, comparators::ByOrd};
let mut merged = VecStorage::from_iter([
vec![9, 6, 3],
vec![8, 5, 2],
vec![7, 4, 1],
])
.into_builder()
.max_by(ByOrd) // {min|max}{_by|_by_func|_by_key}
.build();
let result = merged.into_vec();
assert_eq!(result, vec![9, 8, 7, 6, 5, 4, 3, 2, 1]);
use core::pin::pin;
use iter_merge::{ArrayStorage, MergeIter};
// First create a fixed-capacity array storage
let storage = ArrayStorage::from_arr([
[1, 3, 5],
[2, 4, 6]
]);
// In order to construct a MergeIter you need to pin that storage.
// You won't be able to modify it once you've pinned it.
let storage = pin!(storage);
let mut merge = storage.build();
assert!(merge.eq([1, 2, 3, 4, 5, 6]));
It's 1.45-1.65x faster than itertools::kmerge in my benchmarks and scales as O(item_count + logâ‚‚(iterator_count)) (just as well as itertools::kmerge)
The crate supports following feature flags:
alloc: Enables heap-allocated storage with VecStorage and methods like MergeIter::into_vecFuzzing:
Use cargo-fuzz
cargo +nightly fuzz run fuzz_correctness
cargo +nightly fuzz run fuzz_usage
Miri:
cargo +nightly miri test
Benchmarks:
RUSTFLAGS='-C target-cpu=native --cfg benchmarking' cargo bench --bench benchmarks
(Benchmarking dependencies are behind benchmarking cfg option)
The minimum supported Rust version for this crate is 1.68.0. This may change in a major release, but it is unlikely.
Contributions are welcome. For major changes, please open an issue first to discuss what you would like to change.
itertools - General iterator utilities (includes kmerge)