Refactor Rust's permutation implementation.