DEV Community

loading...
Cover image for Why you should learn Zig

Why you should learn Zig

hanna profile image Hanna Originally published at pyxel.pro ・4 min read

There is a lot of programming languages in the world, why create another, or more relevantly, why learn another? Today we're going to be discussing the programming language named Zig and why I have fallen in love with it.

Let's start from the beginning zig is meant to "fix" a lot of issues other programming languages have, a good video to watch is The Road to Zig 1.0 by Andew Kelley, which goes into a lot of the nitpicking on other languages such as C, Rust, D, and others. Now don't get me wrong, the other languages Zig is meant to compete against are great languages, they power a lot of technologies, however there are some things that just aren't right in my opinion, and that's where Zig comes in.

One thing Zig provides is a featureful toolset out of the box, and when I say featureful, I mean featureful. A general overview of the Zig language and toolchain can be seen here, but we're going to be focusing on a few specific points from this as well as it's comparison to other languages to best get my point across, hopefully.

Simple language

Zig is meant to be "similar" to C in the sense it is simple, the maintainer (Andrew Kelley) aims for the language to be and stay simple at all times, without adding a lot of extra functionality to the language like C++ does, while some may see this as a downside, and rightfully so, this can also be seen as an upside as it means you have less to learn, and you're more likely to pick up good rules of thumb and traditions.

Performance and safety

Zig's toolchain has 4 seperate build modes: Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Each build mode enables and disables specific parts of the compilation process, including but not limited to optimization, runtime safety checks, compression, and more. A simple overview about what each mode does can be viewed here. As Zig handles errors strictly, you're less likely to build and run programs that have undefined behavior in them, as zig has 2 layers of safety checks, both runtime and compile time.

  • D has @property functions, which are methods that you call with what looks like field access, so in the above example, c.d might call a function.
  • C++, D, and Rust have operator overloading, so the + operator might call a function.
  • C++, D, and Go have throw/catch exceptions, so foo() might throw an exception, and prevent bar() from being called. (Of course, even in Zig foo() could deadlock and prevent bar() from being called, but that can happen in any Turing-complete language.)

Another layer of safety Zig has is restricting some things, such as not implementing operator overloading, preventing hidden control flow among other things that can be caused by features as such. Another thing Zig focuses on preventing is hidden allocations.

No dependency on libc and cross compilation

While Zig can compile against libc (musl, glibc, etc), it does not depend on it. This allows you to build binaries that work on any platform. On top of this, Zig also ships with multiple versions of libc itself, so that if for any reason you want to compile against libc, you can cross-compile your program against a large number of platforms. Other programming languages such as rust have some form of cross-compilation, but from what i've seen certainly not to the same level as Zig, cross-compiliation has been made a first class use case in Zig and the platform support is still growing.

Using C libraries without bindings

One thing I've found to love about zig is it's C library support, and by that I mean there is literally no FFI or bindings needed, Zig can directly use C libraries without the need for wrappers, bindings, or an FFI. In some cases this even results in code being simpler than the C code itself, showing that sometimes Zig can be better at using C libraries than C itself is. Andrew Kelley once said that "Zig is better at using C libraries than C is at using C libraries." and after using the language for a while, I can say I agree to an extent.

Closing words

While I love zig, there are certainly downsides of it as with any language, in Zig's case it's the fact it's still fairly early in development and the ecosystem is quite microscopic in the grand scheme of things, but one thing I like to believe is that a language can't grow if it's not used so I encourage you to give the language a try, maybe make a wrapper or library or two or more! The language needs some love and attention, it's a great project with a great promise, and while it's goal of competing against C may be a bit of a big one especially for what it is currently, I do say it may be possible far in the future, it has promise, it has the ability to use C libraries so people can easily migrate, hell Zig can even act as a drop in C compiler and even translate C code to Zig (though very very roughly).

So should you learn Zig? Yes. I think you should.

Discussion (2)

pic
Editor guide
Collapse
darkwiiplayer profile image
DarkWiiPlayer

Zig has been on my radar for a while now and everyone I know who uses it seems quite happy with it. I might actually give it a try one of these days as well.

Collapse
hanna profile image
Hanna Author

I love it so far. It does have it's downsides ofc as every language does, but it's nice.