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

Read More... Read More