k-combinations 0.9.0

Efficient iterator over k-element combinations of a slice
Documentation
  • Coverage
  • 77.78%
    7 out of 9 items documented7 out of 7 items with examples
  • Size
  • Source code size: 17.64 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.24 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • mrcz/combinations
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mrcz

k-combinations

Efficient iterator over k-element combinations of a slice, with no dependencies.

For slices with at most 64 elements, uses a bitmask (Gosper's hack) for fast iteration. Falls back to index-based iteration for larger slices.

Usage

Add to your project:

cargo add k-combinations
use combinations::Combinations;

let items = [1, 2, 3, 4];

// All pairs
for combo in items.combinations(2) {
    println!("{combo:?}");
}
// [&1, &2], [&1, &3], [&2, &3], [&1, &4], [&2, &4], [&3, &4]

// Works on Vec too
let v = vec!["a", "b", "c"];
let pairs: Vec<Vec<&&str>> = v.combinations(2).collect();

Range of sizes

Iterate over combinations of multiple sizes with combinations_range:

use combinations::Combinations;

let items = [1, 2, 3];

// All subsets of size 1 or 2
for combo in items.combinations_range(1..=2) {
    println!("{combo:?}");
}

// All 2^n subsets (power set)
assert_eq!(items.combinations_range(..).count(), 8);

Zero-allocation iteration

Reuse a buffer across calls with next_into, similar to BufReader::read_line:

use combinations::Combinations;

let items = [1, 2, 3, 4];
let mut iter = items.combinations(2);
let mut buf = Vec::new();
while iter.next_into(&mut buf) {
    // buf is reused — no allocation after the first call
    println!("{buf:?}");
}

License

MIT