Implement a Rust program to generate all possible permutations of a given string.
Implement a Rust program to generate all possible permutations of a given string.
379
20-Jun-2023
Aryan Kumar
23-Jun-2023Sure, here is a Rust program to generate all possible permutations of a given string:
Rust
This program first checks if the string is empty. If it is, then the program returns a vector containing a single empty string. Otherwise, the program creates a vector to store the permutations. The program then iterates over the characters in the string. For each character, the program creates a copy of the string without the current character. The program then recursively generates all permutations of the remaining string. For each permutation of the remaining string, the program adds the current character to the front. The program then adds the resulting permutation to the vector of permutations. Finally, the program returns the vector of permutations.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will print the following output:
Code snippet
This indicates that the six permutations of the string hello are hello, helo, hleo, hlol, lloe, and olhe.