Setup

Setting up Zig

Install

1
2
3
4
brew install zig
zig version

zig -h

To upgrade, brew upgrade zig

To uninstall, brew uninstall zig

Hello, world!

1
2
3
mkdir hello-zig
cd hello-zig
zig init-exe

Replace the content of generated src/main.zig with this:

1
2
3
4
5
const std = @import("std");

pub fn main() !void {
    std.debug.print("Hello, {s}!\n", .{"world"});
}

Build and run with:

1
zig build run

The generated executable binary is saved at zig-out/bin/hello-zig

Default build for this “Hello, world” program is around 1.0M in size.

Build a binary optimized for small file size with:

1
zig build -Doptimize=ReleaseSmall

This results in a “Hello, world binary” of around 51K in size!

The distributed financial transactions database TigerBeetle - written in Zig, is only 700K.

How to cross compile from any host OS to multiple supported CPU Architecture/OS cobination is described here.

Add a test to main.zig

1
2
3
test "always succeeds" {
    try std.testing.expect(true);
}

Build and test with:

1
zig build test