defer vs errdefer in Practice
Choose always-run versus on-error.
Two Cleanup Keywords
Zig gives you two scheduling tools: defer always runs, while errdefer runs only when the scope exits with an error.
defer Means Always
Use defer when cleanup must happen no matter what, like closing a file you opened for the whole function.
const f = try open();
defer f.close();All lessons in this course
- What defer Guarantees
- Execution Order of Multiple defers
- defer vs errdefer in Practice
- Pairing Open with Close