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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1The entry API lets you read-or-initialize a map slot in one branchless expression.
- 2Normalizing tokens before counting keeps casing and punctuation from fragmenting your tallies.
- 3A tie-breaker comparator gives sorts a deterministic, predictable order.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
rust
use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex}; use std::time::{Duration, Instant};
Building a counting semaphore in Rust
concurrency
synchronization
condition-variable
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/counting-and-ranking-words-in-rust-explained-rust-11f0/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.