Build Modes
Zig has four build modes that make different trade-offs between safety and speed.
Let’s take another look at our example for checked illegal behavior:
const std = @import("std");
const print = std.debug.print;
fn f() u64 {
return 0;
}
pub fn main() void {
print("{}\n", .{1 / f()});
}Running the compiler with -O Debug selects
Debug build mode. That’s the default, so the output here is
what we’ve already seen:
$ zig build-exe -O Debug build-modes.zig
$ ./build-modes
thread 216374 panic: division by zero
[...]/build-modes.zig:9:23: 0x11d7ed0 in main (build-modes.zig)
print("{}\n", .{1 / f()});
^
[...]
Aborted
ReleaseFast disables safety checks so all illegal behavior becomes unchecked. Instead of an error message we get unpredictable behavior:
$ zig build-exe -O ReleaseFast build-modes.zig
$ ./build-modes
00
There’s four build modes in total:
- Debug: for testing and debugging.
- ReleaseSafe: enables optimizations, keeps safety checks.
- ReleaseFast: disables safety checks, optimizes for speed.
- ReleaseSmall: disables safety checks, optimizes for small binaries.
Next example: Integers.