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
Moves and copies
Which assignments hand the value over, and which copy it.
Borrows that overlap
The exclusivity rule, and where a borrow actually ends.
Lifetimes and slices
References that outlive, and views over a live collection.
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
Who owns it, and when it moves
~38 minName 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
Borrow instead of taking
~23 minJudge 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
How long a reference may live
~22 minCatch 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
Slices over a live collection
~26 minSpot 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
Read the error, name the rule
~47 minSpot 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
More in Languages
Keep going
Python Foundations
Read a program before you run it. Then write one worth showing.
Foundational~6h8 modules
Python Fluency
Choose the shape before you type. Then say it in one line.
Intermediate~5h7 modules
Python Confidence
Somebody else wrote it. Find out whether it works.
Advanced~7h7 modules
Python Craft
It works. Now make it somebody else's.
Advanced~10h7 modules
Call it before the compiler does.
The first item is free. ~3h of focused work, at your own pace.
Advanced~3h5 modules