Skip to content

What is Zig?

Updated Nov 30, 20263 min readBased on official documentation
What is Zig?

In short

Zig is a general-purpose programming language and toolchain for writing robust, optimal, and reusable software. It aims to be easy to read, with explicit code that shows what the program does. The official documentation includes examples that are compiled and tested as part of Zig's test suite.

What Zig is for

Zig is a programming language and a toolchain, which means it comes with the compiler and build tools you need in one package. The official documentation describes it as a language for maintaining software that is robust, optimal, and reusable. In practice, that means the language wants your program to handle edge cases such as running out of memory, to use resources well, and to run in many different environments without rewriting the code.

The language also cares about maintainability. Code should state its intent clearly, so the compiler and other programmers can read it without guessing. Zig tries to keep the amount of extra text needed to understand a piece of code low, so a reader can follow what happens without digging through hidden behavior.

How the code is organized

Every Zig source file is implicitly a struct. A struct is a container that groups data and functions together. You do not write the struct keyword at the top of a file; the file itself acts as that container. Other types in Zig, such as enum, union, and opaque, also create containers of this kind, which the documentation calls namespaces.

Zig has no multiline comments. A line comment starts with two slashes and runs to the end of the line. Doc comments start with three slashes and are used by the compiler to build package documentation. A top-level doc comment starts with two slashes and an exclamation point and describes the namespace that contains it. Identifiers must start with a letter or underscore, and they cannot use a keyword as their name. If you need a name that breaks these rules, for example when linking with a C library, you can wrap it in the @"" syntax.

A simple example

Here is a small program that prints a message to standard error. Standard error is a separate output stream, which is often used for diagnostics. The formatted print helper is the simplest option for most cases.

const std = @import("std");

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

The @import function loads the standard library. The main function has no error return type here because nothing in it can fail. You compile the file with zig build-exe and then run the resulting program. The Zig Standard Library is documented separately and ships with every Zig release, so you can view it offline by running the zig std command, which starts a local web server.

Types and values

Zig has a set of primitive types. Signed integers use the letter i followed by a bit width, such as i32 or i64, and unsigned integers use u. Floating point types include f32 and f64. The bool type holds true or false. The language also has optional values, written with a question mark, which can hold a value or null. Error unions combine an error set with a normal type, so a function can either return a value or an error. Integer types can use other bit widths too, as long as they stay within the documented maximum.

The documentation notes that top-level declarations do not depend on their order in the file. That means you can use a function or constant before the line where it is defined.

Common mistakes

Beginners often place doc comments in the wrong spot. A doc comment must sit directly in front of the declaration it describes. If it appears in the middle of an expression or at the end of a file with nothing after it, the compiler reports an error. Top-level doc comments must come before any other expression in a namespace.

Another common issue is forgetting that Zig does not use multiline comments. Each line that should be ignored needs its own double slash. Also keep in mind that a function returning an error union must be declared that way, and the caller has to handle the error.

Where to go next

The documentation is one long page, so the browser search tool is the fastest way to find a feature. Zig's examples are compiled and tested as part of its main test suite, which means they reflect the current language. If you want to understand the typing rules in more depth, start with the sections on values and primitive types, then move on to the standard library reference.

FAQ

Is Zig a replacement for C?

Zig is often compared with C because it works closely with C libraries and types, such as the c_int type for ABI compatibility. Whether it replaces C in a given project depends on the team's needs and existing code.

Do I need a separate build tool to use Zig?

Zig is described as a toolchain, so the compiler and related tools come together. The documentation shows examples built directly with the zig build-exe command.

Can I read the Zig documentation offline?

Yes. The official HTML document depends on no external files, so it works offline. The standard library documentation is also included with each Zig distribution and can be served locally.

Why does Zig have no multiline comments?

The documentation says that each line of code can be tokenized independently. Line comments with double slashes keep that rule simple, and doc comments cover the documentation case.

Sources

This page is based on the official documentation below. Read it for full detail.

Zig — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.