Reading, filtering, sorting and writing CSV files in Rust

This post builds a small Rust program that reads a CSV file, keeps only the rows above an age threshold, sorts them by age, and writes them to a new CSV file. It uses the csv crate for parsing and writing, and serde to map each row onto a struct so you work with typed fields instead of strings.
Dependencies
Section titled “Dependencies”cargo new csv_examplecd csv_exampleAdd to Cargo.toml:
[dependencies]csv = "1.4"serde = { version = "1", features = ["derive"] }Input data
Section titled “Input data”Save this as data.csv in the project root (next to Cargo.toml, since cargo run uses that as the working directory):
id,name,age1,Alice,302,Bob,253,Carol,274,David,225,Eve,35The program
Section titled “The program”Here is the whole src/main.rs. The sections below walk through each function.
use std::error::Error;use std::path::Path;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]struct Person { id: u32, name: String, age: u32,}
fn read_csv<P: AsRef<Path>>(path: P) -> Result<Vec<Person>, Box<dyn Error>> { let mut reader = csv::Reader::from_path(path)?; let mut persons = Vec::new();
for result in reader.deserialize() { let person: Person = result?; persons.push(person); }
Ok(persons)}
fn filter_and_sort(persons: Vec<Person>, age_threshold: u32) -> Vec<Person> { let mut filtered: Vec<Person> = persons .into_iter() .filter(|person| person.age > age_threshold) .collect();
filtered.sort_by_key(|person| person.age); filtered}
fn write_csv<P: AsRef<Path>>(persons: &[Person], path: P) -> Result<(), Box<dyn Error>> { let mut writer = csv::Writer::from_path(path)?;
for person in persons { writer.serialize(person)?; }
writer.flush()?; Ok(())}
fn main() -> Result<(), Box<dyn Error>> { let age_threshold = 25;
let persons = read_csv("data.csv")?; let filtered = filter_and_sort(persons, age_threshold); write_csv(&filtered, "output.csv")?;
println!("Wrote {} rows to output.csv", filtered.len()); Ok(())}Reading
Section titled “Reading”csv::Reader::from_path opens the file and treats the first row as headers by default. reader.deserialize() returns an iterator of Result<Person, csv::Error>, matching columns to struct fields by header name, so the column order in the file doesn’t have to match the struct. A row with a non-numeric age produces an error that names the line, and ? passes it up to main.
If your file has no header row, build the reader with csv::ReaderBuilder::new().has_headers(false).from_path(path) and fields are matched by position instead.
Filtering and sorting
Section titled “Filtering and sorting”filter_and_sort takes the Vec by value and uses into_iter(), so the kept rows are moved rather than cloned. sort_by_key is a stable sort, so people with the same age keep their original order. To sort by something that isn’t Copy, such as the name, use sort_by(|a, b| a.name.cmp(&b.name)) to avoid cloning each key.
Writing
Section titled “Writing”csv::Writer::from_path creates (or truncates) the output file. Because Person derives Serialize, writer.serialize writes a header row automatically before the first record. The writer buffers its output, so call flush() at the end. Dropping it also flushes, but any error during that flush is silently ignored.
Result
Section titled “Result”With a threshold of 25, cargo run writes this output.csv:
id,name,age3,Carol,271,Alice,305,Eve,35- This loads the whole file into memory, which is fine for files up to a few hundred MB. For larger files, filter while reading and write each matching row immediately. Sorting still needs all the kept rows in memory, though.
- For anything more than filter-and-sort (joins, group-by, aggregations), a dataframe library like Polars will save you code.
- The csv crate tutorial covers delimiters, quoting, and borrowing records to avoid allocations.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)