Introducing Zig

banner image

In this week’s newsletter, we’ll be taking a look at the Zig programming language.

Zig is a general-purpose programming language and toolchain designed for maintaining robust, optimal, and reusable software. As a project that started in 2015, Zig aims to combine the strengths of languages like C, C++, Rust, and Go while offering enhanced developer experiences and comparable performance (1).

Features

Key features of Zig include:

  1. No hidden control flow or memory allocation, giving developers full control.
  2. Absence of preprocessor and macros, instead allowing direct compile-time code writing.
  3. Excellent interoperability with C/C++ and support for cross-compilation.
  4. Built-in testing capabilities, leveraging compile-time evaluation features.
  5. Simple and succinct syntax, with the entire language specification articulated in a 500-line parsing expression grammar file.

Use

Zig positions itself as a potential successor to the C programming language, offering a safer, less buggy, and more maintainable alternative for systems-oriented development.

The language emphasizes simplicity and ease of use, with a small feature footprint and a commitment to providing a single obvious way of doing things. This design philosophy aims to make Zig easier to learn and apply compared to more complex languages like C++ or Rust.

Traction

Zig has been gaining significant traction recently with a 50% growth in developer interest according to one recent language rankings report (2).

Here, we see the rate of increate in Zig’s GitHub star history:

Zig’s GitHub star history (3)

Zig is being used in various areas, such as game development, WebAssembly, systems programming and various open source projects.

Example Project

  1. Install Zig
  2. Create a project:
mkdir hello
cd hello
zig init
  1. Open the hello directory using VSCode. Install VSCode’s “Zig” extension for syntax highlighting and more.
  2. Let’s add this code to print the Fibonacci sequence with a hardcoded start value of 5. Replace src/main.zig with:
const std = @import("std");

pub fn main() !void {
    const start_value: u128 = 5;
    const num_to_print: usize = 10; // Number of Fibonacci numbers to print

    var a: u128 = 0;
    var b: u128 = 1;

    // Find the first Fibonacci number >= start_value
    while (b < start_value) {
        const temp = a + b;
        a = b;
        b = temp;
    }

    std.debug.print("Fibonacci sequence starting from {d}:\n", .{b});

    var i: usize = 0;
    while (i < num_to_print) : (i += 1) {
        std.debug.print("{d} ", .{b});
        const temp = a + b;
        a = b;
        b = temp;
    }
    std.debug.print("\n", .{});
}
  1. Run the program: zig build run

As you can see the syntax is very simple.

Conclusion: My Initial Take on Zig

My quick take:

My latest favorite cross-platform terminal application, Ghostty, written by the founder of HashiCorp, is written in Zig (4) to ensure fast performance (Zig compiles to native machine code), cross-platform compatibility, low level control with the ability to have fine grained control over memory management and an approachable, fun syntax and language design.

I think if I was writing something that needed high performance, especially if it needed seamless interop with C libraries (without needing an FFI layer), I would strongly consider Zig over say, Rust. As to why, that’s probably a topic for another blog post.

What Do You Think? Are you interested in more Zig tutorials? Reach out at hello@seenickcode.com

As always, happy coding,

Nick

Sources: 1, 2, 3, 4