Need A New Programming Language? Try Zig

It’s possible you’ve read of it, possibly you have not. Zig is a new programming language that would seem to be growing in popularity. Let us do a speedy dive into what it is, why it is exceptional, and what sort of items you would use it for. (Ed Notice: Other than “for wonderful justice“, by natural means.)

What Is It?

You have probably heard of Rust as it has designed significant inroads in important small-stage infrastructures these types of as operating programs and embedded microcontrollers. As a gross oversimplification, it presents memory protection and numerous traditional runtime checks pushed to compile time. It has been the darling of quite a few posts listed here at Hackaday as it gives some exceptional strengths. With Rust on the increase, it helps make sense that there may possibly be some place for some new gamers. Languages like Julia, Go, Swift, and even Racket are all relative newcomers vying for the hugely coveted mindshare of software package engineers everywhere.

So let us chat Zig. In a wide perception, Zig is seriously attempting to present some of the security of Rust with the simplicity and simplicity of C. It touts a number of main functions this kind of as:

  • No hidden regulate stream
  • No concealed memory allocations
  • No preprocessor, no macros
  • First-course help for optional typical library
  • Interoperable by layout
  • Adjustable Runtime Safety
  • Compile-time code-execution

The previous a person, in certain, is perhaps the most appealing, but we’ll come back again to that. Let’s seem at some code, but skipping earlier good day entire world and headed straight to opening a file. Here’s the C++ code:


making use of namespace std
int key (int argc, char const *argv[]) 
  ifstream file("nonexistingfile.txt")

  char buffer[1024], sizeof(buffer))

  cout << buffer << endl&#13
  return 0&#13

Now let’s look at some comparable Zig code:

const std = @import("std")

using namespace std.fs

pub fn main() !void 
    const stdout =

    const file = try cwd().openFile(
        . .read = true ,
    defer file.close()

    var buffer: [1024]u8 = undefined
    const size = try file.readAll(buffer[0..])

    try stdout.writeAll(buffer[0..size])

(Thanks to Erik Engheim for the C++ and Zig sample code.)

As you might have guessed from the file name, the file doesn’t exist. The C++ code doesn’t explicitly check for any errors and in this scenario, it is perfectly valid code that displays no indication that anything failed. Zig, on the other hand, we have to do a try since that file could fail. When it does fail, you get a nice stack trace:

error: FileNotFound
/usr/local/Cellar/zig/0.7.0/lib/zig/std/os.zig:1196:23: 0x10b3ba52e in std.os.openatZ (fileopen)
            ENOENT => return mistake.FileNotFound,
/usr/community/Cellar/zig/.7./lib/zig/std/fs.zig:754:13: 0x10b3b857e in std.fs.Dir.openFileZ (fileopen)
            try out os.openatZ(self.fd, sub_path, os_flags, )
/usr/neighborhood/Cellar/zig/.7./lib/zig/std/fs.zig:687:9: 0x10b3b6c4b in std.fs.Dir.openFile (fileopen)
        return self.openFileZ(&route_c, flags)
~/Improvement/Zig/fileopen.zig:8:18: 0x10b3b6810 in most important (fileopen)
    const file = test cwd().openFile(

Removing the try final results in a compilation mistake. The backtrace in this article is particularly remarkable due to the fact this is a comparatively basic language without having a garbage collector, runtime, or digital device.

Let us discuss about some of Zig’s other capabilities: interoperable by style, adjustable runtime protection, and compile-time code execution.

“Interoperable by design” signifies that common Zig is easily eaten by C and in switch, consumes C. In lots of other languages, these as Python, you want to precisely marshall data for C and C++ interoperability. Zig can include things like C information right in the primary code by virtue of the constructed-in Clang compiler.  The output of Zig libraries is a .o file that can be fed appropriate into GCC. Functions can be applied by C code by just prepending export to the beginning of purpose definitions. Structs and datatypes have equivalent relieve.

“Adjustable runtime safety” usually means that quite a few of the runtime checks that Zig has can be turned on or off depending on the software. Things like integer overflow, bounds examining, unreachable code, and other individuals.

You could see in some code you have noticed that there is a knowledge type in Zig known as comptime. You can use it in functionality arguments and in the program alone. It signifies that the worth ought to be computable at compile time. It can be utilized to carry out a form of generics or templates. This is a really effective aspect that can be applied in interesting strategies.

What Would You Use It For?

Considering the fact that Zig is LLVM-based mostly, the targets for Zig include:

  • x86_64
  • ARM/ARM64
  • MIPS
  • PowerPC
  • WASM32
  • RISCV64
  • Sparc v9
  • Linux
  • MacOS
  • Windows
  • FreeBSD
  • DragonFly
  • UEFI

Presented that it interoperates with C so easily, it is really easy to swap out modest chunks or libraries for Zig equivalents.

In addition, Zig can be applied on microcontrollers. As a little bit of a cherry-picked illustration, [Kevin Lynagh] lately went by way of the journey of converting his keyboard firmware from Rust to Zig. Several of Rust’s perfectly-recognized language capabilities such as options, macros, and sample matching are utilised to initialize and scan ports for essential presses. In Zig, these are changed by inline for, a for loop that is unrolled at compile time, and some clever use of comptime. In individual [Kevin] details out the regularity of the language and how it is a language that he feels like he could grasp.

If you’re wanting for inspiration, there’s a Github repo with hundreds of exceptional illustrations published in Zig. There are Gameboy emulators, HTTP/DNS servers, ray tracers, a number of kernels and booters, databases, and compilers.

How Can I Get Began?

There is a studying section on Zig’s homepage as properly the web page that is chock-entire of great means. Ziglings is a Github task that has tiny damaged systems that require compact tweaks to get doing work yet again, permit you to get a sense for Zig. Probably just dipping your toes in the h2o isn’t sufficient, and you want to dive into the deep stop of the language implementation by itself.