graydon/rust — Issue #1013: Add permute to standard library
Author: jdm
Created: 2011-10-08 18:02:10+00:00
State: open

It's too much effort to create a branch and pull request at this point, so I'll paste the code I've been using inline:

iter permute(elts: [@T]) -> [@T] {
if vec::is_not_empty(elts) {
for each i in uint::range(0u, vec::len(elts)) {
let rest = vec::slice(elts, 0u, i);
rest += vec::slice(elts, i + 1u, vec::len(elts));
for each permutation in permute(tmp) {
put [elts[i]] + permutation;
}
}
} else {
put [];
}
}