RustIntermediate~4h

Rust: Types & Traits

Make the wrong program unwriteable

Most Rust is not about the borrow checker. It is about choosing the enum that cannot hold the bad value, carrying a failure as a value instead of throwing it, and naming the one ability a function actually needs — then reading code somebody else wrote and knowing what it does.

Modules
6
Drills
10
Build steps
5
Time
~4h

Walk out able to

Read unfamiliar Rust and say what it produces — and choose between a generic parameter and a `dyn` without guessing which one the design needs.

What you work on

01

Enums that refuse bad states

Variants that differ, and a match that has to be total.

02

Option, Result and ?

Absence and failure carried as values, never thrown.

03

Generics and traits

One bound per ability — then generic or dyn, on purpose.

04

Pipelines, then the build

Adapters instead of loops, and a Rust program that runs.

The pitch

What you practise, and what you leave with

A Rust program that parses a log into an enum, checks it behind one trait, and reports through iterator pipelines.

You will practise

  • Enum shape, match arms, exhaustiveness
  • Option, Result, combinators and ?
  • Trait bounds, dyn, object safety
  • Iterator chains and the three iters

Afterwards you can

  • Pick the enum that refuses the bad value
  • Carry a failure out with ? and no panic
  • Name the bound a rejected line asked for
  • Choose a generic or a dyn, and say why

Modules

6 modules, 20 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    Make the states explicit

    ~40 min

    Choose the type that refuses the bad value, then read any match over it.

    • LessonGive the states one type17 min
    • DrillChoose the definition that refuses it12 min
    • DrillSay which arm runs11 min
  2. 2

    Absence and failure are values

    ~39 min

    Carry a missing or failed result through a program without unwrapping it.

    • LessonCarry the failure instead of throwing it16 min
    • DrillSay what the chain evaluates to12 min
    • DrillFollow a failure out of a function11 min
  3. 3

    One function, many types

    ~25 min

    Write a generic function, bound it, and read the error when the bound is missing.

    • LessonWrite it once, for every type13 min
    • DrillName the bound the body needs12 min
  4. 4

    Behaviour as a contract

    ~40 min

    Name a behaviour, require it, and choose how the caller carries it.

    • LessonName the behaviour, then require it16 min
    • DrillPick the one the design needs13 min
    • DrillSay whether it can be a trait object11 min
  5. 5

    Pipelines instead of loops

    ~36 min

    Predict what an adapter chain produces, and start it with the right one of three.

    • LessonSay what you want, not how to loop14 min
    • DrillSay what the pipeline produces13 min
    • DrillStart the loop with the right one9 min
  6. 6

    Build the reader

    ~50 min

    Get the spellings under your fingers, then ship a Rust program that uses all five.

    • DrillWrite the syntax from memory9 min
    • Build stepModel the log as one enum5 min
    • Build stepFill the type from the text7 min
    • Build stepBreak it with one bad number4 min
    • Build stepPut the checks behind one trait13 min
    • Build stepFinish it with one pipeline12 min

The borrow checker is settled. Now shape the types.

The first item is free. ~4h of focused work, at your own pace.

Intermediate~4h6 modules