0Pricing
Zig Academy · Lesson

Execution Order of Multiple defers

Why deferred code runs in reverse.

Execution Order of Multiple defers is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

More Than One defer

A single scope can hold several defer statements. When the scope ends, Zig has to decide what order to run them in.

They Run in Reverse

Multiple defers fire in reverse order: the last one you wrote runs first, and the first one you wrote runs last.

defer one();
defer two();

Last In, First Out

This is classic LIFO behavior, just like a stack. Each defer is pushed on, and unwinding pops them off in reverse.

A Tiny Example

Here two defers run after work. The output is two then one, because the later defer unwinds before the earlier one.

defer print("one");
defer print("two");
work();

Why Reverse Makes Sense

Resources usually depend on earlier ones. Reverse order means you release the newest first, so nothing is freed while still in use.

Mirroring Acquisition

Open A, then open B. Reverse defers close B, then A, perfectly mirroring how you set them up.

a.open(); defer a.close();
b.open(); defer b.close();

Captured at Schedule Time

Each defer remembers its code in order, even though execution waits. The order is fixed the moment each defer is reached.

Skipped if Never Reached

If the scope exits before a defer line is reached, that defer was never scheduled, so it does not run at all.

if (early) return;
defer late();

Per-Scope Stacks

Each scope keeps its own defer stack. Inner scopes unwind their defers before the outer scope unwinds its own.

Predictable Teardown

Knowing the reverse rule lets you reason about cleanup precisely, so your teardown sequence is never a surprise.

A Handy Mental Model

Picture stacking plates: you add them top down, but you must remove from the top, taking the last one off first.

Quick Check

Two defers, one then two. What is the run order?

Recap

Multiple defers unwind in reverse order, like a stack, so the newest resource is released first and cleanup mirrors setup. 🔁

Frequently asked questions

Is the “Execution Order of Multiple defers” lesson free?

Yes — the full text of “Execution Order of Multiple defers” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Execution Order of Multiple defers”?

Why deferred code runs in reverse. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Execution Order of Multiple defers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What defer Guarantees
  2. Execution Order of Multiple defers
  3. defer vs errdefer in Practice
  4. Pairing Open with Close
← Back to Zig Academy