Master Zig comptime generics, error unions, defer and errdefer for resource management, and the build.zig system.
0 / 5 completed
1 / 5
What is Zig comptime?
comptime:fn add(comptime T: type, a: T, b: T) T { return a + b; } is a generic function. Zig's generics, type introspection, and conditional compilation all use comptime. There are no templates or macros — just Zig code evaluated at compile time.
2 / 5
What is a Zig error union and how is it used?
Error union:fn readFile() ![]u8 returns either an error or a byte slice. The caller uses const data = try readFile() which returns the error from the current function if one occurred. Errors are values — exhaustive handling is enforced at compile time.
3 / 5
What does defer do in Zig?
defer:const file = try openFile(...); defer file.close(); ensures close() always runs when the scope ends. Multiple defers execute in reverse order (LIFO). This is Zig's primary resource management mechanism in the absence of destructors.
4 / 5
What is errdefer and how does it differ from defer?
errdefer:const resource = try acquire(); errdefer release(resource); releases the resource only if a later error occurs. If the function succeeds, errdefer does not run — the caller takes ownership. This avoids prematurely releasing resources when success transfers ownership.
5 / 5
What is the role of build.zig in a Zig project?
build.zig: the build script is plain Zig code. pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{...}); b.installArtifact(exe); }. The zig build command runs this script, which constructs the build DAG. Cross-compilation targets, test steps, and dependencies are all configured in Zig itself.