Rust: Build a Log Triage Tool
Learn Rust by building triage, a real log-analysis CLI: ownership and borrowing, structs and enums, Result and the ? operator, iterators, HashMap, generics, traits, lifetimes, modules and tests — one small step at a time, with every compiler message read and understood.
Hello-world programs teach you syntax and nothing else. This course teaches Rust by building a tool you could genuinely use tomorrow: triage, a command-line log analyzer that answers the first questions of any incident — how many errors, which ones, since when, and what was slow. Every concept arrives exactly when the program needs it: ownership when a loop eats your Vec, enums when strings stop being enough, lifetimes when the compiler genuinely cannot guess. Twice we walk straight into a compiler error on purpose, read it line by line, and fix it — because reading rustc's advice is half of writing Rust.
What you'll build
- Read and write ownership: moves, borrows, and when to clone
- Model data with structs and enums, and take them apart with match
- Handle failure with Option, Result, ? and your own error type
- Process data with iterators, closures, Vec and HashMap
- Write generic functions with trait bounds and implement std traits
- Understand lifetimes where elision ends, split code into modules, and test it
Contents
- A new Cargo project
- Hello, world — for a minute
- Run it
- Read the arguments
- Arguments, seen by the program
- Pick out the file path
- What happens with no argument
- The sample log
- Read the whole file
- unwrap on a missing file
- Match on the Result
- A clean failure
- Extract run and meet ?
- Same behaviour, tidier shape
- Count the lines
- Forty-five lines of afternoon
- Split the first line
- The first entry, dissected
- A shape for one entry
- Parse one line
- Show what we parsed
- One line, structured
- Feed it junk
- Junk becomes None
- A type for levels
- From text to Level
- The entry carries a Level
- Typed levels, silent failures
- if let in main
- Name what can go wrong
- Errors that explain themselves
- Parse returns Result
- Errors with reasons
- Collect every entry
- The whole file, parsed
- Count the errors
- borrow of moved value
- Borrow, don't move
- Errors counted
- Count every level at once
- Trait bounds were not satisfied
- Derive Eq and Hash
- Levels tallied
- Group the error messages
- Rank the top offenders
- Top offenders
- One generic counter
- Same numbers, less code
- FromStr, the standard way
- Display for Level
- A tidy summary
- Which service is failing
- Counts by service
- When did it start
- First seen
- A parser module
- A stats module
- Same tool, clean layout
- Two reports, two functions
- Dispatch on a command
- Subcommands and usage
- Reading durations
- The slow command
- Slow requests
- Test the parser
- cargo test
- Test the stats
- The whole suite
- The release build