#combinatorics #heaps #algorithm #permutation

permute

Generate permutations of vectors and slices in a memory-efficient and deterministic manner, using Heap's algorithm

2 unstable releases

0.2.1 Jun 5, 2022
0.1.0 Aug 7, 2019

#1639 in Algorithms

Download history 360/week @ 2025-10-13 477/week @ 2025-10-20 502/week @ 2025-10-27 399/week @ 2025-11-03 313/week @ 2025-11-10 418/week @ 2025-11-17 184/week @ 2025-11-24 355/week @ 2025-12-01 449/week @ 2025-12-08 210/week @ 2025-12-15 73/week @ 2025-12-22 87/week @ 2025-12-29 225/week @ 2026-01-05 216/week @ 2026-01-12 636/week @ 2026-01-19 922/week @ 2026-01-26

2,013 downloads per month
Used in defamed

AGPL-3.0-or-later

11KB
130 lines

permute

crate.io version badge docs.rs status badge github actions status badge

Generate permutations of a slice in a memory-efficient and deterministic manner, using Heap's algorithm.

For instance, printing all the permutations of the sequence ["red", "green", "blue"]:

use permute::permutations_of;

for permutation in permutations_of(&["red", "green", "blue"]) {
    for element in permutation {
        print!("{}, ", element);
    }
    println!("");
}

Based on the ordering provided by Heap’s algorithm, it’s guaranteed that this program will produce:

red, green, blue,
green, red, blue,
blue, red, green,
red, blue, green,
green, blue, red,
blue, green, red,

This crate also provides the ArbitraryTandemControlIter, which allows iterating over a slice using a slice of indices - that's how Heap's algorithm is implemented here.

No runtime deps