course
Rust from the command line
Learn Rust by building tally, a command-line tool that summarizes web server access logs. For developers who already know another language.
- topic
- rust
- level
- introductory
- lessons
- 8
- read
- 1 h 54 min
This course teaches Rust to developers who already write software in another language. It does not explain what a loop or a function is. It spends its time on what Rust does differently: ownership and borrowing, enums that carry data, errors as ordinary values, traits and generics. Each topic appears when the program being built needs it.
That program is tally. It reads web server access logs in the Common or Combined Log Format, from files or from standard input, and prints a summary: requests per status class, the most requested paths, the busiest client addresses and the total bytes sent. Output is plain text or JSON. The last two lessons give it a proper command-line interface and a test suite.
All code was written and run with Rust 1.99.1 and Cargo 1.99.1, on the 2024 edition that cargo new selects. If you do not have the toolchain yet, install it with rustup. The program ends up with three dependencies: serde 1.0.229 and serde_json 1.0.154 from lesson 6, and clap 4.6.10 from lesson 7. Lesson 4 also discusses thiserror 2.0.21 and anyhow 1.0.105 without adding them. With newer versions, expect small differences in the wording of compiler and help messages.
To follow along, type the code as you go. Each lesson starts from the state the previous one left behind and ends with a program that compiles and runs. The compiler errors shown are real output, provoked on purpose, because reading them is a large part of writing Rust. Every lesson closes with exercises that extend the program; later lessons do not depend on your solutions.
What you build
tally, a command-line tool that reads web server access logs from files or standard input and prints a summary as text or JSON.
Before you start
- You program comfortably in another language, such as JavaScript, Python or Go
- You can work in a terminal and have a text editor you like
- No Rust experience is assumed
After the last lesson you can
- Read a borrow checker error and decide whether to borrow, copy or restructure
- Model data with structs and enums and take it apart with pattern matching
- Return errors as values with Result, the ? operator and your own error types
- Aggregate data with HashMap, Vec and iterator chains
- Choose between generics and trait objects and say what each one costs
- Build, test, lint and release a command-line program with Cargo
lessons
- Cargo and a first programCreate the tally project with Cargo, read a log file, count its requests, and learn how a compiler error message is laid out.
- Ownership and borrowingMoves, references and slices, taught on the text of a log file, with the three borrow checker errors you are most likely to meet first.
- Structs, enums and pattern matchingModel a parsed log line with a struct and an enum, parse it by hand with match, if let and let else, then move the code into a module.
- Errors as valuesReplace Option and expect with Result, the ? operator and two small error enums, and decide which failures are skipped and which end the run.
- Collections and iteratorsCount requests with HashMap and BTreeMap, rank the top paths and clients with an iterator chain, and meet lifetimes where the code needs them.
- Traits and genericsPut the report behind an output trait with text and JSON implementations, and compare generics with trait objects by running into the limits of both.
- A real command-line interfaceParse arguments with clap, read files or standard input line by line through BufRead, and report failures on stderr with meaningful exit codes.
- Testing and shippingUnit tests beside the code, integration tests that run the binary against fixtures, clippy and rustfmt, and a release build with measurements.