Conway with the Zig Programming Language

As suggested in a previous article, this year’s candidate of my lifetime programming language learning activity is Zig, and I decided to reimplement Conway with it.

Yes, Zig, apparently the great nemesis of Rust.

I wrote this version of Conway when I was still using Ubuntu, where Zig is only available as a snap package; but since I had uninstalled Snap completely (because seriously) I just downloaded the release archive and decompressed it into ~/.local/bin/zig, adding it to my PATH. Very simple to install, and no Snap thankyousomuch.

In Fedora 38, thankfully, a sudo dnf install zig will give you version 0.9.1 (at the time of this writing.)

For this exercise I used version 0.10.1 of Zig, installed manually. This is what the source code looks like:

const std = @import("std");
const Coord = @import("coord.zig").Coord;
const World = @import("world.zig").World;

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();

fn clrscr() void {
    std.debug.print("\x1B[2J\x1B[0;0H", .{});
}

pub fn main() !void {
    var alive = try World.blinker(Coord{ .x = 0, .y = 1 }, &allocator);
	defer alive.deinit();

    var world = try World.create(30, alive, &allocator);
    var generation: u16 = 0;

    const stdout_file = std.io.getStdOut().writer();
    var bw = std.io.bufferedWriter(stdout_file);
    const stdout = bw.writer();

    while (true) {
        clrscr();
        generation += 1;
        try world.format(stdout);
        try bw.flush();
        std.debug.print("Generation {d}", .{generation});
        std.os.nanosleep(0, 500000000);
        const new_world = try World.evolve(world);
        world.deinit();
        world = new_world;
    }
    world.deinit();
}

Modern

The first thing that strikes about Zig is that it is indeed a very modern programming language:

And so much more. The language really feels at home in 2023.

Rough Edges

There are a few things that are, in the humble opinion of this author, a bit of a disappointment.

If you want to learn Zig, here are some links for you:

Overall Impression

Zig is a very nice, approachable, and powerful programming language, delivering on its promise of being “a better C than C”. However, I believe Rust is a bit higher-level in the abstraction ladder, and maybe more suitable for situations like web applications, thanks to its higher-level libraries and string manipulation abilities.

In any case, Zig really deserves better documentation and its VSCode extension needs some love. At this point in time, the project appears young and full of promise.