use permutohedron::{Heap, LexicalPermutation}; pub trait PermutationGenerator { fn permutations(n: u32) -> Vec>; } pub struct HeapsPermutations; impl PermutationGenerator for HeapsPermutations { fn permutations(n: u32) -> Vec> { Heap::<'_, Vec, _>::new(&mut (1..=n).collect()).collect() } } pub struct LexicalPermutations { state: Vec, is_ended: bool, } impl Iterator for LexicalPermutations { type Item = Vec; fn next(&mut self) -> Option { if self.is_ended { None } else { self.is_ended = self.state.next_permutation(); Some(self.state.clone()) } } } impl LexicalPermutations { fn new(n: u32) -> Self { Self { state: (1..=n).collect(), is_ended: false } } } impl PermutationGenerator for LexicalPermutations { fn permutations(n: u32) -> Vec> { Self::new(n).collect() } }