4 Months of Zig – Anna Liberty


2026-10-27 · 15 min · #computing

It has been about 4 months since I returned to Zig after being absent for just a few years. In this time, I’ve written some software program in Zig and reengaged with the neighborhood, on-line and in individual. I made a decision to share a few of that have, together with what I’ve preferred about Zig thus far and what I have never. I’ll embrace some comparisons to Rust alongside the way in which since that was my earlier go-to language.

Projects

The first venture I began in Zig was snurl. It is a library for interacting with the smallnet (Gemini, Finger, Gopher, Spartan, and such). My aim has been to supply an alternative choice to curl for the smallnet, with related testing and fuzzing necessities however on a smaller scale. I wished to roughly observe TigerStyle insofar because it made sense for the venture.

Something I bumped into early on was that the fuzzer did not really work. When getting enthusiastic about fuzzing in my final submit, I did not notice that the function had not but been accomplished. This is not stunning, contemplating Zig’s standing, nevertheless it was disappointing, particularly for the reason that fuzzer is demonstrated within the generated important.zig file offered by zig init.

After this, I wrote a two packages which I’ve not printed but. The first is a small TUI typer racer. It was less complicated than I anticipated to get a minimal model working thus far. The second is an n-body simulator with stellar evolution utilizing raylib.

Zig Day

The most enjoyable alternative in the previous few months was the prospect to go to an area Zig Day. I realized of Zig Day Seattle occurring on Lobsters on August twentieth. I used to be ecstatic to be taught that there was a Zig Day in my space. Then I noticed Andrew Kelley could be there and have become extra excited.

Zig Days are native meetups the place folks all for Zig hang around, work on their tasks collectively, and discuss Zig. For this Zig Day, I made a decision to work on snurl. A pal, Evin Yulo additionally determined to attend.

The occasion itself occurred within the Bothell library, moved there from the Bellevue library after so many individuals signed up that capability was overwhelmed. Having the creator of Zig on the occasion will try this.

I really like the format of those Zig Days and I’m glad they’re part of the neighborhood. Since they’re run by native teams, it provides the entire Zig neighborhood a extra interconnected feeling. It’s a approach to work together with different people who find themselves all for Zig in individual. And, on this case, the creator of Zig itself.

I bought the prospect to speak with different programmers about their tasks. Hopefully some ideological cross-pollination occurred that may make all our tasks stronger. I ended up engaged on snurl with evin, who began work on integrating Zig’s commonplace library TLS implementation as various backend.

I additionally briefly talked with Andrew Kelley. I hope Zig’s built-in fuzzing will get fastened someday quickly!

Ryan Hendrickson wrote an excellent article outlining extra of the occasions of that day from his perspective if you need extra particulars.

I’ll undoubtedly be attending Zig Day Seattle once more sooner or later. I could even head all the way down to Portland to catch one earlier than the following native one comes round. I benefit from the alternative to attach with different folks all for Zig and its values.

What I’ve Liked

anyzig

I really like anyzig. I’ve had to make use of zig grasp and 0.16.0 for various tasks, and anyzig has made that doable.

Unfortunately, it breaks exterior of directories with no construct.zig.zon, that means in case you have tooling that requires a zig command to work, it could possibly solely happen in venture directories. This breaks autoformatting for my one-off Zig scripts. Maybe a default zig model may very well be offered in that case.

IO and Allocators

I do know it frustrates some folks, however I really like the truth that you must cross IO and allocator parameters to capabilities in Zig.

Allocators

I’ll begin with allocators, since they have been round longer. For every Zig operate you must settle for an express allocator parameter. This is completely different from most programming languages which both use a default allocator which may be modified globally or one modified by an area context (see Odin).

I like with the ability to change allocators for various elements of code. You would possibly need an enviornment allocator for some issues however want a normal objective allocator for others. Sometimes a FixedBufferAllocator is all it’s worthwhile to carry out an operation. Zig’s model makes this all apparent to deal with.

I favor Zig’s system to Odin’s implicit context system since Zig retains issues express. It’s clear whether or not a operate allocates reminiscence or not, and laborious to by chance cross within the improper allocator.

This can also be superior to Rust’s hidden, infallible allocations that panic when reminiscence is exhausted. They’re engaged on including express allocations, however this might be one more function tacked onto the language slightly than one thing constructed from the bottom up.

It’s a lot simpler to purpose about packages that allocate reminiscence in Zig.

IO

The IO parameter is much more highly effective, even whether it is newer. It is properly well worth the breaking modifications required to convey it about (even when I skipped the 0.15.0 -> 0.16.0 transition). In Zig, each operate that interacts with the underlying working system (with caveats) requires an “IO” parameter. This gives the context for enter, output, and the whole lot else. Porting Zig to new working methods is usually simply porting IO.

Practically, that is very nice for testing. When testing habits, you simply have to put in a testing IO occasion that works as you want it to. You can change the enter stream nevertheless you need, trigger writes to fail, and the rest.

Just like with allocation, passing an IO argument makes it clear which packages are pure and which of them usually are not. You can inform at a glace whether or not or not a program goes to switch one thing exterior.

Zig has a very good monitor document of holding issues easy whereas additionally including highly effective options. Instead of including 100 small options that do barely completely different related issues, it tends to supply one main function that helps all the assorted different use-cases. Low flooring, excessive ceiling.

Comptime

I really like comptime. It’s a seamless approach to do metaprogramming and precomputation multi function. Other folks have written extra detailed accounts so I shall transfer on.

Build System

Zig’s build system is stellar. So a lot in order that firms like Uber have adopted it for his or her software program written in different languages.

One factor I like about it’s that I can combine different instruments with it seamlessly in ways in which Cargo complains about. This is very vital for considered one of my tasks that includes code era.

Dependency Culture

Zig has a wonderful dependency tradition. The important ethos is straightforward: keep away from. When you do not want a dependency, do not add one. However, once you do, it is usually a battle-tested library that Zig can effortlessly combine with translate-c. You can simply additionally vendor in exterior dependencies and audit their code.

Most of my Zig packages have about one or two direct Zig dependencies, with zero oblique dependencies. This makes the codebase a lot simpler to audit and management.

This stands in stark distinction to Rust, the place the smallest of my tasks commonly have just a few direct and dozens of oblique dependencies.

This is thanks each to Zig’s dependency tradition and the truth that important options like reflection, random quantity era, clear error dealing with, and JSON parsing are constructed into the language or commonplace library slightly than maintained as separate particular person libraries. No have to depend upon serde or syn in each program.

Zig encourages cautious consideration earlier than including dependencies, and I respect that.

Handling Errors Effortlessly

Zig pushes programmers to deal with errors accurately by default. The laziest approach to deal with errors in Zig is to make use of the strive key phrase and add ! to the operate’s return kind.

This contrasts to Rust, which leads programmers to insert panics into their code. The difficulty is that it is usually simpler to panic when prototyping, after which depart that within the code longer than it needs to be. I’ve used manufacturing code written in Rust that panic as an alternative of returning a correct error.

In Rust you need to use ?, which is faster than unwrap, however you additionally must do one thing like Result. That’s a mouthful! Alternatively, you need to use the anyhow crate and return anyhow::Result, which additionally gives some good helper strategies. But then you must add a dependency for affordable error dealing with. There’s additionally Zig-style composable errors with one more library.

Incremental Compilation

The core group members at Zig have been doing wonderful work at incremental compilation. If you run zig construct --watch -fincremental, the compiler will look ahead to modifications, verify precisely what you modified, after which modify solely what you have modified all the way down to linking. No effort past what’s required, so it is quicker that human notion even for big codebases. Zig was designed from the bottom as much as help this habits.

This makes for a very nice workflow. I can simply run the compiler in incremental mode proper subsequent to my textual content editor, and get updates on my code as I save. No time to get distracted with one thing else. With Rust tasks 10+ seconds incremental compilation loops have been frequent even in smaller tasks. That’s sufficient time to get distracted however not sufficient time to be productive doing one thing else, leaving annoying gaps that Zig eliminates.

Rust is deprived right here. It’s a steady language that should keep backwards compatibility. This means it could possibly’t afford to interrupt issues as a way to enhance compilation velocity, not less than to not any giant diploma. Meanwhile, if it makes Zig higher, the maintainers are keen to interrupt absolutely anything to make sure the tip outcome might be excellent.

Currently, Zig solely helps incremental compilation on x86_64 on Linux and macOS. If you employ one other working system or structure you will not have entry to this function but, however in that case it is one other factor to look out for!

This makes Zig only a bit extra great to work with. I’ll take it.

What I’d Like to See

Improved Stability

Like each different consumer of Zig, I wish to see the usual library turn into extra steady. I do know that is one of many final duties to do earlier than 1.0, and there are a lot of others earlier than then, however it’s irritating to take care of the fixed breakage within the ecosystem. My understanding is that that is why 0.17.0 took so lengthy to be launched.

Andrew Kelley stated at Zig Day that he wish to see 1.0 greater than anybody. I imagine him, so I’ll have endurance and belief that the ultimate model might be wonderful.

This is the one purpose I nonetheless use Rust for some tasks. Some circumstances require a steady programming language and the ecosystem that gives. But contemplating the spectacular software program that is being inbuilt Zig as of late, stability is not the whole lot. And there are tasks for which the niceties of Zig outweigh its instability. I am unable to await 1.0.

Lifetime Support

I wish to see extra help with lifetimes within the Zig compiler. Specifically, I wish to see returning native variables to fail in debug mode, both as a type of built-in lint (as Andrew Kelley has talked about desirous to implement) or as actually invalidating that reminiscence after the operate returns. That method new customers shortly be taught to keep away from that sample. Currently, it is simple to unintentionally break your code this manner, even when you assume you perceive lifetimes.

Relatedly, the compiler at present accepts code that might be damaged sooner or later. The compiler lets variables reside your complete scope as an alternative of the scope of a block. This implies that when you stack-allocate a buffer and return it from a block, that buffer needs to be invalidated however is not. My understanding is that this might silently break in a future Zig model.

// This works however might be invalid.
const foo = blk: {
	var buf: [1024]u8 = undefined;
	const n = strive writeToBuf(&buf);
	break :blk buf[0..n];
};

More Documentation

Zig is poorly documented. It has sufficient to get you began, however to proceed onward you must parse intention from sparse doc feedback which can be generally connected to capabilities. Examples are uncommon. I nonetheless have not bought Zig’s commonplace library TLS working in snurl as a result of neither I nor a pal can work out how one can arrange the buffers accurately.

Because of this lack of documentation, loads of the information for constructing stable Zig packages is distributed throughout the Web. If you are properly built-in into Zig communities, having learn the present documentation, watched the displays, and browse the code, you possibly can succeed. If you are not that properly built-in, nevertheless, it may be a problem to work successfully.

The good thing about that is that it encourages folks to interact in Zig communities (I’m an admin for the Matrix room, for instance). While LLMs search to demolish human collaboration, we want human-to-human collaboration now greater than ever. However, we are able to nonetheless have that and good documentation too. As everyone knows, folks will nonetheless ask questions in case you have issues well-documented. And a superb neighborhood is aware of how one can level folks in the direction of good assets slightly than simply saying “RTFM”.

I might even be curious to see what the intersection of Zig and literate programming may very well be. My literate programming software is written in Rust proper now, however that may change.

Comptime Allocators

I might like to parse JSON at compile time. For considered one of my tasks, there’s a JSON file that’s saved contained in the binary. It’s static and may very well be parsed at compile time into a group of structs.

However, the usual library JSON parser requires an allocator. Because there are not any allocators that work at compile time, which means that any JSON parsing should be accomplished at runtime. So that is what I did with my venture.

I imagine there’s an intention of including a JSON parser that doesn’t require an allocator (the info already exists within the JSON string, in spite of everything). That will resolve the rapid drawback, however there are extra normal circumstances that will require an allocator. I imagine FixedBufferAllocator labored at compile time earlier than and served this objective. It might accomplish that once more.

Better Bootstrapping

Zig’s bootstrapping story is spectacular. They erased the C++ version of their compiler and replaced it with a small WASM blob and WASM interpreter. The WASM file is a stripped-down model of the compiler that compiles the total Zig compiler to C, which is then compiled by the system C compiler. It’s an attention-grabbing strategy and you must learn the linked article if you need extra particulars.

The one caveat to that is that it requires the compiled binary blob checked into the supply code. This is best than requiring the total earlier compiler (like Rust), nevertheless it’s inferior to having the total supply accessible for each step of the method in any respect factors. A malicious actor might nonetheless sneak one thing into the blob. More importantly, many distributions (like Debian) can not correctly package deal Zig in the intervening time as a result of it is unimaginable to take action in a method that is compliant with their pointers.

Their authentic plan was to make use of the compiler’s C backend to generate a C model of the compiler which might then be maintained individually. They would then have two completely different supply variations. They determined this strategy could be too tough, sadly.

Because of this, somebody should make one other Zig compiler in one other language (most likely C) that may enhance the bootstrap course of. This is explicitly not one thing the Zig Software Foundation desires to develop proper now. This implies that in the intervening time there isn’t any approach to bootstrap Zig absolutely from supply.

This difficulty is not only a theoretical drawback. Because Zig cannot be packaged on Debian, I had to decide on one other language for considered one of my tasks to make sure wider attainable utilization.

Fortunately, this difficulty is momentary. Once Zig is steady, constructing an alternate compiler needs to be simple. Zig is designed to be a easy language the place various implementations are humanly achievable, in contrast to other languages. Currently, the instability means any various compiler might be enjoying a continuing recreation of catch-up, which might be why the core group does not need to handle it whereas they’re quickly iterating.

Alas, this may increasingly additionally require ready for 1.0. May it come quickly!

Moving Forward

I’ll proceed to make use of Zig. I’m excited in regards to the course it is going and I look ahead to persevering with to be part of its neighborhood. So far, I’ve discovered no different programming language that I favor greater than Zig, even with its shortcomings. It’s simply steady sufficient that I haven’t got to make huge modifications between releases. It will proceed to be my go-to programming language for many tasks.

One of those tasks is ReTangled. It was beforehand written in Rust however I grew uninterested in the huge dependency timber and need to take full benefit of Zig’s construct system.

I’ve additionally been seeking to contribute extra to Zig instantly. Between my restricted time as a scholar, Zig’s overflow of contributors already, and the poor documentation, this might be a problem. But I need to contribute again, make an affect on the venture, and turn into a greater software program engineer. Thankfully, enhancing contributor onboarding appears to be a precedence for Andrew Kelley transferring ahead.

I’ll even be at future Zig Days in Seattle, if you wish to meet and work on Zig tasks collectively!



Source link