When I choose Zig over Rust
I was talking with a coworker the other day and I brought up zig (no surprise there). They had just released the 0.16 std.Io update and it was a
very interesting talk about explicit control flow and its pros and cons. My coworker asked me something I hadn’t really thought about if I’m completely honest:
“When does zig make sense over rust?”.
At first I was not sure how to respond. But then, like a vein of diamond ore when you have shaders on, the answer illuminated the dark pit that is my brain.
“When you want to have fun!”
Let me explain. What code block demonstrating basic error handling looks better to you?
Codeblock A
enum MathError { NotPositive }
fn require_positive(value: i32) -> Result<i32, MathError> {
if value <= 0 {
return Err(MathError::NotPositive);
}
Ok(value)
}
Codeblock B
fn requirePositive(value: i32) !i32 {
if (value <= 0) return error.NotPositive;
return value;
}
If you chose the first codeblock, you may need to be institutionalized. To me, it is clear which one looks better. I know that the way a block of code looks might seem like a silly metric to judge a programming language by, most people would likely rather judge a language by feature set, performance, etc. To me though, that all doesn’t really matter. When I get home from a day writing code at work, I want to sit down and write ‘relaxing’ code, not code that makes me stress out. I get stressed out looking at codeblock A. And you should too. Andrew Kelley (the creator the Zig Software Foundation) says it best when he mentions that Zig is about doing more with less. To compare, rust is about doing more with more, and something like the suck-less software mindset is about doing less with less.
Yes you could avoid the error enum with a &'static str to condense a bit but that syntax is insane. The point is the absurd amount of boiler plate around error handling.
Now, I would not recommend zig for every task that rust might be good for, especially in a corporate setting. The language is not developed enough for some tasks and the community
is not as built out as rusts community is. Tools like Ratatui, Dioxus, Clap, etc. make rust very easy to work in if you know the basics of the language (more on that later).
I’ve used rust pretty extensively at work and in my personal life, and its really great for getting things done and sometimes you get to solutions in the language that are really
beautiful, but the work towards getting to that solution is 10x compared to zig (of course, you have the tradeoff of memory safety, which is critical in a lot of work environments,
but to that I say skill issue)
I build a lot of tools in my free time (for my arch machine, btw). I like to know how things work and building them myself helps with that (shocker). A good example of when rust was better: I wanted to build a theme swapping tool which had an interactive tui that displayed color palettes, and was extensible enough to work well with somebar + dwl (yes, pywal would have worked here but its too late now i already built it). Rust was a good choice for me here because I generally did not want to have a fun time writing the code, I just wanted something that works. The community built great tools around CLIs and TUIs, and all I really had to do was the color palette generation which was not an intense amount of work or code.
Something else I have been building in my free time is a source control called Ket. It uses the concepts of quantum physics to make a source control that just makes sense to me. For example, instead of a branch having just one tree, why not let it have multiple trees (called superpositions). This completely removes the dread of merging your code with another branch, because if there are conflicts now you just have two basis’ on a superposition and thats a completely valid state. This is in my opinion the exact way that source control should work. When developers want to write code, they should write code. They should not also have to worry about the source control at all. This is somewhat similar to Jujutsu in some ways (funny enough, jujutsu is written in rust, oddly relavent), but functions fundamentally different on the backend which allows for some sweet tricks to reduce drag. Anyways, this has been a great project for zig because I actually have a lot of fun sitting down and writing it and obsessing over the details. No hidden control flow makes it much easier to tell when I am overusing allocations or filesystem interactions, and I can sit down for hours and fix it (which is considered fun to me).
Lastly, I think an important thing to bring up is philosophy. Not in general, just as pertains to programming languages. In my opinion, you should always be able to be proficient (meaning that the language itself is not costing you large amounts of time in development) in a language in a week of dedicated learning. This was not the case for me in rust. I spent a week studying and working in rust, but still I only knew a fraction of the language. Of course, I do not expect to be an expert, but the complexity should be in the problem you are solving, not in the language you are using. For example, the rust standard library creates so many abstractions that it is often times extremely difficult for somebody to find the ‘right’ way to do something. Zig on the other hand, as mentioned already, avoids abstracting away from the developer. Not only this, but Rust has far too many different ways to do the same thing. Take this example:
// assume iter is an iterator on numbers
while(iter.next()) |item| {
// do something with the item
}
// "But what if I need to enumerate!!!!"
var count: usize = 0;
while(iter.next()) |item| {
// do something with the item
count+=1;
}
// or put more simply:
for (numbers, 0..) |num, i| {
// do something with num and i
}
These are fairly straight forward and intuitive once you understand the syntax of zig. Now lets look at rust.
// say you have some numbers
let numbers = vec![1,2,3,4,5];
// you could do a for loop like this
// generally prefered by rusts linter
for n in &numbers {
// do something with n
}
// or iterate like this
for n in numbers.iter() {
// do something with n
}
// or do that with an enumerator
for (i, n) in numbers.iter().enumerate() {
// do something with n
}
// or functional style (which i see in the workplace quite often)
numbers.iter().for_each(|n| {
// do something with n
});
The only thing I like about rust in that code block is let as a keyword, it just reads nicely. Anyways, I hope you can see my point. In rust, the amount of abstractions that
the language takes makes it hard to understand what is idiomatic and what is optimal. Its the exact issue that torments newcomers to C++ (though rust has much better naming).
Ok one more codeblock I swear, this is a fairly common pattern when dealing with functional rust:
numbers
.iter()
.filter(|n| n == n) // some filter
.map(|n| n) // some map
.enumerate()
.for_each(|(i, n)| {
// something with i and n
});
Sure this may look pretty awesome and clean, but is it idiomatic? Well there is two problems there. One is that it depends on the codebase, and the other is that it depends on the developer writing the code. Having to sift through that is much harder and, decidedly, less fun than just writing a language like zig. Part of this can be attributed to the fact that rust is multi-paradigm, meaning that it can be imperative, functional, or procedural depending on the developer or the needs of the program. Thats not necessarily a bad thing, it just goes against the philosophy I have laid out here.
To sum up that section: its really easy to get lost in the sauce of writing idiomatic rust, but in zig I find that it comes naturally because of its ‘doing more with less’ mindset. Of course, that brings up the elephant in the room.
“What about when there isnt a developer? What if people are only using LLMs to write code, instead of writing it by hand? Which one is better?”
But lets be honest: if you are vibe coding it doesn’t matter what language its in, you weren’t gonna read it either way.
Having this conversation with my coworker branched out into many different aspects of language design, and the idea of ‘what makes a language good’. I found it to be an insightful topic, and wanted to share a quick summary of my thoughts here. Of course, there are many aspects that I did not touch on, or only briefly touched on, and I highly encourage anyone reading this to go try out both languages on a small to medium sized project if you havent already. I promise you’ll come out a better developer with a more broad understanding of how different languages fit different needs (and also an understanding of what you like).
tldr: rust is for when coding is work, and zig is for when coding is fun. And I have a massive rust skill issue.