How to count the number of words in a string with Rust

1 Answer

0 votes
fn count_words(input: &str) -> usize {
    input.split_whitespace().count()
}

fn main() {
    let str = "rust javascript php c c++";
    
    let len = count_words(str);
    
    println!("{}", len);
}



/*
run:

5

*/

 



answered Sep 20, 2024 by avibootz
edited Nov 1 by avibootz
...