Why Zig? Two news items have triggered
my interest in Zig. The first: the Bun JavaScript runtime was written in
Zig. It shows improved performance compared to NodeJS. The second? Zig has the highest
median salary in the Stack Overflow survey. What is Zig? How does it compare to other
systems programming languages like Rust and C? Is it worth learning? Is it worth using for your
project? Is it mature enough to invest time in? Let’s start with Zig’s origin story and the
reasons behind its birth. Why was Zig created? Why was Zig created?
For that, we need to look at the programming landscape. C is the
core language of most operating systems and device drivers. It was invented in
the 1970s by Denis Ritchie. I remember learning to program with it in
the 1990s. In short, it's old. And in the half-century since C's
creation, hardware has evolved. Computers now all have more than one
processor. Network connectivity is the norm, not the exception. All this means that software
needs, principles and best practices have evolved. Most notably, with the advent of
concurrency and asynchronous programming. And to add insult to injury, C has
issues. Let’s go through these problems, and how Zig aims to solve them. In short, let's look at Zig's strengths relative to
C. And how Zig's approach compares to Rust. But before we go into that, it's
worth noting that unlike Rust, Zig does not aim to replace C wholesale.
Instead Andrew Kelley designed Zig to interoperate with the C language. You can use
C libraries from Zig, and Zig libraries from C. This means Zig can draw on the whole C ecosystem.
It also means that migrating from C to Zig need not be all or nothing, nor immediate. With Zig you
can incrementally improve your C and C++ codebase. This means Zig can be used to
complement C, and draw on its strengths. But how does Zig address C's weaknesses? What are Zig's Strengths?
The most fundamental fixes Zig provides is not the most obvious.
Namely: error management and types. Error Management
C returns integers as error codes. This makes
handling errors a haphazard affair. There is nothing stopping you
from ignoring errors or mishandling them. Zig introduces a clearer way
to handle errors. In Zig, error management is a core part of the
syntax. It's a part of the type system. This means a function either deals with the
error, or returns a specific error type. The language feature forces you to write code
to manage the problem when an error occurs. And that is perhaps the genius part of Zig.
By making errors part of the type system, managing errors is not an option. This
makes your code clearer and safer. Zig's sytax encourages you to deal with problems
upfront, and to write more reliable code. Memory management This error management is key to
how Zig reduces memory errors. The C language gives you direct access
to memory. This provides total control, but with great power comes great
responsability. C's control has very limited guardrails. It allows buffer
overflows and null pointer errors. Zig uses the type system to create the guardrails.
This prompts you to define what the behavior should be if a memory error occurs. Zig forces
you to explicitly handle of errors and null value. Thanks to this system, runtime memory
errors in C are caught earlier in Zig. This reduces the risk of crashes
and security vulnerabilities. It is worth noting that Zig is flexible.
You can choose different build modes. These allow you to define how strict the
language should be. This allows you to strike the appropriate balance
between performance and safety. Zig's approach contrasts with Rust's. If Zig forces you to manage errors,
Rust tries to eliminate them by design, via its ownership model. In Rust, each piece
of data can only have one owner at a time. The compiler enforces how data is borrowed and
shared. Rust also tracks variables' lifetime, to ensure data doesn’t get accessed after it’s
been freed. This eliminates common bugs like use-after-free or double-free
errors right from the get-go. Compilation, comptime and tests
Now let's talk about compilation. As you might now, C and C++ have a macro system
and preprocessor directives. These that allow you to configure how compilation should work. This
ends up being a whole new language to learn. Zig introduces a concept called "comptime".
When you define code as comptime code, that code runs at when compilation occurs.
Instead of using a whole new macro syntax, you use Zig to configure what
happens at compilation time. Comptime allows Zig to simplify
how your code is built. This allows for faster compilation
times. And Andrew Kelley has stated that he wants to focus on making
the compilation even faster. Comptime also makes the compilation
behaviour a lot more readable. Zig also allows for cross-compilation. You can
build an executable on one platform when on another platform. For example, you can
build a Mac executable from Windows. All this is possible because the
toolchain is part of the language. And the comptime execution and integrated
compiler have another strength. They allow Zig to have unit tests written
within the source code itself. Other advantages
Now, what else does Zig bring to the table? Well, C has another problem: undefined behaviour.
There are configurations where your code's behaviour depends on where and how it’s run. Zig
solves this by discouraging hidden control flows. C relies on external libraries for
concurrency. In Zig concurrency is a native language feature. This makes multi-threaded
applications safer and easier to build. The problem with Zig
Now, is everything rosy? Should you drop everything and
start learning Zig right now? Well, I'd hesitate to recommend that just yet.
Especially if you don't already have solid programming experience. Zig is still
young. The latest current version is 0.12 This means there are still parts of
the language that are not settled. Andrew Kelley will redesign parts of
the language if it fits the language's goals. In particular if it allows the
compiler to better analyse the code. My recommendation
Conversely, if you are already programming in C or C++, or
want to do so, I recommend you give Zig a go. The syntax will feel familiar, and the
benefits that Zig provide are worth the effort. If you are just starting out, I recommend you
try Zig out using a website called Exercism. It allows you to discover a language using
simple challenges. And if you want to perfect your knowledge, I recommend you take a look at
CodeCrafters. This website allows you to build your own version of tools such as SQLite or
Bitorrent using Zig. This allows you to push your knowledge and polish your portfolio. I've
provided links for both in the description. And in the mean time, let me know
what you think in the comments, and I'll see you in this next video. It also
explores which languages are worth learning.