How to get the first letter of each word in a string with Rust

1 Answer

0 votes
fn get_first_letters(s: &str) -> String {
    let first_letters: String = s
        .split_whitespace()
        .filter_map(|word| word.chars().next())
        .collect();
    
    first_letters
}

fn main() {
    let str = "rust javascript php c typescript node.js";
    
    println!("{}", get_first_letters(str));
}




  
/*
run:

rjpctn

*/

 



answered Sep 19, 2024 by avibootz
...