const std = @import("std");
const testing = std.testing;
export fn add(a: i32, b: i32) i32 {
return a + b;
}
1 Introducing Zig
In this chapter, I want to introduce you to the world of Zig. Zig is a very young language that is being actively developed. As a consequence, it’s world is still very wild and to be explored. This book is my attempt to help you on your personal journey for understanding and exploring the exciting world of Zig.
I assume you have previous experience with some programming language in this book, not necessarily with a low-level one. So, if you have experience with Python, or Javascript, for example, is fine. But, if you do have experience with low-level languages, such as C, C++, or Rust, you will probably learn faster throughout this book.
1.1 What is Zig?
Zig is a modern, low-level, and general-purpose programming language. Some programmers think of Zig as a modern and better version of C.
In the author’s personal interpretation, Zig is tightly connected with “less is more”. Instead of trying to become a modern language by adding more and more features, many of the core improvements that Zig brings to the table are actually about removing annoying and evil behaviours/features from C and C++. In other words, Zig tries to be better by simplifying the language, and by having more consistent and robust behaviour. As a result, analyzing, writing and debugging applications become much easier and simpler in Zig, than it is in C or C++.
This philosophy becomes clear with the following phrase from the official website of Zig:
“Focus on debugging your application rather than debugging your programming language knowledge”.
This phrase is specially true for C++ programmers. Because C++ is a gigantic language, with tons of features, and also, there are lots of different “flavors of C++”. These elements are what makes C++ so much complex and hard to learn. Zig tries to go in the opposite direction. Zig is a very simple language, more closely related to other simple languages such as C and Go.
The phrase above is still important for C programmers too. Because, even C being a simple language, it is still hard sometimes to read and understand C code. For example, pre-processor macros in C are an evil source of confusion. They really makes it hard sometimes to debug C programs. Because macros are essentially a second language embedded in C that obscures your C code. With macros, you are no longer 100% sure about which pieces of code are being sent to the compiler. It obscures the actual source code that you wrote.
You don’t have macros in Zig. In Zig, the code you write, is the actual code that get’s compiled by the compiler. You don’t have evil features that obscures you code. You also don’t have hidden control flow happening behind the scenes. And, you also don’t have functions or operators from the standard library that make hidden memory allocations behind your back.
By being a simpler language, Zig becomes much more clear and easier to read/write, but at the same time, it also achieves a much more robust state, with more consistent behaviour in edge situations. Once again, less is more.
1.2 Hello world in Zig
We begin our journey in Zig by creating a small “Hello World” program. To start a new Zig project in your computer, you simply call the init
command from the zig
compiler. Just create a new directory in your computer, then, init a new Zig project inside this directory, like this:
mkdir hello_world
cd hello_world
zig init
info: created build.zig
info: created build.zig.zon
info: created src/main.zig
info: created src/root.zig
info: see `zig build --help` for a menu of options
1.2.1 Understanding the project files
After you run the init
command from the zig
compiler, some new files are created inside of your current directory. First, a “source” (src
) directory is created, containing two files, main.zig
and root.zig
. Each .zig
file is a separate Zig module, which is simply a text file that contains some Zig code.
By convention, the main.zig
module is where your main function lives. Thus, if you are building an executable program in Zig, you need to declare a main()
function, which represents the entrypoint of your program, i.e. it is where the execution of your program begins.
However, if you are building a library (instead of an executable program), then, the normal procedure is to delete this main.zig
file and start with the root.zig
module. By convention, the root.zig
module is the root source file of your library.
tree .
.
├── build.zig
├── build.zig.zon
└── src
├── main.zig
└── root.zig
1 directory, 4 files
The ìnit
command also creates two additional files in our working directory: build.zig
and build.zig.zon
. The first file (build.zig
) represents a build script written in Zig. This script is executed when you call the build
command from the zig
compiler. In other words, this file contain Zig code that executes the necessary steps to build the entire project.
In general, low-level languages normally use a compiler to build your source code into binary executables or binary libraries. Nevertheless, this process of compiling your source code and building binary executables or binary libraries from it, became a real challenge in the programming world, once the projects became bigger and bigger. As a result, programmers created “build systems”, which are a second set of tools designed to make this process of compiling and building complex projects, easier.
Examples of build systems are CMake, GNU Make, GNU Autoconf and Ninja, which are used to build complex C and C++ projects. With these systems, you can write scripts, which are called “build scripts”. They simply are scripts that describes the necessary steps to compile/build your project.
However, these are separate tools, that do not belong to C/C++ compilers, like gcc
or clang
. As a result, in C/C++ projects, you have not only to install and manage your C/C++ compilers, but you also have to install and manage these build systems separately.
But instead of using a separate build system, in Zig, we use the Zig language itself to write build scripts. In other words, Zig contains a native build system in it. And we can use this build system to write small scripts in Zig, which describes the necessary steps to build/compile our Zig project1. So, everything you need to build a complex Zig project is the zig
compiler, and nothing more.
The second generated file (build.zig.zon
) is the Zig package manager configuration file, where you can list and manage the dependencies of your project. Yes, Zig has a package manager (like pip
in Python, cargo
in Rust, or npm
in Javascript) called Zon, and this build.zig.zon
file is similar to the package.json
file in Javascript projects, or, the Pipfile
file in Python projects, or the Cargo.toml
file in Rust projects.
1.2.2 The file root.zig
Let’s take a look into the root.zig
file. You might have notice that every line of code with an expression ends with a semicolon (;
). This follows the syntax of a C-family programming language2.
Also, notice the @import()
call at the first line. We use this built-in function to import functionality from other Zig modules into our current module. In other words, this @import()
function works similarly to the #include
pre-processor in C or C++, or, to the import
statement in Python or Javascript code. In this example, we are importing the std
module, which gives you access to the Zig Standard Library.
In this root.zig
file, we can also see how assignments (i.e. creating new objects) are made in Zig. You can create a new object in Zig by using the following syntax (const|var) name = value;
. In the example below, we are creating two constant objects (std
and testing
). At Section 1.4 we talk more about objects in general.
Functions in Zig are declared using the fn
keyword. In this root.zig
module, we are declaring a function called add()
, which has two arguments named a
and b
. The function returns an integer of the type i32
as result.
Zig is not exactly a strongly-typed language. Because you can (if you want to) omit the type of an object in your code, if this type can be derived from the assigned value. But there are other situations where you do need to be explicit. For example, you do have to explicitly specify the type of every single function argument, and also, the return type of every function you create in Zig. So, at least in function declarations, Zig is a strongly-typed language.
We specify the type of an object or a function argument in Zig, by using a colon character (:
) followed by the type after the name of this object/function argument. With the expressions a: i32
and b: i32
, we know that, both a
and b
arguments have type i32
, which is a signed 32 bit integer. In this part, the syntax in Zig is identical to the syntax in Rust, which also specifies types by using the colon character.
Lastly, we have the return type of the function at the end of the line, before we open the curly braces to start writing the function’s body. In the example above, this type is also a signed 32 bit integer (i32
) value.
Notice that we also have an export
keyword before the function declaration. This keyword is similar to the extern
keyword in C. It exposes the function to make it available in the library API.
In other words, if you have a project where you are currently building a library for other people to use, you need to expose your functions so that they are available in the library’s API, so that users can use it. If we removed the export
keyword from the add()
function declaration, then, this function would be no longer exposed in the library object built by the zig
compiler.
Having that in mind, the keyword export
is a keyword used in libraries written in Zig. If you are not currently writing a library in your project, then, you do not need to care about this keyword.
1.2.3 The main.zig
file
Now that we have learned a lot about Zig’s syntax from the root.zig
file, let’s take a look at the main.zig
file. A lot of the elements we saw in root.zig
are also present in main.zig
. But we have some other elements that we did not have seen yet, so let’s dive in.
First, look at the return type of the main()
function in this file. We can see a small change. Now, the return type of the function (void
) is accompanied by an exclamation mark (!
). What this exclamation mark is telling us, is that this main()
function might also return an error.
So, in this example, the main()
function can either return void
, or, return an error. This is an interesting feature of Zig. If you write a function, and, something inside of the body of this function might return an error, then, you are forced to:
- either add the exclamation mark to the return type of the function, to make it clear that this function might return an error.
- or explicitly handle this error that might occur inside the function, to make sure that, if this error does happen, you are prepared, and your function will no longer return an error because you handled the error inside your function.
In most programming languages, we normally handle (or deals with) an error through a try catch pattern, and Zig, this is no different. But, if we look at the main()
function below, you can see that we do have a try
keyword in the 5th line. But we do not have a catch
keyword in this code.
This means that, we are using the keyword try
to execute a code that might return an error, which is the stdout.print()
expression. But because we do not have a catch
keyword in this line, we are not treating (or dealing with) this error. So, if this expression do return an error, we are not catching and solving this error in any way. That is why the exclamation mark was added to the return type of the function.
So, in essence, the try
keyword executes the expression stdout.print()
. If this expression returns a valid value, then, the try
keyword do nothing essentially. It simply passes this value forward. But, if the expression do return an error, then, the try
keyword will unwrap and return this error from the function, and also print it’s stack trace to stderr
.
This might sound weird to you, if you come from a high-level language. Because in high-level languages, such as Python, if an error occurs somewhere, this error is automatically returned and the execution of your program will automatically stops, even if you don’t want to stop the execution. You are obligated to face the error.
But if you come from a low-level language, then, maybe, this idea do not sound so weird or distant to you. Because in C for example, normally functions doesn’t raise errors, or, they normally don’t stop the execution. In C, error handling is done by constantly checking the return value of the function. So, you run the function, and then, you use an if statement to check if the function returned a value that is valid, or, if it returned an error. If an error was returned from the function, then, the if statement will execute some code that fixes this error.
So, at least for C programmers, they do need to write a lot of if statements to constantly check for errors around their code. And because of that, this simple feature from Zig, might be extraordinary for them. Because this try
keyword can automatically unwrap the error, and warn you about this error, and let you deal with it, without any extra work from the programmer.
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello, {s}!\n", .{"world"});
}
Now, another thing that you might have noticed in this code example, is that the main()
function is marked with the pub
keyword. This keyword means “public”. It marks the main()
function as a public function from this module.
In other words, every function that you declare in your Zig module is, by default, a private (or “static”) function that belongs to this Zig module, and can only be used (or called) from within this same module. Unless, you explicitly mark this function as a public function with the pub
keyword. This means that the pub
keyword in Zig do essentially the opposite of what the static
keyword do in C/C++.
By making a function “public”, you allow other Zig modules to access and call this function, and use it for they own purposes. all these other Zig modules need to do is, to import your module with the @import()
built-in function. Then, they get access to all public functions that are present in your Zig module.
1.2.4 Compiling your source code
You can compile your Zig modules into a binary executable by running the build-exe
command from the zig
compiler. You simply list all the Zig modules that you want to build after the build-exe
command, separated by spaces. In the example below, we are compiling the module main.zig
.
zig build-exe src/main.zig
Since we are building an executable, the zig
compiler will look for a main()
function declared in any of the files that you list after the build-exe
command. If the compiler does not find a main()
function declared somewhere, a compilation error will be raised, warning about this mistake.
The zig
compiler also offers a build-lib
and build-obj
commands, which work the exact same way as the build-exe
command. The only difference is that, they compile your Zig modules into a portale C ABI library, or, into object files, respectively.
In the case of the build-exe
command, a binary executable file is created by the zig
compiler in the root directory of your project. If we take a look now at the contents of our current directory, with a simple ls
command, we can see the binary file called main
that was created by the compiler.
ls
build.zig build.zig.zon main src
If I execute this binary executable, I get the “Hello World” message in the terminal , as we expected.
./main
Hello, world!
1.2.5 Compile and execute at the same time
On the previous section, I presented the zig build-exe
command, which compiles Zig modules into an executable file. However, this means that, in order to execute the executable file, we have to run two different commands. First, the zig build-exe
command, and then, we call the executable file created by the compiler.
But what if we wanted to perform these two steps, all at once, in a single command? We can do that by using the zig run
command.
zig run src/main.zig
Hello, world!
1.2.6 Compiling the entire project
Just as I described at Section 1.2.1, as our project grows in size and complexity, we usually prefer to organize the compilation and build process of the project into a build script, using some sort of “build system”.
In other words, as our project grows in size and complexity, the build-exe
, build-lib
and build-obj
commands become harder to use directly. Because then, we start to list multiple and multiple modules at the same time. We also start to add built-in compilation flags to customize the build process for our needs, etc. It becomes a lot of work to write the necessary commands by hand.
In C/C++ projects, programmers normally opt to use CMake, Ninja, Makefile
or configure
scripts to organize this process. However, in Zig, we have a native build system in the language itself. So, we can write build scripts in Zig to compile and build Zig projects. Then, all we need to do, is to call the zig build
command to build our project.
So, when you execute the zig build
command, the zig
compiler will search for a Zig module named build.zig
inside your current directory, which should be your build script, containing the necessary code to compile and build your project. If the compiler do find this build.zig
file in your directory, then, the compiler will essentially execute a zig run
command over this build.zig
file, to compile and execute this build script, which in turn, will compile and build your entire project.
zig build
After you execute this “build project” command, a zig-out
directory is created in the root of your project directory, where you can find the binary executables and libraries created from your Zig modules accordingly to the build commands that you specified at build.zig
. We will talk more about the build system in Zig latter in this book.
In the example below, I’m executing the binary executable named hello_world
that was generated by the compiler after the zig build
command.
./zig-out/bin/hello_world
Hello, world!
1.3 How to learn Zig?
What are the best strategies to learn Zig? First of all, of course this book will help you a lot on your journey through Zig. But you will also need some extra resources if you want to be really good at Zig.
As a first tip, you can join a community with Zig programmers to get some help , when you need it:
- Reddit forum: https://www.reddit.com/r/Zig/;
- Ziggit community: https://ziggit.dev/;
- Discord, Slack, Telegram, and others: https://github.com/ziglang/zig/wiki/Community;
Now, one of the best ways to learn Zig is to simply read Zig code. Try to read Zig code often, and things will become more clear. A C/C++ programmer would also probably give you this same tip. Because this strategy really works!
Now, where you can find Zig code to read? I personally think that, the best way of reading Zig code is to read the source code of the Zig Standard Library. The Zig Standard Library is available at the lib/std
folder3 on the official GitHub repository of Zig. Access this folder, and start exploring the Zig modules.
Also, a great alternative is to read code from other large Zig codebases, such as:
- the Javascript runtime Bun4.
- the game engine Mach5.
- a LLama 2 LLM model implementation in Zig6.
- the financial transactions database
tigerbeetle
7. - the command-line arguments parser
zig-clap
8. - the UI framework
capy
9. - the Language Protocol implementation for Zig,
zls
10. - the event-loop library
libxev
11.
All these assets are available on GitHub, and this is great, because we can use the GitHub search bar in our advantage, to find Zig code that fits our description. For example, you can always include lang:Zig
in the GitHub search bar when you are searching for a particular pattern. This will limit the search to only Zig modules.
Also, a great alternative is to consult online resources and documentations. Here is a quick list of resources that I personally use from time to time to learn more about the language each day:
- Zig Language Reference: https://ziglang.org/documentation/master/;
- Zig Standard Library Reference: https://ziglang.org/documentation/master/std/;
- Zig Guide: https://zig.guide/;
- Karl Seguin Blog: https://www.openmymind.net/;
- Zig News: https://zig.news/;
- Read the code written by one of the Zig core team members: https://github.com/kubkon;
- Some livecoding sessions are transmitted in the Zig Showtime Youtube Channel: https://www.youtube.com/@ZigSHOWTIME/videos;
Another great strategy to learn Zig, or honestly, to learn any language you want, is to practice it by solving exercises. For example, there is a famous repository in the Zig community called Ziglings12 , which contains more than 100 small exercises that you can solve. It is a repository of tiny programs written in Zig that are currently broken, and your responsibility is to fix these programs, and make them work again.
A famous tech YouTuber known as The Primeagen also posted some videos (at YouTube) where he solves these exercises from Ziglings. The first video is named “Trying Zig Part 1”13.
Another great alternative, is to solve the Advent of Code exercises14. There are people that already took the time to learn and solve the exercises, and they posted their solutions on GitHub as well, so, in case you need some resource to compare while solving the exercises, you can look at these two repositories:
1.4 Creating new objects in Zig (i.e. identifiers)
Let’s talk more about objects in Zig. Readers that have past experience with other programming languages might know this concept through a different name, such as: “variable” or “identifier”. In this book, I choose to use the term “object” to refer to this concept.
To create a new object (or a new “identifier”) in Zig, we use the keywords const
or var
. These keywords specificy if the object that you are creating is mutable or not. If you use const
, then the object you are creating is a constant (or immutable) object, which means that once you declare this object, you can no longer change the value stored inside this object.
On the other side, if you use var
, then, you are creating a variable (or mutable) object. You can change the value of this object as many times you want. Using the keyword var
in Zig is similar to using the keywords let mut
in Rust.
1.4.1 Constant objects vs variable objects
In the code example below, we are creating a new constant object called age
. This object stores a number representing the age of someone. However, this code example does not compiles succesfully. Because on the next line of code, we are trying to change the value of the object age
to 25.
The zig
compiler detects that we are trying to change the value of an object/identifier that is constant, and because of that, the compiler will raise a compilation error, warning us about the mistake.
const age = 24;
// The line below is not valid!
25; age =
t.zig:10:5: error: cannot assign to constant
age = 25;
~~^~~
In contrast, if you use var
, then, the object created is a variable object. With var
you can declare this object in your source code, and then, change the value of this object how many times you want over future points in your source code.
So, using the same code example exposed above, if I change the declaration of the age
object to use the var
keyword, then, the program gets compiled succesfully. Because now, the zig
compiler detects that we are changing the value of an object that allows this behaviour, because it is an “variable object”.
var age: u8 = 24;
25; age =
1.4.2 Declaring without an initial value
By default, when you declare a new object in Zig, you must give it an initial value. In other words, this means that we have to declare, and, at the same time, initialize every object we create in our source code.
On the other hand, you can, in fact, declare a new object in your source code, and not give it an explicit value. But we need to use a special keyword for that, which is the undefined
keyword.
Is important to emphasize that, you should avoid using undefined
as much as possible. Because when you use this keyword, you leave your object uninitialized, and, as a consequence, if for some reason, your code use this object while it is uninitialized, then, you will definitely have undefined behaviour and major bugs in your program.
In the example below, I’m declaring the age
object again. But this time, I do not give it an initial value. The variable is only initialized at the second line of code, where I store the number 25 in this object.
var age: u8 = undefined;
25; age =
Having these points in mind, just remember that you should avoid as much as possible to use undefined
in your code. Always declare and initialize your objects. Because this gives you much more safety in your program. But in case you really need to declare an object without initializing it… the undefined
keyword is the way to do it in Zig.
1.4.3 There is no such thing as unused objects
Every object (being constant or variable) that you declare in Zig must be used in some way. You can give this object to a function call, as a function argument, or, you can use it in another expression to calculate the value of another object, or, you can call a method that belongs to this particular object.
It doesn’t matter in which way you use it. As long as you use it. If you try to break this rule, i.e. if your try to declare a object, but not use it, the zig
compiler will not compile your Zig source code, and it will issue a error message warning that you have unused objects in your code.
Let’s demonstrate this with an example. In the source code below, we declare a constant object called age
. If you try to compile a simple Zig program with this line of code below, the compiler will return an error as demonstrated below:
const age = 15;
t.zig:4:11: error: unused local constant
const age = 15;
^~~
Everytime you declare a new object in Zig, you have two choices:
- you either use the value of this object;
- or you explicitly discard the value of the object;
To explicitly discard the value of any object (constant or variable), all you need to do is to assign this object to an special character in Zig, which is the underscore (_
). When you assign an object to a underscore, like in the example below, the zig
compiler will automatically discard the value of this particular object.
You can see in the example below that, this time, the compiler did not complain about any “unused constant”, and succesfully compiled our source code.
// It compiles!
const age = 15;
_ = age;
Now, remember, everytime you assign a particular object to the underscore, this object is essentially destroyed. It is discarded by the compiler. This means that you can no longer use this object further in your code. It doesn’t exist anymore.
So if you try to use the constant age
in the example below, after we discarded it, you will get a loud error message from the compiler (talking about a “pointless discard”) warning you about this mistake.
// It does not compile.
const age = 15;
_ = age;// Using a discarded value!
"{d}\n", .{age + 2}); std.debug.print(
t.zig:7:5: error: pointless discard
of local constant
This same rule applies to variable objects. Every variable object must also be used in some way. And if you assign a variable object to the underscore, this object also get’s discarded, and you can no longer use this object.
1.4.4 You must mutate every variable objects
Every variable object that you create in your source code must be mutated at some point. In other words, if you declare an object as a variable object, with the keyword var
, and you do not change the value of this object at some point in the future, the zig
compiler will detect this, and it will raise an error warning you about this mistake.
The concept behind this is that every object you create in Zig should be preferably a constant object, unless you really need an object whose value will change during the execution of your program.
So, if I try to declare a variable object such as where_i_live
below, and I do not change the value of this object in some way, the zig
compiler raises an error message with the phrase “variable is never mutated”.
var where_i_live = "Belo Horizonte";
_ = where_i_live;
t.zig:7:5: error: local variable is never mutated
t.zig:7:5: note: consider using 'const'
1.5 Primitive Data Types
Zig have many different primitive data types available for you to use. You can see the full list of available data types at the official Language Reference page15.
But here is a quick list:
- Unsigned integers:
u8
, 8-bit integer;u16
, 16-bit integer;u32
, 32-bit integer;u64
, 64-bit integer;u128
, 128-bit integer. - Signed integers:
i8
, 8-bit integer;i16
, 16-bit integer;i32
, 32-bit integer;i64
, 64-bit integer;i128
, 128-bit integer. - Float number:
f16
, 16-bit floating point;f32
, 32-bit floating point;f64
, 64-bit floating point;f128
, 128-bit floating point; - Boolean:
bool
, represents true or false values. - C ABI compatible types:
c_long
,c_char
,c_short
,c_ushort
,c_int
,c_uint
, and many others. - Pointer sized integers:
isize
andusize
.
1.6 Arrays
You create arrays in Zig by using a syntax that resembles the C syntax. First, you specify the size of the array (i.e. the number of elements that will be stored in the array) you want to create inside a pair of brackets.
Then, you specify the data type of the elements that will be stored inside this array. All elements present in an array in Zig must have the same data type. For example, you cannot mix elements of type f32
with elements of type i32
in the same array.
After that, you simply list the values that you want to store in this array inside a pair of curly braces. In the example below, I am creating two constant objets that contain different arrays. The first object contains an array of 4 integer values, while the second object, an array of 3 floating point values.
Now, you should notice that in the object ls
, I am not explicitly specifying the size of the array inside of the brackets. Instead of using a literal value (like the value 4 that I used in the ns
object), I am using the special character underscore (_
). This syntax tells the zig
compiler to fill this field with the number of elements listed inside of the curly braces. So, this syntax [_]
is for lazy (or smart) programmers who leave the job of counting how many elements there are in the curly braces for the compiler.
const ns = [4]u8{48, 24, 12, 6};
const ls = [_]f64{432.1, 87.2, 900.05};
_ = ns; _ = ls;
Is worth noting that these are static arrays, meaning that they cannot grow in size. Once you declare your array, you cannot change the size of it. This is very commom in low level languages. Because low level languages normally wants to give you (the programmer) full control over memory, and the way in which arrays are expanded is tightly related to memory management.
1.6.1 Selecting elements of the array
One very commom activity is to select specific portions of an array you have in your source code. In Zig, you can select a specific element from your array, by simply providing the index of this particular element inside brackets after the object name. In the example below, I am selecting the third element from the ns
array. Notice that Zig is a “zero-index” based language, like C, C++, Rust, Python, and many other languages.
const ns = [4]u8{48, 24, 12, 6};
try stdout.print("{d}\n", .{ ns[2] });
12
In contrast, you can also select specific slices (or sections) of your array, by using a range selector. Some programmers also call these selectors of “slice selectors”, and they also exist in Rust, and have the exact same syntax as in Zig. Anyway, a range selector is a special expression in Zig that defines a range of indexes, and it have the syntax start..end
.
In the example below, at the second line of code, the sl
object stores a slice (or a portion) of the ns
array. More precisely, the elements at index 1 and 2 in the ns
array.
const ns = [4]u8{48, 24, 12, 6};
const sl = ns[1..3];
_ = sl;
When you use the start..end
syntax, the “end tail” of the range selector is non-inclusive, meaning that, the index at the end is not included in the range that is selected from the array. Therefore, the syntax start..end
actually means start..end - 1
in practice.
You can for example, create a slice that goes from the first to the last elements of the array, by using ar[0..ar.len]
syntax In other words, it is a slice that access all elements in the array.
const ar = [4]u8{48, 24, 12, 6};
const sl = ar[0..ar.len];
_ = sl;
You can also use the syntax start..
in your range selector. Which tells the zig
compiler to select the portion of the array that begins at the start
index until the last element of the array. In the example below, we are selecting the range from index 1 until the end of the array.
const ns = [4]u8{48, 24, 12, 6};
const sl = ns[1..];
_ = sl;
1.6.2 More on slices
As we discussed before, in Zig, you can select specific portions of an existing array. This is called slicing in Zig (Sobeston 2024), because when you select a portion of an array, you are creating a slice object from that array.
A slice object is essentially a pointer object accompained by a length number. The pointer object points to the first element in the slice, and the length number tells the zig
compiler how many elements there are in this slice.
Slices can be thought of as a pair of
[*]T
(the pointer to the data) and ausize
(the element count) (Sobeston 2024).
Through the pointer contained inside the slice you can access the elements (or values) that are inside this range (or portion) that you selected from the original array. But the length number (which you can access through the len
property of your slice object) is the really big improvement (over C arrays for example) that Zig brings to the table here.
Because with this length number the zig
compiler can easily check if you are trying to access an index that is out of the bounds of this particular slice, or, if you are causing any buffer overflow problems. In the example below, we access the len
property of the slice sl
, which tells us that this slice have 2 elements in it.
const ns = [4]u8{48, 24, 12, 6};
const sl = ns[1..3];
try stdout.print("{d}\n", .{sl.len});
2
1.6.3 Array operators
There are two array operators available in Zig that are very useful. The array concatenation operator (++
), and the array multiplication operator (**
). As the name suggests, these are array operators.
One important detail about these two operators is that they work only when both operands have a size (or “length”) that is compile-time known. We are going to talk more about the differences between “compile-time known” and “runtime known” at Section 3.1.1. But for now, keep this information in mind, that you cannot use these operators in every situation.
In summary, the ++
operator creates a new array that is the concatenation, of both arrays provided as operands. So, the expression a ++ b
produces a new array which contains all the elements from arrays a
and b
.
const a = [_]u8{1,2,3};
const b = [_]u8{4,5};
const c = a ++ b;
try stdout.print("{any}\n", .{c});
{ 1, 2, 3, 4, 5 }
This ++
operator is particularly useful to concatenate strings together. Strings in Zig are described in depth at Section 1.8. In summary, a string object in Zig is essentially an arrays of bytes. So, you can use this array concatenation operator to effectively concatenate strings together.
In contrast, the **
operator is used to replicate an array multiple times. In other words, the expression a ** 3
creates a new array which contains the elements of the array a
repeated 3 times.
const a = [_]u8{1,2,3};
const c = a ** 2;
try stdout.print("{any}\n", .{c});
{ 1, 2, 3, 1, 2, 3 }
1.7 Blocks and scopes
Blocks are created in Zig by a pair of curly braces. A block is just a group of expressions (or statements) contained inside of a pair of curly braces. All of these expressions that are contained inside of this pair of curly braces belongs to the same scope.
In other words, a block just delimits a scope in your code. The objects that you define inside the same block belongs to the same scope, and, therefore, are accessible from within this scope. At the same time, these objects are not accessible outside of this scope. So, you could also say that blocks are used to limit the scope of the objects that you create in your source code. In less technical terms, blocks are used to specify where in your source code you can access whatever object you have in your source code.
So, a block is just a group of expressions contained inside a pair of curly braces. And every block have it’s own scope separated from the others. The body of a function is a classic example of a block. If statements, for and while loops (and any other structure in the language that uses the pair of curly braces) are also examples of blocks.
This means that, every if statement, or for loop, etc., that you create in your source code have it’s own separate scope. That is why you can’t access the objects that you defined inside of your for loop (or if statement) in an outer scope, i.e. a scope outside of the for loop. Because you are trying to access an object that belongs to a scope that is different than your current scope.
You can create blocks within blocks, with multiple levels of nesting. You can also (if you want to) give a label to a particular block, with the colon character (:
). Just write label:
before you open the pair of curly braces that delimits your block. When you label a block in Zig, you can use the break
keyword to return a value from this block, like as if it was a function’s body. You just write the break
keyword, followed by the block label in the format :label
, and the expression that defines the value that you want to return.
Like in the example below, where we are returning the value from the y
object from the block add_one
, and saving the result inside the x
object.
var y: i32 = 123;
const x = add_one: {
1;
y += break :add_one y;
};
if (x == 124 and y == 124) {
try stdout.print("Hey!", .{});
}
Hey!
1.8 How strings work in Zig?
The first project that we are going to build and discuss in this book is a base64 encoder/decoder (Chapter 4). But in order for us to build such a thing, we need to get a better understanding on how strings work in Zig. So let’s discuss this specific aspect of Zig.
In Zig, a string literal value is just a pointer to a null-terminated array of bytes (i.e. the same thing as a C string). However, a string object in Zig is a little more than just a pointer. A string object in Zig is an object of type []const u8
, and, this object always contains two things: the same null-terminated array of bytes that you would find in a string literal value, plus a length value. Each byte in this “array of bytes” is represented by an u8
value, which is an unsigned 8 bit integer, so, it is equivalent to the C data type unsigned char
.
// This is a string literal value:
"A literal value";
// This is a string object:
const object: []const u8 = "A string object";
Zig always assumes that this sequence of bytes is UTF-8 encoded. This might not be true for every sequence of bytes you have it, but is not really Zig’s job to fix the encoding of your strings (you can use iconv
16 for that). Today, most of the text in our modern world, specially on the web, should be UTF-8 encoded. So if your string literal is not UTF-8 encoded, then, you will likely have problems in Zig.
Let’s take for example the word “Hello”. In UTF-8, this sequence of characters (H, e, l, l, o) is represented by the sequence of decimal numbers 72, 101, 108, 108, 111. In xecadecimal, this sequence is 0x48
, 0x65
, 0x6C
, 0x6C
, 0x6F
. So if I take this sequence of hexadecimal values, and ask Zig to print this sequence of bytes as a sequence of characters (i.e. a string), then, the text “Hello” will be printed into the terminal:
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
const bytes = [_]u8{0x48, 0x65, 0x6C, 0x6C, 0x6F};
try stdout.print("{s}\n", .{bytes});
}
Hello
If you want to see the actual bytes that represents a string in Zig, you can use a for
loop to iterate through each byte in the string, and ask Zig to print each byte as an hexadecimal value to the terminal. You do that by using a print()
statement with the X
formatting specifier, like you would normally do with the printf()
function17 in C.
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
const string_object = "This is an example of string literal in Zig";
try stdout.print("Bytes that represents the string object: ", .{});
for (string_object) |byte| {
try stdout.print("{X} ", .{byte});
}
try stdout.print("\n", .{});
}
Bytes that represents the string object: 54 68 69
73 20 69 73 20 61 6E 20 65 78 61 6D 70 6C 65 20 6F
F 66 20 73 74 72 69 6E 67 20 6C 69 74 65 72 61 6C 2
20 69 6E 20 5A 69 67
1.8.1 Strings in C
At first glance, this looks very similar to how C treats strings as well. In more details, string values in C are treated internally as an array of arbitrary bytes, and this array is also null-terminated.
But one key difference between a Zig string and a C string, is that Zig also stores the length of the array inside the string object. This small detail makes your code safer, because is much easier for the Zig compiler to check if you are trying to access an element that is “out of bounds”, i.e. if your trying to access memory that does not belong to you.
To achieve this same kind of safety in C, you have to do a lot of work that kind of seems pointless. So getting this kind of safety is not automatic and much harder to do in C. For example, if you want to track the length of your string troughout your program in C, then, you first need to loop through the array of bytes that represents this string, and find the null element ('\0'
) position to discover where exactly the array ends, or, in other words, to find how much elements the array of bytes contain.
To do that, you would need something like this in C. In this example, the C string stored in the object array
is 25 bytes long:
#include <stdio.h>
int main() {
char* array = "An example of string in C";
int index = 0;
while (1) {
if (array[index] == '\0') {
break;
}
++;
index}
("Number of elements in the array: %d\n", index);
printf}
Number of elements in the array: 25
But in Zig, you do not have to do this, because the object already contains a len
field which stores the length information of the array. As an example, the string_object
object below is 43 bytes long:
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
const string_object = "This is an example of string literal in Zig";
try stdout.print("{d}\n", .{string_object.len});
}
43
1.8.2 A better look at the object type
Now, we can inspect better the type of objects that Zig create. To check the type of any object in Zig, you can use the @TypeOf()
function. If we look at the type of the simple_array
object below, you will find that this object is a array of 4 elements. Each element is a signed integer of 32 bits which corresponds to the data type i32
in Zig. That is what an object of type [4]i32
is.
But if we look closely at the type of the string_object
object below, you will find that this object is a constant pointer (hence the *const
annotation) to an array of 43 elements (or 43 bytes). Each element is a single byte (more precisely, an unsigned 8 bit integer - u8
), that is why we have the [43:0]u8
portion of the type below. In other words, the string stored inside the string_object
object is 43 bytes long. That is why you have the type *const [43:0]u8
below.
In the case of string_object
, it is a constant pointer (*const
) because the object string_object
is declared as constant in the source code (in the line const string_object = ...
). So, if we changed that for some reason, if we declare string_object
as a variable object (i.e. var string_object = ...
), then, string_object
would be just a normal pointer to an array of unsigned 8-bit integers (i.e. * [43:0]u8
).
Now, if we create an pointer to the simple_array
object, then, we get a constant pointer to an array of 4 elements (*const [4]i32
), which is very similar to the type of the string_object
object. This demonstrates that a string object (or a string literal) in Zig is already a pointer to an array.
Just remember that a “pointer to an array” is different than an “array”. So a string object in Zig is a pointer to an array of bytes, and not simply an array of bytes.
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
const string_object = "This is an example of string literal in Zig";
const simple_array = [_]i32{1, 2, 3, 4};
try stdout.print("Type of array object: {}", .{@TypeOf(simple_array)});
try stdout.print(
"Type of string object: {}",
{@TypeOf(string_object)}
.
);try stdout.print(
"Type of a pointer that points to the array object: {}",
{@TypeOf(&simple_array)}
.
);}
Type of array object: [4]i32
Type of string object: *const [43:0]u8
Type of a pointer that points to
the array object: *const [4]i32
1.8.3 Byte vs unicode points
Is important to point out that each byte in the array is not necessarily a single character. This fact arises from the difference between a single byte and a single unicode point.
The encoding UTF-8 works by assigning a number (which is called a unicode point) to each character in the string. For example, the character “H” is stored in UTF-8 as the decimal number 72. This means that the number 72 is the unicode point for the character “H”. Each possible character that can appear in a UTF-8 encoded string have its own unicode point.
For example, the Latin Capital Letter A With Stroke (Ⱥ) is represented by the number (or the unicode point) 570. However, this decimal number (570) is higher than the maximum number stored inside a single byte, which is 255. In other words, the maximum decimal number that can be represented with a single byte is 255. That is why, the unicode point 570 is actually stored inside the computer’s memory as the bytes C8 BA
.
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
const string_object = "Ⱥ";
try stdout.print("Bytes that represents the string object: ", .{});
for (string_object) |char| {
try stdout.print("{X} ", .{char});
}
}
Bytes that represents the string object: C8 BA
This means that to store the character Ⱥ in an UTF-8 encoded string, we need to use two bytes together to represent the number 570. That is why the relationship between bytes and unicode points is not always 1 to 1. Each unicode point is a single character in the string, but not always a single byte corresponds to a single unicode point.
All of this means that if you loop trough the elements of a string in Zig, you will be looping through the bytes that represents that string, and not through the characters of that string. In the Ⱥ example above, the for loop needed two iterations (instead of a single iteration) to print the two bytes that represents this Ⱥ letter.
Now, all english letters (or ASCII letters if you prefer) can be represented by a single byte in UTF-8. As a consequence, if your UTF-8 string contains only english letters (or ASCII letters), then, you are lucky. Because the number of bytes will be equal to the number of characters in that string. In other words, in this specific situation, the relationship between bytes and unicode points is 1 to 1.
But on the other side, if your string contains other types of letters… for example, you might be working with text data that contains, chinese, japanese or latin letters, then, the number of bytes necessary to represent your UTF-8 string will likely be much higher than the number of characters in that string.
If you need to iterate through the characters of a string, instead of its bytes, then, you can use the std.unicode.Utf8View
struct to create an iterator that iterates through the unicode points of your string.
In the example below, we loop through the japanese characters “アメリカ”. Each of the four characters in this string is represented by three bytes. But the for loop iterates four times, one iteration for each character/unicode point in this string:
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
var utf8 = (
try std.unicode.Utf8View.init("アメリカ"))
(
.iterator()
);while (utf8.nextCodepointSlice()) |codepoint| {
try stdout.print(
"got codepoint {}\n",
{std.fmt.fmtSliceHexUpper(codepoint)}
.
);}
}
got codepoint E382A2
got codepoint E383A1
got codepoint E383AA
got codepoint E382AB
1.9 Safety in Zig
A general trend in modern low-level programming languages is safety. As our modern world become more interconnected with techology and computers, the data produced by all of this technology becomes one of the most important (and also, one of the most dangerous) assets that we have.
This is probably the main reason why modern low-level programming languages have been giving great attention to safety, specially memory safety, because memory corruption is still the main target for hackers to exploit. The reality is that we don’t have an easy solution for this problem. For now, we only have techniques and strategies that mitigates these problems.
As Richard Feldman explains on his most recent GOTO conference talk18 , we haven’t figured it out yet a way to achieve true safety in technology. In other words, we haven’t found a way to build software that won’t be exploited with 100% certainty. We can greatly reduce the risks of our software being exploited, by ensuring memory safety for example. But this is not enough to achieve “true safety” territory.
Because even if you write your program in a “safe language”, hackers can still exploit failures in the operational system where your program is running (e.g. maybe the system where your code is running have a “backdoor exploit” that can still affect your code in unexpected ways), or also, they can exploit the features from the architecture of your computer. A recently found exploit that involves memory invalidation through a feature of “memory tags” present in ARM chips is an example of that (Kim et al. 2024).
The question is: what Zig and other languages have been doing to mitigate this problem? If we take Rust as an example, Rust is, for the most part19, a memory safe language by enforcing specific rules to the developer. In other words, the key feature of Rust, the borrow checker, forces you to follow a specific logic when you are writing your Rust code, and the Rust compiler will always complain everytime you try to go out of this pattern.
In contrast, the Zig language is not a memory safe language by default. Instead of forcing the developer to follow a specific rule, the Zig language achieves memory safety by offering tools that the developer can use for this purpose. In other words, the zig
compiler does not obligates you to use such tools. But there is often no reason to not use these tools in your Zig code, so you often achieve a similar level of memory safety of Rust in Zig by simply using these tools.
The tools listed below are related to memory safety in Zig. That is, they help you to achieve memory safety in your Zig code:
defer
allows you to keep free operations phisically close to allocations. This helps you to avoid memory leaks, “use after free”, and also “double-free” problems. Furthermore, it also keeps free operations logically tied to the end of the current scope, which greatly reduces the mental overhead about object lifetime.errdefer
helps you to garantee that your program frees the allocated memory, even if a runtime error occurs.- pointers and object are non-nullable by default. This helps you to avoid memory problems that might arise from de-referencing null pointers.
- Zig offers some native types of allocators (called “testing allocators”) that can detect memory leaks and double-frees. These types of allocators are widely used on unit tests, so they make your unit tests a weapon that you can use to detect memory problems in your code.
- arrays and slices in Zig have their lengths embedded in the object itself, which makes the
zig
compiler very effective on detecting “index out-of-range” type of errors, and avoiding buffer overflows.
Despite these features that Zig offers that are related to memory safety issues, the language also have some rules that help you to achieve another type of safety, which is more related to program logic safety. These rules are:
- pointers and objects are non-nullable by default. Which eliminates an edge case that might break the logic of your program.
- switch statements must exaust all possible options.
- the
zig
compiler forces you to handle every possible error.
1.10 Other parts of Zig
We already learned a lot about Zig’s syntax, and also, some pretty technical details about it. Just as a quick recap:
- We talked about how functions are written in Zig at Section 1.2.2 and Section 1.2.3.
- How to create new objects/identifiers at Section 1.2.2 and specially at Section 1.4.
- How strings work in Zig at Section 1.8.
- How to use arrays and slices at Section 1.6.
- How to import functionality from other Zig modules at Section 1.2.2.
But, for now, this amount of knowledge is enough for us to continue with this book. Later, over the next chapters we will still talk more about other parts of Zig’s syntax that are also equally important as the other parts. Such as:
- How Object-Oriented programming can be done in Zig through struct declarations at Section 2.3.
- Basic control flow syntax at Section 2.1.
- Enums at Section 7.6;
- Pointers and Optionals at Chapter 6;
- Error handling with
try
andcatch
at Chapter 10; - Unit tests at Chapter 8;
- Vectors;
- Build System at Chapter 9;
https://en.wikipedia.org/wiki/List_of_C-family_programming_languages↩︎
https://www.youtube.com/watch?v=OPuztQfM3Fg&t=2524s&ab_channel=TheVimeagen.↩︎
https://ziglang.org/documentation/master/#Primitive-Types.↩︎
https://www.youtube.com/watch?v=jIZpKpLCOiU&ab_channel=GOTOConferences↩︎
Actually, a lot of existing Rust code is still memory unsafe, because they communicate with external libraries through FFI (foreign function interface), which disables the borrow-checker features through the
unsafe
keyword.↩︎