What defer Guarantees
Run code when a scope ends.
What defer Guarantees is a free Zig Academy lesson on CoddyKit — lesson 1 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.
Cleanup You Can Trust
Almost every program opens something it must later close. Zig gives you defer so that cleanup is never forgotten.
What defer Does
A defer statement schedules a piece of code to run when the surrounding scope ends, no matter how it exits.
defer cleanup();Write Cleanup Right Away
The big idea: put the cleanup line next to the line that acquires the resource, so the two are impossible to separate.
const file = try open();
defer file.close();It Runs at Scope Exit
The deferred code does not run immediately. It waits until the scope it lives in finishes, then fires.
{
defer cleanup();
work();
}Works on Every Exit Path
Whether the scope ends normally or you return early from the middle, the deferred code still runs. There is no escape.
defer file.close();
if (bad) return error.Oops;Errors Do Not Skip It
Even if a try later in the function returns an error, your defer has already been scheduled and will run on the way out.
Any Statement, Not Just Calls
A defer can hold any single statement, including a block, so you can group several cleanup steps together.
defer {
free(buf);
log("done");
}Scopes Beyond Functions
defer is tied to its enclosing scope, which can be a loop body or an if block, not only a whole function.
while (more()) {
defer step();
}No Manual Tracking
Without defer you must remember to close on every branch. With it, the compiler runs your cleanup so you cannot forget a path.
Reads Top to Bottom
Because acquire and cleanup sit together, your code reads as a clear story: open, use, and the close is already promised.
The Resource Safety Workhorse
From files to allocations to locks, defer is the everyday tool that keeps Zig programs leak-free and tidy.
Quick Check
When does a deferred statement actually run?
Recap
You met defer: write cleanup beside acquisition, and Zig guarantees it runs when the scope exits on every path. 🧹
Frequently asked questions
Is the “What defer Guarantees” lesson free?
Yes — the full text of “What defer Guarantees” 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 “What defer Guarantees”?
Run code when a scope ends. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “What defer Guarantees” 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.