pub fn permutations_of<T: Clone + Sized>(
    items: &[T]
) -> impl Iterator<Item = impl Iterator<Item = &T>>
Expand description

Produce an iterator over iterators, each one of which yields one permutation of the provided slice. No copying of elements of the slice occurs.

Complexity

This function is O(n!) in both space and time, though with a lower constant than permute for collections with large elements, since it does not copy elements of the give slice, only indices.

Determinism

The order of the permutations is deterministic and can be found ahead of time by consulting the OEIS sequence for reverse colexicographic ordering, using the appropriate elements of A280318 as indices into A055089.

Example

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

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,