The borrow checker is the unwritten social contract. Learn Rust.
Barry Danvers lives by rules nobody else has heard of. Every petty dispute on the Westside, over a latte, a lawnmower or the last shrimp, is settled by a plan, and every plan is a short Rust program on his yellow legal pad. Break the rules and Sharon will let you know. At the exact line.
The way rustc reads your whole program before anything runs, Barry plays every plan through in his head first. If it breaks the rules, it never happens. If it compiles, every println! is somebody saying that line out loud.
Write the plan
Talk to someone and the dispute turns into a short Rust program on Barry's legal pad, with blanks to fill. Pick what goes in each.
Rehearse it, in sepia
Barry runs it in his head, line by line. Break a rule and Sharon bursts in at that line with the real rustc error, error[E0382] and all, and an opinion about it.
Watch it happen
When it compiles, the scene plays it out for real, and only moves on if the program prints exactly what should happen. Then a squint, a freeze frame, and the tuba.
Starring
The cast
Barry Danvers
Semi-retired TV writer on the Westside. Lives by rules nobody else has heard of, and holds everyone to them.
Gil Greene
Barry's manager and best friend. Endlessly patient, which Barry tests daily.
Sharon Greene
Gil's wife, and the borrow checker. When a plan breaks the rules, she bursts in at the exact line, with rustc's own error and an opinion about it.
Carol
Barry's ex-wife, somehow still on speaking terms. Counts his minutes.
Lamont
The houseguest who came for one night. It's been six weeks.
Ted Pruitt
The neighbor whose lawnmower has been in Barry's garage for six weeks.
Also appearing: Sol at the deli, who counts the samples; Whit Thornbury, who manages the country club; Philippe, maître d' of a bistro Barry can't get into; Dana at returns, who has heard it all; and Rain, the barista.
The season
Ten episodes, ten pieces of Rust
Each episode is one petty dispute and one idea, and they build on each other: from let mut to ownership, borrowing and lifetimes. Every episode below has one of its plans on a pad to try. It opens on Barry's first idea, which is wrong.
fn makes a function. Its parameters each have a type, and -> says what it hands back. The last line, with no semicolon, is the answer.
Sol explains
The semicolon swallows it
fntip(bill: i32) -> i32 {
bill / 5;
}
error[E0308]
Put a semicolon on the last line and it's a statement, not an answer. Now the function hands back nothing, which is not an i32. Rustc tells you to remove the semicolon. Listen to it.
Episode 3
The Regift
The Greenes' Dinner Party · Ownership and moves · with Barry, Sharon, Gil
Sharon Barry. You brought wine. How thoughtful.
Barry A Barolo. A good one. It's a gift, but it's a gift with a future.
Gil What's a gift with a future?
Barry If we don't open it tonight, it comes home with me. That's the etiquette.
Sharon That is NOT the etiquette.
gift.rs The host gift
fnmain() {
let wine = String::from("Barolo");
let host_gift = ;
println!("Sharon: Thanks for the {}.", host_gift);
println!("Barry: So are we opening the {}?", wine);
}
Blank 1:wine · &wine · wine.clone()
Barry, thinking: Once I hand it over, it's hers. Unless I hand over something else.
Tap a blank to change it.
Show the answer
wine.clone(). Then the scene plays out, one line per println!:
Sharon: Thanks for the Barolo.
Barry: So are we opening the Barolo?
Barry explains
Giving it away
let wine = String::from("Barolo");
let gift = wine; // the wine movesprintln!("{}", gift); // Barolo
prints Barolo
A String has one owner. let gift = wine; doesn't copy it, it hands it over. After that wine is empty-handed, and using it is error E0382.
Gil explains
Buy your own
let wine = String::from("Barolo");
let gift = wine.clone();
println!("{} {}", wine, gift); // Barolo Barolo
prints Barolo Barolo
clone() makes a second one. Two bottles, two owners, nobody goes without. It costs a little more. Manners usually do.
Episode 4
The Lawnmower
Ted's Driveway · Borrowing with & · with Barry, Ted
Ted Barry. My mower's been in your garage for six weeks.
Barry I'm borrowing it. A borrow has no fixed term. It's open-ended.
Ted A borrow ends when the owner says it ends.
Barry Where is that written? Show me where that's written.
admire.rs Just looking
fnadmire(mower: ) {
println!("Barry: Nice {}. Very quiet.", mower);
}
fnmain() {
let mower = String::from("mower");
admire();
println!("Ted: And my {} comes home with me.", mower);
}
Blank 1:String · &String
Blank 2:mower · &mower
Barry, thinking: I only want to look at it. Looking is a borrow. Both of us have to agree it's a borrow.
Tap a blank to change it.
Show the answer
&String and &mower. Then the scene plays out, one line per println!:
&mut lends a value so the borrower may change it. The owner has to be let mut and has to say &mut when lending. Everybody agrees, in writing.
Episode 5
The Last Shrimp
Club Brunch · &mut: one at a time · with Barry, Gil, Whit
Whit Gentlemen. Two shrimp left. Club rules: one hand on the tray at a time.
Gil I saw them first, Barry.
Barry You saw. I reached. Reaching beats seeing. Everybody knows that.
Gil Nobody knows that.
shrimp.rs Two hands, one tray
fnmain() {
letmut tray = vec!["shrimp", "shrimp"];
let barry = &mut tray;
let gil = &mut tray;
println!("Whit: {} shrimp left.", tray.len());
}
Blank 1:// (Barry hovers) · barry.pop();
Blank 2:barry.pop(); · gil.pop();
Barry, thinking: One &mut at a time. I take mine and I'm DONE before Gil reaches in.
Tap a blank to change it.
Show the answer
barry.pop(); and gil.pop();. Then the scene plays out, one line per println!:
Whit: 0 shrimp left.
Whit explains
One hand on the tray
letmut tray = vec![1, 2];
let a = &mut tray;
a.push(3);
let b = &mut tray; // fine: a is done
b.push(4);
println!("{:?}", tray); // [1, 2, 3, 4]
prints [1, 2, 3, 4]
One &mut borrow at a time, or any number of & borrows, never both. A borrow ends at its last use, so finish with one before the next one starts.
Whit explains
No clearing while someone's looking
letmut tray = vec!["shrimp"];
let last = &tray[0];
tray.clear();
println!("{}", last);
error[E0502]
last is still looking at the tray when clear() wants to change it: error E0502. Move the println! up, and the looking is over before the clearing starts.
Episode 6
The Mulligan
The Ninth Hole · Structs and methods · with Barry, Gil, Whit
Gil That's five, Barry. You took five.
Barry Four. The first one was a practice swing that happened to hit the ball.
Gil That's called a stroke.
Barry That's called a technicality.
scorecard.rs The scorecard
structGolfer { name: String, strokes: i32 }
implGolfer {
fnadd_stroke() {
self.strokes += 1;
}
}
fnmain() {
let name = String::from("Barry");
let barry = Golfer { name, strokes: 4 };
barry.add_stroke();
println!("{}: {} strokes.", barry.name, barry.strokes);
}
Blank 1:&self · self · &mut self
Blank 2:(nothing) · mut
Barry, thinking: Adding a stroke changes the card. The method needs &mut self, and the golfer has to be let mut.
Tap a blank to change it.
Show the answer
&mut self and mut . Then the scene plays out, one line per println!:
Barry: 5 strokes.
Gil explains
Structs
structGolfer { name: String, strokes: i32 }
let g = Golfer {
name: String::from("Gil"),
strokes: 3,
};
println!("{} {}", g.name, g.strokes); // Gil 3
prints Gil 3
A struct bundles named fields into one thing. Make one by filling in every field. Read a field with a dot.
Barry, thinking: Yes, no, maybe. A match has to say what happens for every one of them.
Tap a blank to change it.
Show the answer
Answer::Maybe => println!("Philippe: The bar."),. Then the scene plays out, one line per println!:
Philippe: The bar.
Philippe explains
Enums and match
enumAnswer { Yes, No, Maybe }
let a = Answer::Maybe;
match a {
Answer::Yes => println!("yes"),
Answer::No => println!("no"),
Answer::Maybe => println!("maybe"),
} // maybe
prints maybe
An enum lists the only values there can be. match picks an arm for each, and it must cover them all, or it's error E0004. _ covers whatever's left.
Carol explains
Was that a yes?
let seats: Option<i32> = Some(2);
match seats {
Some(n) => println!("party of {}", n),
None => println!("nobody"),
} // party of 2
prints party of 2
Rust has no null. A value that might not be there is an Option: Some(value) or None. To get the value out, you have to say what happens if it's None.
Episode 8
The Receipt
The Returns Desk · Result and ? · with Barry, Dana
Dana Do you have the receipt?
Barry I have the sweater. The sweater is the receipt. Look at it. It's clearly yours.
Dana No receipt, no refund.
Barry Who made that rule? Was there a vote? I wasn't told about a vote.
Barry, thinking: The answer is borrowed from one of them. Which one, nobody knows, so they have to share a lifetime.
Tap a blank to change it.
Show the answer
fn longest<'a>(a: &'a str, b: &'a str) -> &'a str. Then the scene plays out, one line per println!:
Barry: six weeks, maybe seven?!
Lamont explains
Lifetimes
fn longest<'a>(a: &'astr, b: &'astr) -> &'astr {
if a.len() > b.len() { a } else { b }
}
println!("{}", longest("a night", "six weeks"));
// six weeks
prints six weeks
'a is a lifetime: a name for how long a borrow lasts. Here the answer lives as long as both inputs do. Rustc asks for one when a function hands back a borrow and can't tell where from.
Barry explains
Borrows can't outlive the owner
let key;
{
let spare = String::from("key");
key = &spare;
}
println!("{}", key);
error[E0597]
spare is dropped at the closing brace, so a borrow of it can't be used after that: E0597, doesn't live long enough. Own the thing instead: move it or clone it.
20 lesson cards · 20 plans · every one compiled with the real rustc
The rulebook
rustc, as Sharon puts it
The error codes are the compiler's own. The delivery is Sharon's.
error[E0384]You said it once and now you're changing it? Then you say `mut`! Up front!
error[E0308]Those types don't match, Barry! It says one thing and you hand it another!
error[E0061]Wrong number of arguments! Count them! It's right there in the signature!
error[E0382]You gave it away, Barry! It MOVED! You don't get to use something after you've given it away! #@%&!
error[E0596]You can't change something you only borrowed to look at! It has to be `mut`!
error[E0499]Two hands on one thing! One mutable borrow at a time, Barry! ONE!
error[E0502]Somebody's still LOOKING at it and you're changing it? Wait till they're done!
error[E0594]You can't write on something that isn't yours to change! #@%&!
error[E0063]You left out a field! You can't just leave things out because you feel like it!
error[E0004]You didn't cover every case! What if it's the other one, Barry? WHAT THEN?
error[E0277]That doesn't work with that! It doesn't have what it takes, and neither do you!
error[E0106]It returns a borrow, but borrowed from WHAT? Say how long it lives!
error[E0597]That borrow outlived the thing it borrowed! The owner's GONE, Barry!
Controls
How to play
Action
Keyboard
Gamepad
Walk
← / → or A / D
D-pad or left stick
Talk, open a plan
E, Space or Enter
X / Square or A / Cross
Pick a blank on the pad
← / →
D-pad left and right
Change the blank
↑ / ↓
D-pad up and down
Run the plan
Enter or R
LB / L1
Close the pad, pause
Esc or P
Start / Options
On a phone or tablet, turn it sideways and tap: tap someone to talk, tap a blank to change it. Your progress saves in this browser, separately from the other games'.
Curb Your Oxidation is the third Dunder Quest game. Dunder Quest teaches Python on a quest for the Grail, Flatmates.js teaches JavaScript in a very small Croydon flat, and Prince of Versions teaches Git with a dagger that turns back time.
Curb Your Oxidation is an homage to a certain LA cringe-comedy series. The names, lines and music are original, not the show's own, and it isn't affiliated with or endorsed by anyone who made it.