fn generate_permutations(str: &str) -> Vec<String> {
// Check if the string is empty.
if str.len() == 0 {
return vec![String::new()];
}
// Create a vector to store the permutations.
let mut permutations = Vec::new();
// Iterate over the characters in the string.
for i in 0..str.len() {
// Create a copy of the string without the current character.
let mut remaining_str = str.to_string();
remaining_str.remove(i);
// Recursively generate all permutations of the remaining string.
let mut recursive_permutations = generate_permutations(&remaining_str);
// For each permutation of the remaining string, add the current character to the front.
for permutation in recursive_permutations {
permutations.push(format!("{}{}", str[i], permutation));
}
}
// Return the vector of permutations.
return permutations;
}
fn main() {
let str = "hello";
let permutations = generate_permutations(str);
println!("The permutations of the string are:");
for permutation in permutations {
println!("{}", permutation);
}
}
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 .rs file and then compile and run it using the following commands:
Code snippet
rustc permutations.rs
./permutations
This will print the following output:
Code snippet
The permutations of the string are:
hello
helo
hleo
hlol
lloe
This indicates that the six permutations of the string hello are hello, helo, hleo, hlol, lloe, and olhe.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, 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.