Created
July 10, 2019 03:59
-
-
Save tahmidsadik/8ca641ac003fec1abbcb22676850d1e6 to your computer and use it in GitHub Desktop.
Serial word counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
use std::path::{Path, PathBuf}; | |
use std::collections::HashMap; | |
use std::fs::read_to_string; | |
fn main() -> Result<(), std::io::Error> { | |
let mut word_counts = HashMap::new(); | |
let mut files: Vec<PathBuf> = vec!(); | |
for entry in fs::read_dir("/mnt/c/Users/tahmi/source/repos/shopup-lite/server/controllers")? { | |
let p = entry?.path(); | |
if(!p.is_dir()) { | |
files.push(p); | |
} | |
}; | |
println!("files count = {}", files.len()); | |
for p in files { | |
count_words(&mut word_counts, read_to_string(p)?.split(" ").collect::<Vec<&str>>()); | |
} | |
// let content = fs::read_to_string("/mnt/c/Users/tahmi/source/repos/shopup-lite/")?; | |
// let words = content.split(" "); | |
// for word in words { | |
// *word_counts.entry(word).or_insert(0) += 1; | |
// } | |
for (word, count) in word_counts { | |
if(count > 10) { | |
println!("{} {}", count, word); | |
} | |
} | |
Ok(()) | |
} | |
fn count_words(wc: &mut HashMap<String, i32>, content: Vec<&str>) { | |
for word in content { | |
*wc.entry(word.to_owned()).or_insert(0) += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment