What defer Guarantees
Run code when a scope ends.
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();