0PricingLogin
Zig Academy · Lesson

Execution Order of Multiple defers

Why deferred code runs in reverse.

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();

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