A grumpy crab customs officer stamping your code

The Borrow Checker Isn't Mad at You

Every TypeScript or Java developer who tries Rust has the same first week. You write something perfectly reasonable, the compiler says no, you rephrase it, the compiler says no again but in more words, and somewhere around Thursday you start referring to the borrow checker as "he" and taking it personally.

I've been there. I'm writing this from the other side, and I'm here to tell you something annoying: it was right. Not "technically right in an academic way." Right in the "you have shipped this exact bug before" way.

So let's do it the other way round. Instead of explaining ownership from first principles, here are three bugs you've almost certainly put into production in a garbage collected language, and what Rust says when you try to write them.

The rules, quickly

There are only three, and they fit on a napkin:

  1. Every value has exactly one owner.
  2. When the owner goes away, the value goes away.
  3. You can have either any number of read-only references (&T) or exactly one mutable reference (&mut T) to a value. Never both at the same time.

That's it. Everything below is just these three rules catching someone in the act.

Bug 1: two people own the shopping cart

Here's some TypeScript that looks fine and isn't:

function checkout(cart: Cart) {
  pendingOrders.push(cart); // charged later, by a job
}

checkout(cart);
cart.items.push("regret"); // also modifies the order you just placed

checkout kept a reference to the cart. The caller also still has one. They're the same object, so the "regret" you added afterwards quietly ends up in an order that was supposedly already placed. Nobody wrote anything wrong on any single line. The bug lives in the gap between two lines written by two different people, which is where all the good bugs live.

The Rust version:

struct Cart {
    items: Vec<String>,
}

fn checkout(cart: Cart) {
    println!("Charging for {} items", cart.items.len());
}

fn main() {
    let mut cart = Cart { items: vec!["coffee".into(), "more coffee".into()] };
    checkout(cart);
    cart.items.push("regret".into());
}

And the compiler, unimpressed:

error[E0382]: borrow of moved value: `cart`
  --> move.rs:12:5
   |
10 |     let mut cart = Cart { items: vec!["coffee".into(), "more coffee".into()] };
   |         -------- move occurs because `cart` has type `Cart`, which does not implement the `Copy` trait
11 |     checkout(cart);
   |              ---- value moved here
12 |     cart.items.push("regret".into());
   |     ^^^^^^^^^^ value borrowed here after move

checkout(cart: Cart) takes ownership. Rule 1: one owner. Once you've handed the cart over, it's not yours anymore, and the compiler won't let you keep fiddling with something you gave away. If checkout only needs to look at it, say so in the signature:

fn checkout(cart: &Cart) {
    println!("Charging for {} items", cart.items.len());
}

checkout(&cart);
cart.items.push("regret".into()); // fine, checkout is done looking

Notice what happened there. The question "does this function keep my data or just look at it?" used to be answered by reading the function body, or a comment, or by finding out in production. Now it's in the signature, and it's enforced. Cart means "I'm taking this." &Cart means "I'm borrowing it and I'll give it back." &mut Cart means "I'm borrowing it and I'll change it." You can read a Rust function signature and know who owns what afterwards, which is more than I can say about most Java codebases I've worked in.

Bug 2: editing the list while you walk it

The Java version of this one has a name, and the name is ConcurrentModificationException, which is funny, because there is nothing concurrent about it. One thread, one loop:

for (Job job : queue) {
    if (job.needsRetry()) {
        queue.add(job.retry()); // boom, at runtime, if you're lucky
    }
}

"If you're lucky" because Java's check is best effort. JavaScript doesn't even try. Push to an array inside a for...of over that same array and the loop happily picks up the new items too, so a retry that always needs retrying gives you an infinite loop and a very warm laptop.

Rust:

fn main() {
    let mut queue = vec![1, 2, 3];
    for job in &queue {
        if *job == 2 {
            queue.push(4);
        }
    }
}
error[E0502]: cannot borrow `queue` as mutable because it is also borrowed as immutable
 --> iter.rs:5:13
  |
3 |     for job in &queue {
  |                ------
  |                |
  |                immutable borrow occurs here
  |                immutable borrow later used here
4 |         if *job == 2 {
5 |             queue.push(4);
  |             ^^^^^^^^^^^^^ mutable borrow occurs here

Rule 3. The loop holds a read-only borrow of queue for as long as it runs. push wants a mutable one. You can't have both, so it doesn't compile. Not "throws sometimes." Doesn't compile. Ever.

And in Rust this isn't just a logic bug. push can reallocate the vector's memory, which would leave the loop reading from wherever the old memory used to be. In C++ that's called iterator invalidation and it's the kind of thing people write CVEs about. In Rust it's a compile error with ASCII art.

The fix is to stop pretending it's one operation. Read first, then write:

let extra: Vec<i32> = queue.iter().filter(|job| **job == 2).map(|_| 4).collect();
queue.extend(extra);

Two steps, and anyone reading it can see exactly when the list changes.

Bug 3: the reference that outlives its data

This is the one garbage collected languages genuinely don't have, and it's worth being honest about that. In Java, if you hold on to something, the GC keeps it alive. That's the deal you signed. It costs you a GC, but nothing ever points at freed memory.

Rust doesn't have a GC, so it needs another way to make sure that never happens:

struct Config<'a> {
    name: &'a str,
}

fn load() -> Config<'static> {
    let raw = String::from("prod");
    Config { name: &raw }
}
error[E0515]: cannot return value referencing local variable `raw`
 --> dangle.rs:7:5
  |
7 |     Config { name: &raw }
  |     ^^^^^^^^^^^^^^^----^^
  |     |              |
  |     |              `raw` is borrowed here
  |     returns a value referencing data owned by the current function

Rule 2. raw is owned by load, so it goes away when load returns, and you tried to hand out a reference to it on the way out. In C this compiles, runs, and works perfectly right up until it doesn't. Here it's an error that names the variable and points at the line.

That 'a is a lifetime, by the way, the bit everyone is scared of. It's not magic. It's just the compiler asking "this reference points into something. How long does that something live?" Most of the time it works that out on its own and you never write one. When it can't, it asks you.

The fix, as usual, is to be clear about who owns the thing:

struct Config {
    name: String,
}

fn load() -> Config {
    let raw = String::from("prod");
    Config { name: raw } // ownership moves into the Config
}

If you've ever had to explain to a junior why a struct full of borrowed references is a bad idea, congratulations, you've been doing the borrow checker's job by hand.

When you're losing the fight

Sometimes you'll be fighting the compiler and it's not that the code is buggy, it's that you're not sure who should own what. A few honest tips from someone who lost that fight for longer than I'd like to admit:

  • .clone() is allowed. Copying a small String to get past a borrow error isn't a moral failing. Make it work, then make it fast. Most of those clones will never show up in a profiler.
  • Prefer owned data in structs. String over &str, Vec<T> over &[T]. Borrowed references inside structs are where most of the lifetime pain comes from, and you rarely need them.
  • Shared ownership exists. If something really does have several owners, Rc<RefCell<T>> (one thread) or Arc<Mutex<T>> (several) let you say so explicitly. That's also where the concurrency post picks up.
  • If you need all three at once, step back. When the design keeps asking for a graph of things that all point at each other and all mutate each other, the borrow checker isn't the problem. That design would be a bug factory in TypeScript too. Rust is just the only one telling you.

In a nutshell

The borrow checker isn't being difficult. It's asking the questions a good reviewer would ask: who owns this, who's allowed to change it, and does it still exist when you use it? The difference is that it asks every time, on every line, and it doesn't get tired at 5pm on a Friday.

The rules feel strict in week one. By month three you notice you're asking yourself the same questions in TypeScript, and you start getting a bit grumpy that nobody's checking. Same as with illegal states: once a compiler has caught these bugs for you, it's hard to go back to catching them yourself.

Signing out!

Paul