rust 28 lines · 7 steps

Counting and ranking words in Rust

Tally word frequencies into a HashMap, then sort them to surface the most common terms.

Explained by highlit
1use std::collections::HashMap;
2 
3pub fn word_frequencies(text: &str) -> HashMap<String, usize> {
4 let mut counts: HashMap<String, usize> = HashMap::new();
5 
6 for word in text.split_whitespace() {
7 let normalized: String = word
8 .chars()
9 .filter(|c| c.is_alphanumeric())
10 .flat_map(|c| c.to_lowercase())
11 .collect();
12 
13 if normalized.is_empty() {
14 continue;
15 }
16 
17 *counts.entry(normalized).or_insert(0) += 1;
18 }
19 
20 counts
21}
22 
23pub fn top_words(text: &str, limit: usize) -> Vec<(String, usize)> {
24 let mut ranked: Vec<(String, usize)> = word_frequencies(text).into_iter().collect();
25 ranked.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
26 ranked.truncate(limit);
27 ranked
28}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1The entry API lets you read-or-initialize a map slot in one branchless expression.
  2. 2Normalizing tokens before counting keeps casing and punctuation from fragmenting your tallies.
  3. 3A tie-breaker comparator gives sorts a deterministic, predictable order.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Counting and ranking words in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code