Word Frequency Counter CLI
Build a Rust command-line tool that reads one or more text files and shows the words used most often, with how many times each appears. It ignores upper and lower case and punctuation, so The, the and the. all count as the same word, and it can skip common filler words like the, a and and. It's a good first Rust project: you'll learn to read files, split and clean up text, count with a HashMap, sort results, handle errors with Result, and build a friendly command line with the clap crate. Try it on a book from Project Gutenberg to see which words an author uses most.
- Estimate
- ~7.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
clap
Project roadmap
- 01
Set up the project and arguments
~1hRun cargo new wordfreq and add clap with the derive feature. Define the arguments: one or more file paths, --top N (default 10) and --stop-words. Download a book as plain text from Project Gutenberg into samples/ to test with. Done when: wordfreq --help lists the arguments.
- 02
Read and split the text
~1.5hRead each file into a String with std::fs::read_to_string, printing a clear error and skipping files that can't be read. Split the text into words: lowercase everything, treat any character that isn't a letter, a digit or an apostrophe inside a word as a separator, and ignore empty pieces. Put this in a function words(text: &str) -> Vec<String>. Done when: words("Hello, hello! It's here.") returns hello, hello, it's and here.
- 03
Count the words
~1hCount each word across all the files in a HashMap<String, usize>, using the entry API (entry(word).or_insert(0)). Also keep the total number of words and the number of different words. Done when: the counts for a small test text are correct.
- 04
Sort and print the top words
~1.5hMove the counts into a Vec, sort it by count from highest to lowest, and alphabetically when counts are equal, so the output is always the same. Print the top N as a table with the rank, word, count and percentage of all words, followed by the totals. Done when: the output is sorted, aligned and identical on every run.
- 05
Filter out stop words
~1hWith --stop-words, skip common English words (the, a, an, and, of, to, in, is, it and about 30 more) stored in a HashSet. Add --stop-file to load extra words from a file, one per line. Done when: with --stop-words, words like the and and disappear from the results.
- 06
Write tests
~1.5hWrite unit tests for words (punctuation, upper and lower case, apostrophes, numbers, empty text), for counting, for the sort order with equal counts, and for stop-word filtering. Done when: cargo test passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack