Zig Posts

10 posts in this category

Using Zig for Advent of Code
This year, I decided to use Zig to solve Advent of Code. I started learning Zig sometime ago but took a break. It was not something easy from the beginning, so in the first few days, I had to use…
Zig
How ArenaAllocator works
ArenaAllocator is one of Zig’s built-in allocators. It takes another allocator and wraps some code around to allow you to allocate memory without the need to free them individually. Every allocated…
Zig
Build system
There are 4 build modes in Zig, each can be enabled via the arguments -O ReleaseSafe, -O ReleaseSmall, and -O ReleaseFast when running zig run or zig test. Depending on which mode is enabled, runtime…
Zig
Handling errors
A function in Zig can return either an error or an actual value, most of the time you will see something like this: The return type of these functions is the combination of an error set type and an…
Zig
JSON in 5 minutes
Zig has built-in support for JSON via the std.json module. To convert any object into a JSON string, we use std.json.stringify(<input>, <options>, <output stream>): See StringifyOptions struct for…
Zig
Case Study: Implementing a Generic Stack
Implementing a stack is one of my favorite tests when working with a new language because it will show us a lot about the language’s ergonomics. We are going to make this stack a generic data…
Zig
Cross-compile C C++ and Zig programs
Zig came with a built-in C/C++ compiler zig cc and zig c++. So you can compile and run your C/C++ code with Zig! For example, let’s compile this simple C program: What’s more interesting is…
Zig
Comptime
One of the unique features of Zig is the ability to let us control what happens in our code, during both runtime and compile time. For optimization, Zig implicitly performs some evaluations at…
Zig
Where data is stored and how to choose an allocator
Zig has no runtime, hence, no automatic memory management. It’s the developer’s responsibility to manage the allocation/deallocation of memories. It’s important to know how memory allocation works…
Zig
Strings in 5 minutes
Just like C/C++, you can define a string in Zig with an array of bytes or as a null-terminated string literal. You can reference to any byte in the string just like an array: Or reference to a…
Zig