DEV Community

Dayvster 🌊
Dayvster 🌊

Posted on • Updated on • Originally published at dayvster.com

Zig is the Next Big Programming Language

What is Zig?

Zig is a general-purpose programming language designed for performance, safety, and simplicity. It aims to provide modern features while remaining lightweight and efficient. Developed with a focus on readability and robustness, Zig offers compile-time memory safety, error handling without exceptions, and predictable behavior. It supports a wide range of platforms, making it suitable for various application domains.

What that means is that Zig is a language that is designed to be easy to read, write and maintain, all while having a comparable performance and efficiency to C. With a few modern features that make it a joy to work with. I've been using Zig for a few years now (since 2020) and I've been loving it. It's been my go to language for a lot of my hobby projects, server side applications and even some zany web dev projects that I have running on my home network.

Can Zig Replace C?

That is hard to say, personally I think it's entirely possible, because zig is basically also a drop in compiler for C and C++. You can use Zig to compile your C and C++ code without ever needing to use GCC, Clang or MSVC. This basically means all C and C++ code can be compiled and even interop with Zig.

How does Zig manage memory?

Zig has a very unique way of handling memory allocation, which is drastically different from the approach that Rust took. With zig you have full control over how you allocate your memory and how you deallocate it. That means you have arena allocation, page allocation, general allocation etc. you can even create your own custom allocation and together with comptime access you can even make a custom allocator that will automatically deallocate memory when it goes out of scope, which may be super familiar to a lot of you Rust devs out there.

When does Zig outshine Rust?

The performance of Zig and Rust are pretty much neck and neck, that is to say the differences in performance are negligible. However Zig has a lot of cool features that Rust does not and most likely never will, such as the ability to compile C/C++ interop with C and C++ code, consume C and C++ libraries, manual memory management, comptime which is similar to rust macros but much more powerful and straightforward.

So I wouldn't say it's a matter of Zig outshining Rust, but rather Zig being a different tool with a different approach to solving problems. An approach that I personally find much more enjoyable and straightforward than Rust.

Can Zig compile C++?

Yes as mentioned above one of Zigs biggest strengths is it's ability to compile C++ and C code.

Can Zig be used for embedded systems?

Absolutely, I've used Zig on my Raspberry Pi and it's been a joy to work with. Now I know that a Raspberry Pi is not strictly the same as proper embedded systems programming, but it's at the very least comparable. Basically if you can run C on it you can run Zig on it for the most part. So yes Zig can be used for embedded systems programming.

Zig vs Go

Much like Go Zig is a systems programming language, but unlike Go Zig is a low level language that gives you full control over memory allocation and deallocation. Go on the other hand is a garbage collected language that does not give you that level of control. There are a lot of situations where Garbage collection is too slow or too inefficient or simply does not fit the use case. If that is your current use case then Zig would be a much better choice than Go. Of course on the other hand Zig is at the time of writing this article still not stable and no where near as mature as Go, so do put that into consideration.

Zig vs C++

Zig is a much simpler language than C++. While it also builds on the same principles as C++ and allow you to allocate and deallocate memory manually just like C++, it does not have the same level of complexity that C++ has. Zig is designed to be a modern language that is easy to read, write and maintain. C++ has been around for a while and powers some of the biggest and most vital applications in the world. But it is not without it's flaws, one of which that I specifically dislike is how many different ways you have to achieve what is essentially the same thing. Zig on the other hand is much more straightforward and simple.

Also Zig does not have nor will ever have classes, so if OOP something you can't live without then Zig may not be for you. But it'd still urge you to try it and experience a paradigm shift. I know I did and I've never looked back.

When will Zig be stable?

That is hard to say, the Zig team is working hard to get Zig to a stable release, but as of writing this article it is still in version 0.something. So it's hard to say when it will be stable. But what I can tell you is that you can already write a heck of a lot of stuff with Zig.

Does Zig have a package manager?

No zig does not currently have a package manager, one is planned and actively worked on and will most likely be released sometime in 2024. But for not we sadly have to manually manage our dependencies.

Does Zig have async?

No Zig at the moment of writign this article does not have async, but it is planned and actively worked on. So it's only a matter of time before Zig has async.
Also as a side note you can write your Zig applications out as multiple binaries and use the GNU scheduler to run them concurrently, which does a much better job than most async implementations in other languages, give it a try you may be pleasantly surprised.

Why should I use Zig?

Zig is a fantastic modern language that is a joy to use and work with. It reads wonderfully and gives you the developer full and complete autonomy over your entire application including how you will manage your memory. Instead of managing your memory behind the scenes it gives you the tools and the power to do that yourself. Personally I find this approach of teach instead of enforce much better and even more secure than Rusts approach of enforcing strict memory safety by having the developer jump through hoops. Not to mention that it's still entirely possible to write memory unsafe code in Rust, in fact it's painfully easy to do so.

So use zig if you want a modern, performant and memory safe language that is actively developed by a team of incredibly talented developers.

Is Zig hard to learn?

Zig the language itself is not hard to learn, I'd even say it's surprisingly simple. The main difficulty comes from the fact that Zig is a low level systems programming language, so you will have to understand some core Computer Science concepts and have a good low level understanding of how your system works. But if you are willing to put in the time and effort to learn Zig you will be rewarded.

Conclusion

Zig is a fantastic language that I've really loved using and will continue to use, in fact I will most likely write about it a lot more in the future. As it's a language that I really really enjoy working with and I firmly believe it has a bright future and a lot of potential to become a major player in the programming language space. With this article I hope I've given you a good overview of what Zig is and why you should consider using it and hopefully answered some of the questions you may have had about Zig. If you have any more questions feel free to reach out on Twitter, I'm always happy to help out and chat.

Top comments (2)

Collapse
 
rofrol profile image
Roman Frołow

that's not strictly the same as proper embedded systems programming

What do you mean by proper?

Also zig has package manager, though it does not have central package repository.

Collapse
 
isaalula profile image
Isa Al-Ula

Raspberry pi mostly run linux. Which even with real time kernel is not "real" real time.