rust 19 lines · 5 steps

Filtering duplicate lines in Rust

A streaming uniq that prints each line only the first time it appears, backed by a HashSet.

Explained by highlit
1use std::collections::HashSet;
2use std::io::{self, BufRead, BufWriter, Write};
3 
4fn main() -> io::Result<()> {
5 let stdin = io::stdin();
6 let stdout = io::stdout();
7 let mut out = BufWriter::new(stdout.lock());
8 
9 let mut seen: HashSet<String> = HashSet::new();
10 
11 for line in stdin.lock().lines() {
12 let line = line?;
13 if seen.insert(line.clone()) {
14 writeln!(out, "{}", line)?;
15 }
16 }
17 
18 out.flush()
19}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A HashSet gives O(1) membership tests, and its insert return value tells you whether the value was new.
  2. 2Wrapping stdout in a BufWriter batches writes so per-line output doesn't hammer the OS.
  3. 3Returning io::Result from main lets the ? operator propagate I/O errors without manual match arms.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Filtering duplicate lines in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code