Here's an example implementation of finding all permutations of an array in Rust:
main.rs292 chars12 lines
This function takes in an array arr
, as well as the starting index left
and ending index right
of the subarray that we want to perform permutation on. Initially, the function is called with left = 0
and right = arr.len() - 1
.
Within the function, we check if left
and right
are the same. If they are, we have reached the end of the subarray and print out the permutation.
Otherwise, we loop through the subarray from left
to right
and perform recursive calls to the permutation
function after swapping elements in the subarray.
Here's an example usage of the function:
main.rs54 chars3 lines
This will print out all permutations of the subarray [1, 2, 3]
:
main.rs60 chars7 lines
gistlibby LogSnag