The Zig Programming Language

by Joseph Kashi

Version 3
Date Sun 21 Jan 2024
Author Joseph Kashi ([email protected])

Summary

The Zig Programming Language is an online primer for learning real-world Zig programming.

Status

This document has not yet been reviewed. It is an unstable document (work in progress) and, as such, may be used as reference material or cited at your own responsibility.

Please submit corrigenda and other comments to [email protected]


About

This website is for anyone with the desire to learn the Zig programming language. Zig is a small programming language designed for solving big problems. Zig offers a small, yet capable, language that can be understood and used by anyone, no matter their experience. Newcomers, hobbyists, and professionals alike can benefit from a fast, modern programming language. The Zig Programming Language provides an introduction to Zig language fundamentals, serving as foundation for your coding projects.

Zig, being a relatively small language, with minimal syntax and few concept hurdles, is a perfect candidate for the next great language for beginners. Currently, many resources for learning Zig presume a working knowledge of the C programming language. This website exists to fill the gap for anyone looking for a direct path to Zig. Mastering Zig will take effort and patience, but I hope with The Zig Programming Language you will discover the joy of coding.

– Joseph



Getting Started

The Zig programming language is currently fast-evolving and it's recommended using the latest binaries of Zig during installation. The information is correct for the latest release of Zig (version 0.12.0+), and you should install this if you’d like to code-along with the provided examples.

Setting Up Your Zig Environment

The first step to learning a new programming language is to set up the tools and environment to run a simple “Hello, World!” application.

Every programming language needs a development environment - this section will help you install Zig and its supporting tools on your computer.

Installing Zig

Step 0: Check for zig Installation

Start by checking if you already have Zig installed on your computer. You can quickly check this from your shell by using the zig version command. The output should look similar to this:

$ zig version
0.12.0-dev.2150+63de8a598

If you already have zig installed on your computer, then you're all done and can carry on. If not, follow the step-by-step guide below on how to install the Zig development environment on your platform.

Step 1: Installation

You can install Zig by either:

  1. Grab a Zig bundle for your platform from the official Zig website and extract it in a directory, or
  2. Using a package manager for your platform that automatically with download and extract the files

You can choose either approach, both are described below.

Some package manger might have bundle outdated versions of Zig. If you want full control of which Zig environment you want to setup, chose the Direct Install approach below.
Option 1: Direct Install
  1. Download the latest Zig bundle for your platform from the official Zig Download page
  2. Unzip the folder in your Download directory
  3. In your terminal move the unpacked bundle to an appropriate location and rename the zig version:
$ cd ~/.local/bin
$ mv ~/Downloads/zig-macos-x86_64-0.12.0-dev.2158+4f2009de1/ .
$ mv zig-macos-x86_64-0.12.0-dev.2158+4f2009de1/ zig
Make sure that you downloaded the Zig bundle to your Download folder or else you need to change the directory path your moving your files from.
In the example above the latest Zig bundle was named zig-macos-x86_64-0.12.0-dev.2158+4f2009de1, this may have changed in the meantime - remember to type the correct name.
Option 2: Package Manager
  1. Use your favorit package manager to download and install on your platform:
Homebrew

Latest tagged release:

$ brew install zig
MacPorts
$ port install zig
For more information on setting up and installing Zig, visit the official Zig Getting startet > Installing Zig.

Step 2: Setting up the PATH

After installation you need to update the PATH environment variable for you zig binary.

  1. Open a terminal and type: vim ~/.zprofile to open the file in the Vim editor
  2. Push i (to enter --INSERT-- mode) and add a blank linke at the end of the file.
  3. Add this line to the end of the file export PATH=$PATH:~/.local/bin/zig
  4. Push Esc (to exit --INSERT-- mode)
  5. Push :wq (to write and quite the editor)

You can verify that the line was added by typing and confirming that the last line is the PATH typed:

$ cat ~/.zprofile
...
export PATH=$PATH:~/.local/bin/zig

After you’re done, you need to restart your shell.

If you used a package manager to install the Zig bundle, you need to change the PATH to where the Zig binary files where installed on your platform. Consult the package manager documentation to know the location or search for the directory on your platform.

Step 3: Verify Installation

Repeat Step 0 (see above). If the shell outputs a version number, then zig is installed and setup correctly, and you're ready to being Zig coding!

For more information about download, installation PATH envorinment variable, see the official Zig documentation Started > Installing Zig.

Conclusion

You have downloaded, installed, set up, and verified your Zig environment, and can now run Zig code by executing the zig command from anywhere in your shell.

Before we jump into writing our first program and exploring the world of Zig, let's setup a couple of more tools to make working with Zig code a bit more fun.

Installing the Zig Tools

When programming, having the right tools, makes a big difference. Language servers are one of these great tools. They connect your editor to advanced coding features, no matter which editor you use.

Language servers are tools that work with any text editor to give you features like highlighting code and suggesting words as you type. They can do more than just simple highlighting extensions, making coding easier and better.

Setting up language servers for Zig is straightforward. In this section, I'll guide you through installing the Zig Language Server (ZLS).

The Zig Language Server (ZLS) is currently fast-evolving and it's recommended using the latest binaries of ZLS during installation.

Step 0: Check for zig Installation

Building the Zig Language Server (ZLS) is very easy, but you will need a build of Zig to build ZLS.

If you are following along from the previous step in setting up your Zig environment, you verified Zig by running zig version in a shell. If this output a version number (e.g. 0.12.0-dev.2150+63de8a598) then you can move on or else revisit the previous step Installing Zig

Step 1: Installation

You can install Zig by either:

  1. Visiting the Zig Language Server (ZLS) page on GitHub and downloading a zip of the master branch, or
  2. Cloning the latest repository with git
Option 1: Manually Download ZIP from GitHub
  1. Browser to the Language Server (zls) page on GitHub.
  2. Click on the blue button named 'Code' (top right corner on a desktop) and select last option 'Download ZIP' to your Download folder
  3. Unzip the folder
  4. Rename the unzipped folder from zls-master to zls
  5. In a terminal move zls to a proper destination and build the binary files:
$ cd ~/.local/bin
$ mv ~/Downloads/zls/ zls
$ cd zls
$ zig build -Doptimize=ReleaseSafe
Option 2: Clone Repository from GitHub
Cloning a repository pre-requires that you have git installed on your platform, consult the Git Documentation > Getting Started - Installing Git.
  1. Open a shell and type:
$ cd ~/.local/bin
$ git clone https://github.com/zigtools/zls
$ cd zls
$ zig build -Doptimize=ReleaseSafe

Conclusion

TODO

The zig Command

TODO

Setup Your IDE

TODO

Staying Up to Date

TODO

A Tour of Zig

Introduction to Zig

What is Zig

Zig is an open-source programming language created by Andrew Kelley in 2016. It's a general-purpose programming language designed to be used for writing software in a wide range of applications.

Zig is a successor of the C programming language. C has been around for decades, and it's still among the top 10 most popular programming languages in the world. Zig offers the same features as C, but includes modern features that make programming easier, more efficient and robust. It helps you as a developer to write clean, safe, and fast software.

As of January 2024, Zig has not yet reached version 1.0 (signifying that it has reached a level of maturity and stability considered suitable for production-level use), with the latest stable version being 0.11.0.

The community of people who have adopted Zig call themselves ziguanas.

The first program

Software that is written in this language are called programs. Here is an example of a simple Zig program that prints I'm starting to learn Zig!:

const std = @import("std");

pub fn main() !void {
  std.debug.print("I'm starting to learn Zig!\n", .{});
}

If you have completed the installation process correctly, you should now be able to call the Zig compiler from your shell. Let's look at the result of our first program.

$ zig run main.zig
I'm starting to learn Zig!

As you can see, the result of our first program was the line I'm starting to learn Zig!

Conclusion

Zig has a growing community of developers all over the world. This programming language is easy to learn and pleasant to write programs in. However, you will still encounter difficulties, but when you do, you can always ask other programmers for help or find a ready-made answer!

Foundations

TODO