Skip to content

Zig Tutorial

This is a practical guide to Zig for people who are new to it. The lessons are short, every example is real code, and I’ll be honest about the parts that usually give beginners trouble.

Here’s a full Zig program. Save it as hello.zig and run it with zig run hello.zig:

const std = @import("std");
pub fn main() void {
std.debug.print("Hello, World!\n", .{});
}

Output:

Hello, World!

A few things to notice. @import("std") pulls in the standard library. pub fn main() is where the program starts. And the .{} at the end is an empty list of values to put into the message. You’ll use it a lot once you start printing variables.

Zig is a small systems language, aimed at the same jobs as C: tools, games, embedded devices and anything where you want full control over memory. It has no hidden control flow and no hidden memory allocations. If a function needs memory, you pass it an allocator, so you can always see where memory comes from.

It can also compile C code. Plenty of people use zig cc as a C compiler before they ever write a line of Zig.

One honest heads-up: Zig hasn’t reached 1.0 yet, so the language and standard library still change between releases. If an example from an old blog post won’t compile, that’s usually why. Check which Zig version a tutorial was written for.

Lessons will appear on the left as they’re written. Work through them roughly in order if you’re starting from zero.

See if that landed

1. How do you run a single Zig file?

2. Where does a Zig function get memory from when it needs some?

3. Why might code from an old Zig blog post not compile today?

Score: 0 / 3