I should say up front how little Zig I've written. I've run zig init twice, built and tested what it generated, and done the first two Ziglings exercises. One of those was adding the word pub in front of main. My day job is C#, and I've been reading Zig more than writing it.

So what follows is only what I noticed, coming from .NET, when I sat down and read the project that zig init creates on 0.16. The whole thing is 71 lines of main.zig and 18 lines of root.zig, and nearly every line does something C# would never ask me to do.

Main takes an argument

This is where the generated program starts:

pub fn main(init: std.process.Init) !void {
    // Prints to stderr, unbuffered, ignoring potential errors.
    std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

    // This is appropriate for anything that lives as long as the process.
    const arena: std.mem.Allocator = init.arena.allocator();

    // Accessing command line arguments:
    const args = try init.minimal.args.toSlice(arena);
    for (args) |arg| {
        std.log.info("arg: {s}", .{arg});
    }

In C#, Main gets an array of strings, and everything else is ambient. Console is a static class. The heap is just there. The thread pool starts when something needs it.

In this template, main receives one value, init, and the rest comes out of it. The allocator comes from init.arena. The command-line arguments come from init.minimal.args, and turning them into a slice needs that allocator, so the call can fail and is marked with try. A few lines further down, I/O comes from init.io.

The return type is !void. The exclamation mark means the function can return an error. There are no exceptions in Zig, so that mark and the try keyword are the entire mechanism: try unwraps a success or returns the error to the caller.

What struck me is that I can see every line that can fail. In C# I honestly don't know which calls in a method might throw unless I go and read them.

Writing to stdout takes four lines

This part surprised me most:

    // In order to do I/O operations need an `Io` instance.
    const io = init.io;

    var stdout_buffer: [1024]u8 = undefined;
    var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
    const stdout_writer = &stdout_file_writer.interface;

    try cap.printAnotherMessage(stdout_writer);

    try stdout_writer.flush(); // Don't forget to flush!
}

To print a line to standard output, the program declares a 1,024-byte buffer on the stack, builds a file writer over stdout using that buffer and the io value, takes a pointer to the writer's generic interface, hands the pointer to a function, and then flushes.

Console.WriteLine does all of that too. It has a buffer, an encoder, a lock and a flush policy. I've just never had to know where any of them live. Here the buffer is mine: I chose its size, it sits in my stack frame, and if I forget the last line then the output never appears. The template's own comment says "Don't forget to flush!", so I assume plenty of people have forgotten.

The function being called lives in root.zig:

/// Accepting an `Io.Writer` instance is a handy way to write reusable code.
pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void {
    try writer.print("Run `zig build test` to run the tests.\n", .{});
}

It doesn't know that it's writing to stdout. It takes a writer and writes to it. A test could pass a writer backed by a fixed buffer and check the bytes afterwards. In C# I'd reach for the same idea with a TextWriter parameter, but nothing pushes me towards it, and most code calls Console directly. In Zig there's no global to call, so the parameter is the only way.

The test that tells you to break it

Tests sit in the same file as the code, in test blocks. The template includes this one:

test "simple test" {
    const gpa = std.testing.allocator;
    var list: std.ArrayList(i32) = .empty;
    defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
    try list.append(gpa, 42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

Three things in here are foreign to a C# developer.

The list doesn't own an allocator. std.ArrayList(i32) starts as .empty, and every operation that might need memory takes the allocator as an argument: append(gpa, 42), deinit(gpa). A List<int> in C# just grows. This one can't grow unless I tell it where the memory comes from, each time.

defer schedules the cleanup for when the scope ends. It's the closest thing here to using, except that it works for any statement, and it's written on the line right after the thing it cleans up.

And the allocator is std.testing.allocator, which fails the test if anything allocated through it is still alive at the end. The comment in the template invites you to comment out the defer line and see what happens. I like that a lot. In .NET, finding a leak means a memory profiler and an afternoon. Here it's the default behaviour of a unit test, and the starter project shows it to you in the first file you open.

Below it there's a fuzz test, also generated, which feeds random operations to a list and checks an invariant. I haven't done anything with it yet, but I noticed that fuzzing is part of the standard test runner and not a separate tool.

Setting it up was the hard part

Zig hasn't reached 1.0, and I felt that before I wrote a line.

I started on a development build of 0.16, because the tools I wanted needed one. Ziglings asked for a specific dev build or newer. The language server, zls, has to be built against a matching compiler, so I built it from source. For months my shell's PATH pointed at a directory named zig-aarch64-macos-0.16.0-dev.2682+02142a54d, and the zls config had two more paths pinned to the same build. When 0.16.0 was released, I switched the PATH to it and deleted the pins.

I also cloned Ghostty, which is written in Zig, to have a real codebase to read. Its build file asks for Zig 0.15.2. My installed compiler is 0.16.0. So at the moment I have one compiler installed, and it doesn't match every Zig project on my disk.

A language that's still changing will do this to you, and Zig is open about it. It does mean that "install Zig" has a different answer for each project you want to build.

What I did in my editor

I spent more time on editor support than on Zig itself, which is typical of me.

My Neovim build key walks up from the current file looking for a project it recognises. I added one more case:

if vim.fn.filereadable(dir .. "/build.zig") == 1 then
    return "zig build", dir
end

For single files there's a :ZigRun command on ;zr. It runs zig run on the current buffer and sends the output to the quickfix list, so a compile error is one keypress from the line that caused it.

I also wrote a colour scheme. The code samples in the Zig documentation use very little colour. Keywords, strings, builtins, comments, numbers and primitive types are coloured, and everything else is plain text. I liked how calm that looks, so I copied the palette into a Neovim theme.

What I think so far

I can only speak from the first step, so take this as a first impression.

Everything C# hides from me, Zig hands over as a parameter. The allocator is a parameter, and so are the I/O handle and the writer. Failure is in the return type. It makes a hello-world longer than I thought a hello-world could be, and it makes every function honest about what it needs.

I don't know yet whether that's pleasant across ten thousand lines. I can already see the cost in the template, where printing one line takes a buffer, a writer, a pointer and a flush. I can see the benefit as well, in a unit test that catches a leak without my asking.

The obvious next step is something small that does real work with files and memory, such as a command-line tool. A proper post about Zig can wait until I have code in it that I wrote myself.