How to remove punctuation from a string in Rust

1 Answer

0 votes
fn main() {
    let mut s = String::from("Rust: is a general-purpose, ~!@#$%^&*() programm[i]ng language, emphasizes performance.");
    
    // Replace non-word characters and underscores with an empty string
    s = s.chars()
        .filter(|c| c.is_alphanumeric() || c.is_whitespace())
        .collect();
    
    // Replace multiple spaces with a single space
    s = s.split_whitespace().collect::<Vec<&str>>().join(" ");
    
    println!("{}", s);
}


   
   
/*
run:
   
Rust is a generalpurpose programming language emphasizes performance
   
*/

 



answered Jul 31, 2024 by avibootz

Related questions

1 answer 39 views
39 views asked Nov 23, 2024 by avibootz
2 answers 110 views
1 answer 77 views
...