How to count only the unique words from a string in Rust

1 Answer

0 votes
use std::collections::HashSet;

fn main() {
    let str = "rust c c++ java c++ rust java go rust rust";
    
    let words: HashSet<&str> = str.split(' ').collect();
    
    let total_unique_words = words.len();

    println!("{}", total_unique_words);
}



  
/*
run:

5

*/

 



answered Sep 20, 2024 by avibootz
...