Curb Your Oxidation Play

Curb Your Oxidation

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.

Free, no signup. Keyboard, gamepad or touch.

Pixel art of a palm-lined Westside street at sunset, a silver hybrid parked at the curb

The trailer

Previously, on the Westside

Curb Your Oxidation trailer thumbnail: the title in yellow serif on black, over Sharon interrupting with error[E0382]Play the Curb Your Oxidation trailer

More on the channel: youtube.com/@dunderquest.

How an episode goes

Plan it. Rehearse it. Get interrupted.

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.

  1. Barry's legal pad open on gift.rs, with the blank in let host_gift = wine; picked out and three options below

    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.

  2. Sharon bursting in with error[E0382]: borrow of moved value

    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.

  3. The plan playing out for real: Sharon says Thanks for the Barolo while the code highlights her println! line

    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

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.

  1. Episode 1The Second Lattelet and mut
  2. Episode 2The Sample AbuserFunctions
  3. Episode 3The RegiftOwnership and moves
  4. Episode 4The LawnmowerBorrowing with &
  5. Episode 5The Last Shrimp&mut: one at a time
  6. Episode 6The MulliganStructs and methods
  7. Episode 7The MaybeEnums, match and Option
  8. Episode 8The ReceiptResult and ?
  9. Episode 9Stop and ChatVec and for loops
  10. Episode 10The HouseguestLifetimes

Episode 1

The Second Latte

Barry's Kitchen · let and mut · with Barry, Carol

Barry's Kitchen, as the episode opens

Carol I'm going to the cafe. You want a latte?

Barry One. One latte. I'm committing to one.

Carol Write it down. Last time you changed it four times.

Barry That was a different situation. That was a Tuesday.

order.rs The latte order
fn main() {
     lattes = 1;
    println!("Barry: Just {} latte.", lattes);
    lattes = 2;
    println!("Barry: Make it {}. I'm thirsty.", lattes);
}
  • Blank 1: let · let mut

Barry, thinking: If I might change my mind later, I have to say so up front.

Tap a blank to change it.
Show the answer

let mut. Then the scene plays out, one line per println!:

  1. Barry: Just 1 latte.
  2. Barry: Make it 2. I'm thirsty.

Barry explains

Values don't change unless you say so

let lattes = 1;
let mut coffees = 1;
coffees = 2;
println!("{} {}", lattes, coffees); // 1 2

prints 1 2

let gives a value a name, and that's that. let mut is a public announcement that you might change your mind later. In Rust, you have to announce it.

Barry explains

Every value has a type

let minutes: i32 = 10;
let place: &str = "the deli";
println!("{minutes} minutes to {place}");
// 10 minutes to the deli

prints 10 minutes to the deli

i32 is a whole number. &str is a bit of text. Write the type after a colon and Rust holds you to it. Names in braces get filled in.

Episode 2

The Sample Abuser

Sol's Deli · Functions · with Barry, Sol, Gil

Sol's Deli, as the episode opens

Sol That's your eleventh sample, Barry.

Barry It's a tasting. You taste. That's the whole concept.

Sol The concept is you buy something.

Gil Buy a pound of pastrami, Barry. I'm begging you.

samples.rs How many samples left?
fn samples_left(tray: i32, taken: i32) -> i32 {
    
}

fn main() {
    let left = samples_left(12, 11);
    println!("Sol: {} sample left. ONE.", left);
}
  • Blank 1: tray - taken; · tray + taken · tray - taken

Barry, thinking: The last line of a function is its answer. Unless something swallows it.

Tap a blank to change it.
Show the answer

tray - taken. Then the scene plays out, one line per println!:

  1. Sol: 1 sample left. ONE.

Barry explains

Functions

fn tip(bill: i32) -> i32 {
    bill / 5
}
println!("{}", tip(40)); // 8

prints 8

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

fn tip(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

The Greenes' Dinner Party, as the episode opens

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
fn main() {
    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!:

  1. Sharon: Thanks for the Barolo.
  2. Barry: So are we opening the Barolo?

Barry explains

Giving it away

let wine = String::from("Barolo");
let gift = wine; // the wine moves
println!("{}", 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's Driveway, as the episode opens

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
fn admire(mower: ) {
    println!("Barry: Nice {}. Very quiet.", mower);
}

fn main() {
    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!:

  1. Barry: Nice mower. Very quiet.
  2. Ted: And my mower comes home with me.

Ted explains

Borrowing

fn admire(m: &String) {
    println!("Nice {}.", m);
}
let mower = String::from("mower");
admire(&mower);
println!("Still mine: {}", mower);

prints Nice mower. Still mine: mower

& lends a value without handing it over. The function gets to look. When it's done, the owner still has it. That's what borrowing means, Barry.

Ted explains

Borrowing to change

fn sharpen(b: &mut String) {
    b.push_str("!");
}
let mut blade = String::from("blade");
sharpen(&mut blade);
println!("{}", blade); // blade!

prints blade!

&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

Club Brunch, as the episode opens

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
fn main() {
    let mut 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!:

  1. Whit: 0 shrimp left.

Whit explains

One hand on the tray

let mut 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

let mut 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

The Ninth Hole, as the episode opens

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
struct Golfer { name: String, strokes: i32 }

impl Golfer {
    fn add_stroke() {
        self.strokes += 1;
    }
}

fn main() {
    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!:

  1. Barry: 5 strokes.

Gil explains

Structs

struct Golfer { 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.

Gil explains

Methods

struct Card { strokes: i32 }
impl Card {
    fn add(&mut self) { self.strokes += 1; }
    fn show(&self) -> i32 { self.strokes }
}
let mut c = Card { strokes: 4 };
c.add();
println!("{}", c.show()); // 5

prints 5

impl gives a struct methods. &self means the method only looks. &mut self means it changes things, so the struct has to be let mut.

Episode 7

The Maybe

Le Petit Bistro · Enums, match and Option · with Barry, Philippe, Carol

Le Petit Bistro, as the episode opens

Philippe Do you have a reservation, monsieur?

Barry Sort of. I called. Someone said 'maybe'.

Philippe Maybe is not a reservation.

Barry Maybe is a soft yes. Everyone knows maybe is a soft yes.

answer.rs Was that a yes?
enum Answer { Yes, No, Maybe }

fn reply(a: Answer) {
    match a {
        Answer::Yes => println!("Philippe: Come in."),
        Answer::No => println!("Philippe: Non."),
        
    }
}

fn main() {
    reply(Answer::Maybe);
}
  • Blank 1: (nothing) · _ => println!("Philippe: Non."), · Answer::Maybe => println!("Philippe: The bar."),

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!:

  1. Philippe: The bar.

Philippe explains

Enums and match

enum Answer { 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

The Returns Desk, as the episode opens

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.

refund.rs The refund
fn refund(receipt: bool) -> Result<i32, String> {
    if receipt {
        Ok(80)
    } else {
        Err(String::from("No receipt, no refund."))
    }
}

fn main() {
    match refund() {
        Ok(cash) => println!("Dana: Here's ${}.", cash),
        Err(why) => println!("Dana: {}", why),
    }
}
  • Blank 1: "yes" · false · true

Barry, thinking: Do I have the receipt? That's a bool. true or false. Not a speech.

Tap a blank to change it.
Show the answer

true. Then the scene plays out, one line per println!:

  1. Dana: Here's $80.

Dana explains

Result

fn refund(receipt: bool) -> Result<i32, String> {
    if receipt { Ok(80) } else { Err(String::from("no")) }
}
match refund(true) {
    Ok(n) => println!("${}", n),
    Err(e) => println!("{}", e),
} // $80

prints $80

Result is for things that can fail: Ok(value) or Err(reason). Like Option, you have to deal with both.

Barry explains

Passing the blame

fn find() -> Result<i32, String> {
    Err(String::from("Carol has it"))
}
fn refund() -> Result<i32, String> {
    let r = find()?; // Err: return it now
    Ok(r)
}
println!("{:?}", refund()); // Err("Carol has it")

prints Err("Carol has it")

? after a Result means: if it's an Err, stop and hand it to whoever called me. It only works in a function that returns a Result itself.

Episode 9

Stop and Chat

The Coffee Line · Vec and for loops · with Barry, Rain, Gil, Lamont

The Coffee Line, as the episode opens

Rain Next!

Barry I'm next. But Gil stopped to chat with a guy. So who's in front, Gil or the guy?

Gil It's a stop-and-chat, Barry. It's allowed.

Barry A stop-and-chat is not a reservation. Nobody holds your place for a chat.

hello.rs Saying hello to everyone
fn main() {
    let friends = vec![
        String::from("Gil"),
        String::from("Lamont"),
    ];
    for f in  {
        println!("Barry: Hello, {}.", f);
    }
    println!("Barry: {} hellos. Done.", friends.len());
}
  • Blank 1: friends · &friends

Barry, thinking: Saying hello shouldn't use my friends up. Loop over a borrow of them.

Tap a blank to change it.
Show the answer

&friends. Then the scene plays out, one line per println!:

  1. Barry: Hello, Gil.
  2. Barry: Hello, Lamont.
  3. Barry: 2 hellos. Done.

Gil explains

Looping without taking

let friends = vec![1, 2];
for f in &friends {
    println!("{}", f);
}
println!("{}", friends.len()); // 1 2 2

prints 1 2 2

for f in friends hands each item to the loop and uses the Vec up. for f in &friends borrows each one instead, and the Vec is still there afterwards.

Rain explains

Vec

let mut line = vec![String::from("Barry")];
line.push(String::from("Gil"));
println!("{} {:?}", line.len(), line);
// 2 ["Barry", "Gil"]

prints 2 ["Barry", "Gil"]

A Vec is a list that can grow. push adds to the end, len counts, and {:?} prints the lot.

Episode 10

The Houseguest

Barry's Guest Room · Lifetimes · with Barry, Lamont

Barry's Guest Room, as the episode opens

Barry Lamont. It's been six weeks.

Lamont Six weeks of quality time, Barry.

Barry You said one night.

Lamont I said one night at a time.

longest.rs Who said what
 {
    if a.len() > b.len() { a } else { b }
}

fn main() {
    let said = String::from("one night");
    let meant = String::from("six weeks, maybe seven");
    println!("Barry: {}?!", longest(&said, &meant));
}
  • Blank 1: fn longest(a: &str, b: &str) -> &str · fn longest(a: String, b: String) -> String · fn longest<'a>(a: &'a str, b: &'a str) -> &'a str

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!:

  1. Barry: six weeks, maybe seven?!

Lamont explains

Lifetimes

fn longest<'a>(a: &'a str, b: &'a str) -> &'a str {
    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.

Controls

How to play

ActionKeyboardGamepad
Walk← / → or A / DD-pad or left stick
Talk, open a planE, Space or EnterX / 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 planEnter or RLB / L1
Close the pad, pauseEsc or PStart / 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'.

Play Curb Your Oxidation

a Dunder Quest game

Prefer Python, JavaScript, or Git?

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.