Advanced~3h

Rust: Ownership

Does this compile, and why not

Most Rust teaching hands you a broken program and asks you to repair it, so you never have to decide whether it was broken. This is the other question: six lines you have not seen, a verdict, and the rule that settles it. Half the programs here build, so guessing no is worse than reading.

Modules
5
Drills
10
Build steps
0
Time
~3h

Walk out able to

Read an unfamiliar Rust function and say whether it compiles — and when it does not, name the rule and the one edit that clears it.

What you judge

01

Moves and copies

Which assignments hand the value over, and which copy it.

02

Borrows that overlap

The exclusivity rule, and where a borrow actually ends.

03

Lifetimes and slices

References that outlive, and views over a live collection.

04

Diagnostics and repairs

Spot the broken line; pick the edit that clears it.

The pitch

What you practise, and what you leave with

Not an artifact — a reflex. Six lines of unfamiliar Rust, a verdict, and the rule behind it, before you reach for the compiler.

You will practise

  • Moves, copies and partial moves
  • Shared and mutable borrows, and where they end
  • Lifetimes, dangling returns and elision
  • Slices, syntax recall and the smallest repair

Afterwards you can

  • Say whether a function compiles, cold
  • Name the rule that broke, in words
  • Find the line the error points at
  • Pick the edit that actually clears it

Modules

5 modules, 17 items

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

  1. 1

    Who owns it, and when it moves

    ~38 min

    Name the owner at any line, and say whether an assignment moved the value or copied it.

    • LessonWhy call it before the compiler5 min
    • LessonDecide which line drops the value8 min
    • LessonSay which types duplicate instead5 min
    • DrillRead the type, call the assignment9 min
    • DrillFind the move you cannot see11 min
  2. 2

    Borrow instead of taking

    ~23 min

    Judge overlapping borrows, and find the line where each one actually ends.

    • DrillName the borrowing rule that broke12 min
    • DrillFind where the borrow really ends11 min
  3. 3

    How long a reference may live

    ~22 min

    Catch a reference that outlives its value, and say when a signature needs a lifetime.

    • DrillCatch the reference that outlives12 min
    • DrillSay whether it needs a lifetime10 min
  4. 4

    Slices over a live collection

    ~26 min

    Spot a slice held across a change, and choose the parameter type a function wants.

    • DrillHold a slice across a change11 min
    • LessonAsk for the least the body needs7 min
    • LessonThen take the widest type8 min
  5. 5

    Read the error, name the rule

    ~47 min

    Spot the broken line, read the diagnostic it earns, and pick the edit that clears it.

    • DrillSpell it, and spot the broken line8 min
    • DrillFind the line the error points at11 min
    • DrillChoose the edit that compiles12 min
    • LessonCall three programs cold10 min
    • LessonWrite five failures on purpose6 min

Call it before the compiler does.

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

Advanced~3h5 modules