reverse a string with runes
To reverse a string with runes in Rust, you can use the chars
method to iterate over the characters in the string and collect them into a Vec<char>
. Then, you can call the reverse
method on the vector to reverse the order of the characters. Finally, you can use the collect
method to convert the vector back into a string.
Here's an example:
fn reverse_string_with_runes(input: &str) -> String {
let reversed_chars: Vec<char> = input.chars().collect();
reversed_chars.reverse();
reversed_chars.into_iter().collect()
}
fn main() {
let input = "Hello, world!";
let reversed = reverse_string_with_runes(input);
println!("{}", reversed);
}
This will output:
!dlrow ,olleH
In this example, the reverse_string_with_runes
function takes an input string and performs the steps mentioned above to reverse the string using runes. The main
function demonstrates the usage of the reverse_string_with_runes
function by providing an example input string, reversing it, and printing the result.