valfz

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

lessons8, in order

  1. Cargo and a first program9 minCreate the tally project with Cargo, read a log file, count its requests, and learn how a compiler error message is laid out.
  2. Ownership and borrowing14 minMoves, references and slices, taught on the text of a log file, with the three borrow checker errors you are most likely to meet first.
  3. Structs, enums and pattern matching14 minModel 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.
  4. Errors as values16 minReplace Option and expect with Result, the ? operator and two small error enums, and decide which failures are skipped and which end the run.
  5. Collections and iterators15 minCount requests with HashMap and BTreeMap, rank the top paths and clients with an iterator chain, and meet lifetimes where the code needs them.
  6. Traits and generics13 minPut the report behind an output trait with text and JSON implementations, and compare generics with trait objects by running into the limits of both.
  7. A real command-line interface15 minParse arguments with clap, read files or standard input line by line through BufRead, and report failures on stderr with meaningful exit codes.
  8. Testing and shipping18 minUnit tests beside the code, integration tests that run the binary against fixtures, clippy and rustfmt, and a release build with measurements.

keyboard

h
home
w c a
writing, courses, about
n p
next and previous lesson
j k
scroll down and up
g G
top and bottom of the page
t
switch light and dark
?
this help

shortcuts are