🍗 Wiki

Zig

Zig

2. Install

nix-shell -p zig

As you can see, you can try the Zig compiler in nearly any version you want.

nix-shell -p zig_0_10  # Zig v0.10.1

3. Writing a code and compiling it

This code below will print "Hello World" to the console.

// hello.zig

const std = @import("std");

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

You can instantly run the Zig code with zig run command.

$ zig run hello.zig
Hello, World!

4. Tips

4.1. Zig cc and Zig c++

Zig CLI offers nice features: Drop-in C and C++ compiling feature!

Write a simple C and C++ code:

#include <stdio.h>

int main() {
  for (int i=2; i<10; i++) {
    for (int j=1; j<10; j++) {
      printf("%d * %d = %d\n", i, j, i*j);
    }
  }

  return 0;
}

Then compile it with the Zig CLI:

zig cc times.c

Result is as expected. And same goes for C++

zig c++ my-coding-competition.cpp

See Also:

6. See Also

  • Bun is a JavaScript and TypeScript runtime written in Zig.