How to reverse a string in Rust

2 Answers

0 votes
fn reverse_string(s: &str) -> String {
    s.chars().rev().collect()
}
  
fn main() {
    let string = String::from("rust c c++ c#");
  
    let reversed = reverse_string(&string);
  
    println!("{}", reversed);
}
 




/*
run:
    
#c ++c c tsur
    
*/

 



answered Oct 1, 2022 by avibootz
0 votes
fn main() {
    let s = "rust";
     
    let revs = s.chars().rev().collect::<String>();
     
    println!("{}", revs);
}
 
 
       
/*
run:
   
tsur
  
*/

 



answered Oct 19, 2024 by avibootz
...