A CLI tool that analyzes text files and provides statistics including word frequency analysis.
- File I/O: Reading files from disk
- HashMap: Using collections for frequency counting
- Iterators: Processing text with iterator methods
- String Operations: Splitting, trimming, case conversion
- Path Handling: Working with
std::path::Path
- Line count
- Word count
- Character count
- Top 5 most frequent words
cargo runThen enter the path to a text file when prompted.
Enter file path:
> text.txt
Lines: 3
Words: 89
Chars: 571
Top 5 words:
the: 8
of: 6
code: 5
to: 4
a: 4
let mut top_words: HashMap<String, usize> = HashMap::new();
for word in &words {
let word = word.to_lowercase();
*top_words.entry(word).or_insert(0) += 1;
}let mut top_vec: Vec<(String, usize)> = top_words.into_iter().collect();
top_vec.sort_by(|a, b| b.1.cmp(&a.1));
for (word, count) in top_vec.iter().take(5) {
println!("{}: {}", word, count);
}fn read_file_content(path: &Path) -> String {
match fs::read_to_string(path) {
Ok(content) => content,
Err(err) => {
println!("err: {}", err);
panic!("Failed!!")
}
}
}let words = content.split_whitespace().collect::<Vec<&str>>();
let lines = content.lines().count();
let chars = content.chars().collect::<Vec<char>>().len();- Collections: Using
HashMapfor counting and aggregation - Ownership with Collections: Understanding how data moves between
HashMapandVec - Iterator Adapters: Chaining
.iter(),.take(),.collect() - Entry API: Using
.entry().or_insert()for elegant counting - Path Types: Difference between
&str,String, and&Path
- Better error handling with
Resultinstead ofpanic! - Configurable top N words
- Ignore common stop words (the, a, an, etc.)
- Support multiple file formats
- Export results to JSON/CSV
- Add unit tests
Status: ✅ Completed | Difficulty: Beginner