by Joseph Kashi
Version | 3 |
Date | Sun 21 Jan 2024 |
Author | Joseph Kashi ([email protected]) |
The Zig Programming Language is an online primer for learning real-world Zig programming.
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]
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
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.
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.
You can install Zig by either:
You can choose either approach, both are described below.
$ 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
zig-macos-x86_64-0.12.0-dev.2158+4f2009de1
, this may have changed in the meantime - remember to type the correct name.Latest tagged release:
$ brew install zig
$ port install zig
After installation you need to update the PATH
environment variable for you zig binary.
vim ~/.zprofile
to open the file in the Vim editori
(to enter --INSERT--
mode) and add a blank linke at the end of the file.PATH=$PATH:~/.local/bin/zig
Esc
(to exit --INSERT--
mode):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.
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.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!
PATH
envorinment variable, see the official Zig documentation Started > Installing Zig.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.
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).
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
You can install Zig by either:
git
zls-master
to zls
zls
to a proper destination and build the binary files:$ cd ~/.local/bin
$ mv ~/Downloads/zls/ zls
$ cd zls
$ zig build -Doptimize=ReleaseSafe
$ cd ~/.local/bin
$ git clone https://github.com/zigtools/zls
$ cd zls
$ zig build -Doptimize=ReleaseSafe
TODO
TODO
TODO
TODO
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.
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!
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!
TODO