Explainer Videos

Explainer Videos

These are short animated videos that go with the lectures. Each set takes one topic that students often find confusing, and rebuilds it step by step on real Rust code, with real compiler output. Every video has English captions and can be downloaded.

  • Rust Lifetimes: Where a Borrow Lives: four episodes, about 35 minutes, for Lecture 9. What a lifetime really is, why &mut borrows may not overlap, what 'a promises, and how structs and 'static fit in.
  • Rust Closures: What They Carry, How They’re Called: two episodes, about 19 minutes, for Lecture 10. The three ways a closure captures a value, what decides whether it is FnOnce, FnMut or Fn, and why functions in the standard library ask for the trait they do.
  • Rust Smart Pointers: How Wide Is a Pointer?: two episodes, about 14 minutes, for Lecture 11. What is inside a Box, why &str and Box<dyn Trait> are two words wide, and how Deref and Drop let a plain struct act like a pointer.
  • Rust Concurrency: Who Owns the Data?: four episodes, about 34 minutes, for Lecture 12. What an actor is and why sending a message hands over its ownership, when a thread on a channel waits and what ends the wait, how Arc<Mutex<T>> and atomics let threads share a counter after all, and how Send and Sync tell the compiler which types may cross threads.

Rust Lifetimes: Where a Borrow Lives

This is a series of four short animated episodes, about 35 minutes in total, that accompanies Lecture 9 on lifetimes. The episodes rebuild the borrow checker’s reasoning on one picture: every borrow drawn as a bar from where it starts to where it is last used. Watch them in order, as each one builds on the picture from the one before. The order differs from the lecture: liveness comes first, and annotations after.

Every program shown in the episodes was compiled with rustc 1.98.1, and every error message is the compiler’s real output. English captions are available from each player’s captions menu. The animations were made with ManimGL, and the narration uses a synthesized voice.

Reading the Pictures

Every episode uses the same picture: the code on the left, and to its right one coloured bar per value or borrow, lined up with the code lines.

  • Grey bar: an owned value, from its let to where it is dropped
  • Blue bar: a shared borrow &, from where it is created to its last use
  • Gold bar: a mutable borrow &mut
  • Teal band: a lifetime parameter 'a, one region the borrows must fit
  • Purple band: 'static, the region that never ends
  • Red: a conflict, where the compiler says no

Episode 1: A lifetime is not a scope

Download

The question: If a reference’s lifetime is the block its variable lives in, why does deleting one println! make an error disappear?

The takeaway: A borrow is alive from where it is created to its last use, possibly with gaps. What it borrows only has to outlive that stretch, not the variable’s whole block.

Episode 2: Why push can’t happen while you’re looking

Download

The question: Why does data.push(4) fail while x = &data[0] is still in use, and compile once x’s last use comes first?

The takeaway: A &mut borrow may not overlap a live borrow of the same data. Liveness, not scope, decides overlap, and the rule both prevents dangling references and lets the compiler optimise.

Episode 3: What 'a promises

Download

The question: Why can’t the compiler check a call to longest without 'a, and what does 'a actually promise?

The takeaway: The compiler checks each call using only the signature, so 'a names one region that both inputs must cover and the result must stay inside. It changes no value’s lifetime, and the elision rules write it for you in the common cases.

Episode 4: Borrowing structs, and the lifetime that never ends

Download

The question: What does the 'a in ImportantExcerpt<'a> forbid, and why is 'static a demand on callers rather than a fix?

The takeaway: A struct that holds a borrow can’t be used outside its 'a region. 'static is the region that never ends, so requiring it accepts only data that lives for the whole program, such as string literals.

Rust Closures: What They Carry, How They’re Called

This is a series of two short animated episodes that accompanies Lecture 10 on functional Rust. The first untangles the two lists on the closure slides: the three ways a closure can capture a value, and the three traits FnOnce, FnMut and Fn. The second shows why three functions in Rust’s standard library each ask for a different one of these traits. Watch them in order.

As in the lifetimes series, every program shown was compiled with rustc 1.98.1, every error message is the compiler’s real output, and every standard-library signature is quoted from the Rust 1.98.1 source.

Reading the Pictures

Every closure is drawn as a teal box with one slot for each variable it captures.

  • Blue arrow out of a slot: the closure captured the variable by shared reference &
  • Gold arrow out of a slot: captured by mutable reference &mut
  • Grey value inside a slot: the closure owns the value
  • Purple: what a call does with the box (reads, changes, or gives a value away), and the traits Fn, FnMut and FnOnce
  • Red: an error, or a value carried out of the box

Episode 1: It’s about the call, not the capture

Download

The question: A closure can borrow, mutably borrow or own what it captures; so what makes it FnOnce, FnMut or Fn?

The takeaway: A closure carries what it captures. How each value got in (borrowed, mutably borrowed or moved) is one question; what a call does with it is another, and only that decides the trait: a call that only reads makes it Fn, one that changes a captured value makes it FnMut, and one that gives a captured value away makes it only FnOnce.

Episode 2: Who asks for which, and why

Download

The question: Why does unwrap_or_else ask for FnOnce, retain for FnMut and set_hook for Fn, and which closures does each accept?

The takeaway: An Fn-trait bound is the function’s promise about how it will call your closure: at most once (FnOnce, so every closure fits), many times one after another (FnMut, so it may not give captured values away), or only through a shared reference (Fn, so it may not change them either). The less a function promises to do with your closure, the more closures it accepts.

Rust Smart Pointers: How Wide Is a Pointer?

This is a series of two short animated episodes that accompanies Lecture 11 on smart pointers and chapters 15.1 to 15.3 of the Rust Book. The first asks what is inside a pointer: why a Box<i32> is one word but a &str and a Box<dyn Animal> are two, and what the second word holds. The second shows how a plain struct acts like a pointer through the Deref and Drop traits, and how deref coercion works. Watch them in order.

As in the earlier series, every program shown was compiled with rustc 1.98.1, and every size, error message and output is the compiler’s or the program’s real output. The sizes are for a 64-bit machine.

Reading the Pictures

Memory is drawn in three areas: the stack on the left, in cells of eight bytes (one machine word), the heap on the right, and static memory for vtables.

  • Grey: a value’s bytes
  • Blue cell with an arrow: an address, the part of a pointer that points
  • Teal cell: the second word of a wide pointer, a length or a pointer to a vtable
  • Purple: a vtable, with a type’s methods and size, and the traits Deref and Drop
  • Gold arrow: a deref step
  • Red: an error, or a value being dropped

Episode 1: How wide is a pointer?

Download

The question: Why is a Box<i32> eight bytes, but a &str and a Box<dyn Animal> sixteen, and what is in the second eight?

The takeaway: A pointer is one word when the compiler knows the size of what it points to from the type. For a slice or a trait object, whose size is known only at run time, it carries a second word: the length, or a pointer to the vtable with the concrete type’s methods and size.

Episode 2: How a struct becomes a pointer

Download

The question: MyBox is just a struct, so how can *y, hello(&m) and automatic cleanup work on it?

The takeaway: A smart pointer is usually a struct that implements Deref, so *y means *(y.deref()) and, while compiling, the compiler chains deref calls to turn &MyBox<String> into &String and &str, and Drop, whose drop method runs when the value goes out of scope, or early through std::mem::drop.

Rust Concurrency: Who Owns the Data?

This is a series of short animated episodes that accompanies Lecture 12 on fearless concurrency and chapter 16 of the Rust Book. One example runs through all of them: ten threads that want to add to one counter. The first episode gives the counter to an actor, a thread that alone owns it, and shows that sending a message hands over its ownership. The second asks when a thread on a channel waits, and what ends the wait. The third shares the counter after all, with a Mutex inside an Arc, or with an atomic. The fourth asks how the compiler knows which types may cross threads: Send and Sync. Watch them in order.

As in the earlier series, every program shown was compiled with rustc 1.98.1, and every error message and output is the compiler’s or the program’s real output.

Reading the Pictures

Each thread is drawn as a horizontal lane, with its name at the left. Ten threads are drawn as three lanes marked ”× 10”.

  • Grey lane: a thread
  • Gold: the actor’s lane and the state it owns, such as the count
  • Blue plug: a transmitter, tx, or one of its clones
  • Teal: a receiver, rx, and the channel it ends
  • Purple chip: a message on its way, and the tag that says who owns a value
  • Red: an error, a dropped transmitter, or a thread waiting forever
  • Orange frame and padlock: a Mutex and its lock, and the guard that lock returns
  • Maroon arrow: an Rc or Arc owner pointing at shared data
  • Purple and pink checks: in episode 4’s grid, whether a type may be moved to another thread (Send) and shared by reference (Sync)

Episode 1: Who owns the counter?

Download

The question: Ten threads want to add to one counter; how does an actor let them do it without ever sharing the counter?

The takeaway: Here an actor is a thread that alone owns its state and changes it only in response to messages from a channel. send hands each message over to the receiver: a String’s heap bytes stay where they are, but the sender can no longer touch them. So no two threads reach the message at the same time, and you write no lock.

Episode 2: When does a channel wait?

Download

The question: When does a thread on a channel wait, and what ends the wait?

The takeaway: recv waits until a message arrives or the last transmitter, tx and all its clones, is dropped, so a loop over the receiver ends only when every transmitter is gone. send on an ordinary channel never waits; on a bounded sync_channel it waits when the channel is full. try_recv and try_send answer at once instead of waiting.

Episode 3: Sharing the counter after all

Download

The question: When ten threads really must share one counter, what makes Arc<Mutex<i32>> safe, and when is an atomic enough?

The takeaway: A Mutex owns its data, so the only way in is lock, whose guard unlocks when dropped. Arc lets ten threads own one Mutex with a thread-safe count, where Rc is refused. For a single number an atomic type needs no lock, but only an operation like fetch_add is one indivisible step; a load followed by a store is not.

Episode 4: How does the compiler know?

Download

The question: Why does the compiler let an Arc<Mutex<i32>> cross threads but refuse an Rc or a RefCell?

The takeaway: Send means a value’s ownership may move to another thread, and Sync means it may be shared by reference (T is Sync exactly when &T is Send). The compiler works both out from a type’s fields, and thread::spawn demands Send of everything a thread carries, so an Rc or a shared RefCell is refused before the program runs.