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 checked.zig
$ ./checked
panic: division by zero
checked.zig:10:23: in main
    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 checked.zig
$ ./checked
00

There’s four build modes in total:

Next example: Integers.