DEV Community

Cover image for Rust vs. Zig: A Head-to-Head Comparison of Two Modern Systems Programming Languages
Kinanee Samson
Kinanee Samson

Posted on

Rust vs. Zig: A Head-to-Head Comparison of Two Modern Systems Programming Languages

Lots of new programming languages are trying to replace the C programming language, most of which have been tagged the C killer. Most notable in this category are the Zig and Rust both of which we have discussed very recently. Zig is a fast and highly performant low-level, compiled, statically typed systems programming language developed to be the ultimate replacement for C. Zig claims to be faster and more performant than C. Zig was designed by Andrew Kelley and was first released in March 2016.

Rust is a statically compiled, strongly typed, Multi-paradigm, general-purpose programming language that is designed to build low-level memory-safe applications. Rust was first announced in 2010 and it was released in 2015. Since then it has quickly gained popularity, and this is because Rust is designed to be performant Rust is one of the fastest programming languages available, and can often compete with C and C++ in terms of performance. You can read our review about Zig and Rust. In today's post, we will pit both programming languages side by side in an attempt to uncover which one of them is the ultimate Systems programming language.

We will consider both of them under the following points.

  • Memory Management
  • Safety
  • Community
  • Syntax
  • Live projects

Memory Management

Rust introduces a borrower-checker rule for handling memory management. Rust implements a system of rules that govern how data is allocated to memory in your application. This relieves you of the need to manually free up the memory used in your application. In Rust, the compiler will automatically free up used memory once its Owner goes out of scope. This implies that all memory used in Rust applications must have an owner. Rust also ensures that each bit of memory can only have one owner at a time.

In Zig, a pointer is not allowed to point to nothing, this is in stark contrast to C, where you can have empty pointers, Instead, Zig makes use of option types which is just separate type used to indicate data that is optionally empty. This can be compared to using a structure that contains a pointer and a boolean that determines whether the pointer is valid, but the boolean's state is implicitly managed by the language rather than having to be explicitly managed by the programmer.

Safety

Rust is a systems programming language that is designed to be fast. This is achieved by using it's borrow-checker. Rust's ownership system ensures that memory is managed efficiently and without leaks. Ownership is a set of rules that govern how memory is allocated and freed and they are enforced by the compiler, Rust's type system is very expressive and can be used to catch many common programming errors.

Zig is designed with safety in mind and it provides a debug mode to compile your code. The Debug mode is optimized for safety. It disables all of Zig's compiler optimizations and enables all of Zig's safety checks. The Debug mode is characterized by fast compilation speed, slow runtime performance, and a large binary size. Zig also ships with a standard testing library that enables you to test your code. Zig's std.testing test module is an effective tool that makes it simple to write and execute unit tests.

Community

Rust has a big ecosystem with a large and active community of developers with 67% of developers using Rust for hobbies and side projects. While 16% of developers use Rust for work. Although 80% of Rust developers are new users of the language with 1 year of experience or less. This is according to Jet Brains Developer Ecosystem. Rust has a growing number of corporate users, including Amazon, Facebook, Google, and Microsoft. This is a testament to the language's maturity and potential.

Zig is a relatively new programming language. The ecosystem behind Zig is not as big as Rust's ecosystem, there's only a handful of developers using Zig and I myself just recently learned about Zig.

Syntax

Rust's syntax is designed to be concise and readable. It is also designed to be safe. The syntax of Rust is similar to C++ but with some key differences, it also shares a bit of similarity to typescript when it comes to type annotation. Although the Borrow-Checker mechanism in Rust creates a bit of complexity for developers who are new to the language.

Zig is designed to be easy to learn and use from a beginner's perspective. If you've used any C-like language then the learning curve for Zig is significantly reduced. Zig attempts to keep most of the existing syntax of C in place and only introduces new syntax wherever it's necessary. Making Zig to be beginner-friendly.

Real World Application

Arguably one of the biggest apps to be built with Rust is the Linux Kernel, Rust has officially been adopted by the Linux community for actively building and maintaining the Linux kernel This is a major milestone for the language, and it shows that Rust is capable of supporting even the most demanding applications. The Rust Compiler itself is also written in Rust, Parity is a client for the Ethereum blockchain written in Rust. Rust is also being used by many companies in their production systems, including Amazon, Facebook, Google, and Microsoft.

The hottest application built with Zig right now is the Bun Javascript runtime, this is a runtime that executes Javascript on the server and it has full compatibility with NodeJS and npm. The Zig compiler is also written with Zig, Uber has also used the Zig C++ compiler to run Uber services on arm64 hardware via the Hermetic CC toolchain.

So which one of them is the ultimate replacement for C? I'd say that it depends, you should know that both programming languages allow you to build performant and memory-safe applications for low-level systems. Both programming language offers a set of unique features that makes each one of them special and in the end, comes down to your personality, the kind of application you'd want to build, and which one of them you're able to set up on your computer. Personally, I'd go with Zig because I just like going against what's trending. Anyway make sure to leave your thoughts in the comment section below, let me know which one of them you'd go with and why you'd go with it. I will see you in the next post.

Top comments (7)

Collapse
 
taikedz profile image
Tai Kedzierski

A pleasant and succinct overview. Both languages look promising at this stage.

Last conclusions however are to be taken lightly - I have a preference for Rust over Zig (personal reasons which aren't worth highlighting here), and a Linux advocate, but I would say it's a little misleading to claim

one of the biggest apps to be built with Rust is the Linux Kernel

implying that a major part of the kernel is written in rust - which is not true. The Linux kernel project has started accepting modules being written in rust to integrate with the kernel, and has thus added tooling to do so during kernel build. This is good 😁 but not quite "Linux is built in rust" 😅

It is worth noting that Microsoft has added Rust to its codebase too


Conversely, Zig's Bun project is indeed gaining good visibility 🎉

Collapse
 
kalashin1 profile image
Kinanee Samson

A huge milestone for the Rust community.

Collapse
 
tythos profile image
Brian Kirkpatrick

Ultimately I find that the Zig compiler is much less of an asshole than Rust. Between that and the Foundation drama (not to mention the ease of cross-integration with legacy C) I think Zig is going to be the focus of my attention for 2024. Now if only they can help the documentation and secondary tooling (like examples and regex libraries) keep up with the compiler releases!

Collapse
 
kalashin1 profile image
Kinanee Samson

I really hope that they do

Collapse
 
mslapek profile image
Michał Słapek

Nice article, my favorite part was "Real World Application". 😀

I think one of the major differences is aliasing: by default, Rust forbids mutable aliasing.

Zig (like C, C++, Python, etc...) allows mutable aliasing by default (there is a dedicated keyword in C/C++ to inform about lack of the mutable aliasing, en.cppreference.com/w/c/language/r...).

Collapse
 
kalashin1 profile image
Kinanee Samson

wow, I had no idea, thanks for your contribution.

Collapse
 
sergeyshandar profile image
Sergey Shandar

I would also mention meta-programming, by comparing amazing Zig compile-time reflection vs not so amazing Rust macros.