Introduction

Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

RobustBehavior is correct even for edge cases such as out of memory.OptimalWrite programs the best way they can behave and perform.ReusableThe same code works in many environments which have different constraints.MaintainablePrecisely communicate intent to the compiler and other programmers. The language imposes a low overhead to reading code and is resilient to changing requirements and environments.

Often the most efficient way to learn something new is to see examples, so this documentation shows how to use each of Zig's features. It is all on one page so you can search with your browser's search tool.

The code samples in this document are compiled and tested as part of the main test suite of Zig.

This HTML document depends on no external files, so you can use it offline.

Zig Standard Library

The Zig Standard Library has its own documentation.

Zig's Standard Library contains commonly used algorithms, data structures, and definitions to help you build programs or libraries. You will see many examples of Zig's Standard Library used in this documentation. To learn more about the Zig Standard Library, visit the link above.

Alternatively, the Zig Standard Library documentation is provided with each Zig distribution. It can be rendered via a local webserver with:

$ zig std

Hello World

hello.zig
const std = @import("std");

pub fn main(init: std.process.Init) !void {
    try std.Io.File.stdout().writeStreamingAll(init.io, "Hello, World!\n");
}
$ zig build-exe hello.zig
$ ./hello
Hello, World!

Most of the time, it is more appropriate to write to stderr rather than stdout, and whether or not the message is successfully written to the stream is irrelevant. Also, formatted printing often comes in handy. For this common case, there is a simpler API:

hello_again.zig
const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, {s}!\n", .{"World"});
}
$ zig build-exe hello_again.zig
$ ./hello_again
Hello, World!

In this case, the ! may be omitted from the return type of main because no errors are returned from the function.

See also:

Comments

There are three types of comments. Normal comments are ignored, while Doc Comments and Top-Level Doc Comments are used by the compiler to generate the package documentation.

comments.zig
const print = @import("std").debug.print;

pub fn main() void {
    // Comments in Zig start with "//" and end at the next LF byte (end of line).
    // The line below is a comment and won't be executed.

    //print("Hello?", .{});

    print("Hello, world!\n", .{}); // another comment
}
$ zig build-exe comments.zig
$ ./comments
Hello, world!

There are no multiline comments. Zig has the property that each line of code can be tokenized independently.

Doc Comments

A doc comment is one that begins with exactly three slashes (i.e. /// but not ////); multiple doc comments in a row are merged together to form a multiline doc comment. The doc comment documents whatever immediately follows it.

doc_comments.zig
/// A structure for storing a timestamp, with nanosecond precision (this is a
/// multiline doc comment).
const Timestamp = struct {
    /// The number of seconds since the epoch (this is also a doc comment).
    seconds: i64, // signed so we can represent pre-1970 (not a doc comment)
    /// The number of nanoseconds past the second (doc comment again).
    nanos: u32,

    /// Returns a `Timestamp` struct representing the Unix epoch; that is, the
    /// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).
    pub fn unixEpoch() Timestamp {
        return Timestamp{
            .seconds = 0,
            .nanos = 0,
        };
    }
};

Doc comments are only allowed in certain places; it is a compile error to have a doc comment in an unexpected place, such as in the middle of an expression, or just before a non-doc comment.

invalid_doc-comment.zig
/// doc-comment
//! top-level doc-comment
const std = @import("std");
$ zig build-obj invalid_doc-comment.zig
/home/andy/src/zig/doc/langref/invalid_doc-comment.zig:1:16: error: expected type expression, found 'a document comment'
/// doc-comment
               ^
unattached_doc-comment.zig
pub fn main() void {}

/// End of file
$ zig build-obj unattached_doc-comment.zig
/home/andy/src/zig/doc/langref/unattached_doc-comment.zig:3:1: error: unattached documentation comment
/// End of file
^~~~~~~~~~~~~~~

Doc comments can be interleaved with normal comments, which are ignored.

Top-Level Doc Comments

A top-level doc comment is one that begins with two slashes and an exclamation point: //!; it documents the type which owns the containing Namespace.

It is a compile error if a top-level doc comment is not placed at the start of a namespace, before any expressions.

tldoc_comments.zig
//! Provides functions for retrieving the current date and time with varying
//! degrees of precision and accuracy.

const S = struct {
    //! Top level comments are allowed inside namespaces other than the
    //! implicit struct created by files, but it is not very useful. Currently,
    //! when producing the package documentation, these comments are ignored.
};

Namespace

A namespace in Zig is created by struct, enum, union, and opaque.

They contain Namespace Level Variables, function declarations, and comptime blocks.

Although namespaces use curly braces to surround their definition, they should not be confused with blocks or function bodies.

Every Zig source file is implicitly a struct, with the keyword struct and curly braces omitted.

Identifiers

Identifiers must start with an alphabetic character or underscore and may be followed by any number of alphanumeric characters or underscores. They must not overlap with any keywords. See Keyword Reference.

String Identifier Syntax

If a name that does not fit these requirements is needed, such as for linking with external libraries, the @"" syntax may be used.

identifiers.zig
const @"identifier with spaces in it" = 0xff;
const @"1SmallStep4Man" = 112358;

const c = @import("std").c;
pub extern "c" fn @"error"() void;
pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int;

const Color = enum {
    red,
    @"really red",
};
const color: Color = .@"really red";

Values

values.zig
// Top-level declarations are order-independent:
const print = std.debug.print;
const std = @import("std");
const os = std.os;
const assert = std.debug.assert;

// Custom error set definition:
const ExampleErrorSet = error{
    ExampleErrorVariant,
};

pub fn main() void {
    // integers
    const one_plus_one: i32 = 1 + 1;
    print("1 + 1 = {}\n", .{one_plus_one});

    // floats
    const seven_div_three: f32 = 7.0 / 3.0;
    print("7.0 / 3.0 = {}\n", .{seven_div_three});

    // boolean
    print("{}\n{}\n{}\n", .{
        true and false,
        true or false,
        !true,
    });

    // optional
    var optional_value: ?[]const u8 = null;
    assert(optional_value == null);

    print("\noptional 1\ntype: {}\nvalue: {?s}\n", .{
        @TypeOf(optional_value), optional_value,
    });

    optional_value = "hi";
    assert(optional_value != null);

    print("\noptional 2\ntype: {}\nvalue: {?s}\n", .{
        @TypeOf(optional_value), optional_value,
    });

    // error union
    var number_or_error: ExampleErrorSet!i32 = ExampleErrorSet.ExampleErrorVariant;

    print("\nerror union 1\ntype: {}\nvalue: {!}\n", .{
        @TypeOf(number_or_error),
        number_or_error,
    });

    number_or_error = 1234;

    print("\nerror union 2\ntype: {}\nvalue: {!}\n", .{
        @TypeOf(number_or_error), number_or_error,
    });
}
$ zig build-exe values.zig
$ ./values
1 + 1 = 2
7.0 / 3.0 = 2.3333333
false
true
false

optional 1
type: ?[]const u8
value: null

optional 2
type: ?[]const u8
value: hi

error union 1
type: error{ExampleErrorVariant}!i32
value: error.ExampleErrorVariant

error union 2
type: error{ExampleErrorVariant}!i32
value: 1234

Primitive Types

Type C Equivalent Description
i8 int8_t signed 8-bit integer
u8 uint8_t unsigned 8-bit integer
i16 int16_t signed 16-bit integer
u16 uint16_t unsigned 16-bit integer
i32 int32_t signed 32-bit integer
u32 uint32_t unsigned 32-bit integer
i64 int64_t signed 64-bit integer
u64 uint64_t unsigned 64-bit integer
i128 __int128 signed 128-bit integer
u128 unsigned __int128 unsigned 128-bit integer
isize intptr_t, ssize_t signed pointer sized integer
usize uintptr_t, size_t unsigned pointer sized integer. Also see #5185
c_char char for ABI compatibility with C
c_short short for ABI compatibility with C
c_ushort unsigned short for ABI compatibility with C
c_int int for ABI compatibility with C
c_uint unsigned int for ABI compatibility with C
c_long long for ABI compatibility with C
c_ulong unsigned long for ABI compatibility with C
c_longlong long long for ABI compatibility with C
c_ulonglong unsigned long long for ABI compatibility with C
c_longdouble long double for ABI compatibility with C
f16 _Float16 16-bit floating point (10-bit mantissa) IEEE-754-2008 binary16
f32 float 32-bit floating point (23-bit mantissa) IEEE-754-2008 binary32
f64 double 64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64
f80 long double 80-bit floating point (64-bit mantissa) IEEE-754-2008 80-bit extended precision
f128 _Float128 128-bit floating point (112-bit mantissa) IEEE-754-2008 binary128
bool bool true or false
anyopaque void Used for type-erased pointers.
noreturn (none) the type of break, continue, return, unreachable, and while (true) {}
type (none) the type of types
anyerror (none) an error code
comptime_int (none) Only allowed for comptime-known values. The type of integer literals.
comptime_float (none) Only allowed for comptime-known values. The type of float literals.

Primitive Types

In addition to the integer types above, arbitrary bit-width integers can be referenced by using an identifier of i or u followed by digits. For example, the identifier i7 refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is 65535.

See also:

Primitive Values

Name Description
true and false bool values
null used to set an optional type to null
undefined used to leave a value unspecified

Primitive Values

See also:

String Literals and Unicode Code Point Literals

String literals are constant single-item Pointers to null-terminated byte arrays. The type of string literals encodes both the length, and the fact that they are null-terminated, and thus they can be coerced to both Slices and Null-Terminated Pointers. Dereferencing string literals converts them to Arrays.

Because Zig source code is UTF-8 encoded, any non-ASCII bytes appearing within a string literal in source code carry their UTF-8 meaning into the content of the string in the Zig program; the bytes are not modified by the compiler. It is possible to embed non-UTF-8 bytes into a string literal using \xNN notation.

Indexing into a string containing non-ASCII bytes returns individual bytes, whether valid UTF-8 or not.

Unicode code point literals have type comptime_int, the same as Integer Literals. All Escape Sequences are valid in both string literals and Unicode code point literals.

string_literals.zig
const print = @import("std").debug.print;
const mem = @import("std").mem; // will be used to compare bytes

pub fn main() void {
    const bytes = "hello";
    print("{}\n", .{@TypeOf(bytes)}); // *const [5:0]u8
    print("{d}\n", .{bytes.len}); // 5
    print("{c}\n", .{bytes[1]}); // 'e'
    print("{d}\n", .{bytes[5]}); // 0
    print("{}\n", .{'e' == '\x65'}); // true
    print("{d}\n", .{'\u{1f4a9}'}); // 128169
    print("{d}\n", .{'💯'}); // 128175
    print("{u}\n", .{'⚡'});
    print("{}\n", .{mem.eql(u8, "hello", "h\x65llo")}); // true
    print("{}\n", .{mem.eql(u8, "💯", "\xf0\x9f\x92\xaf")}); // also true
    const invalid_utf8 = "\xff\xfe"; // non-UTF-8 strings are possible with \xNN notation.
    print("0x{x}\n", .{invalid_utf8[1]}); // indexing them returns individual bytes...
    print("0x{x}\n", .{"💯"[1]}); // ...as does indexing part-way through non-ASCII characters
}
$ zig build-exe string_literals.zig
$ ./string_literals
*const [5:0]u8
5
e
0
true
128169
128175
⚡
true
true
0xfe
0x9f

See also:

Escape Sequences

Escape Sequence Name
\n Newline
\r Carriage Return
\t Tab
\\ Backslash
\' Single Quote
\" Double Quote
\xNN hexadecimal 8-bit byte value (2 digits)
\u{NNNNNN} hexadecimal Unicode scalar value UTF-8 encoded (1 or more digits)

Escape Sequences

Note that the maximum valid Unicode scalar value is 0x10ffff.

Multiline String Literals

Multiline string literals have no escapes and can span across multiple lines. To start a multiline string literal, use the \\ token. Just like a comment, the string literal goes until the end of the line. The end of the line is not included in the string literal. However, if the next line begins with \\ then a newline is appended and the string literal continues.

multiline_string_literals.zig
const hello_world_in_c =
    \\#include <stdio.h>
    \\
    \\int main(int argc, char **argv) {
    \\    printf("hello world\n");
    \\    return 0;
    \\}
;

See also:

Assignment

Use the const keyword to assign a value to an identifier:

constant_identifier_cannot_change.zig
const x = 1234;

fn foo() void {
    // It works at file scope as well as inside functions.
    const y = 5678;

    // Once assigned, an identifier cannot be changed.
    y += 1;
}

pub fn main() void {
    foo();
}
$ zig build-exe constant_identifier_cannot_change.zig
/home/andy/src/zig/doc/langref/constant_identifier_cannot_change.zig:8:5: error: cannot assign to constant
    y += 1;
    ^
referenced by:
    main: /home/andy/src/zig/doc/langref/constant_identifier_cannot_change.zig:12:8
    callMain [inlined]: /home/andy/src/zig/lib/std/start.zig:788:64
    callMainWithArgs [inlined]: /home/andy/src/zig/lib/std/start.zig:729:20
    posixCallMainAndExit: /home/andy/src/zig/lib/std/start.zig:681:38
    2 reference(s) hidden; use '-freference-trace=6' to see all references

const applies to all of the bytes that the identifier immediately addresses. Pointers have their own const-ness.

If you need a variable that you can modify, use the var keyword:

mutable_var.zig
const print = @import("std").debug.print;

pub fn main() void {
    var y: i32 = 5678;

    y += 1;

    print("{d}", .{y});
}
$ zig build-exe mutable_var.zig
$ ./mutable_var
5679

Variables must be initialized:

var_must_be_initialized.zig
pub fn main() void {
    var x: i32;

    x = 1;
}
$ zig build-exe var_must_be_initialized.zig
/home/andy/src/zig/doc/langref/var_must_be_initialized.zig:2:15: error: expected '=', found ';'
    var x: i32;
              ^

undefined

Use undefined to leave variables uninitialized:

assign_undefined.zig
const print = @import("std").debug.print;

pub fn main() void {
    var x: i32 = undefined;
    x = 1;
    print("{d}", .{x});
}
$ zig build-exe assign_undefined.zig
$ ./assign_undefined
1

undefined can be coerced to any type. Once this happens, it is no longer possible to detect that the value is undefined. undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, undefined means "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used."

In debug and safe mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger. However, this behavior is only an implementation feature, not a language semantic, so it is not guaranteed to be observable to code.

Destructuring

A destructuring assignment can separate elements of indexable aggregate types ( Tuples, Arrays, Vectors):

destructuring_to_existing.zig
const print = @import("std").debug.print;

pub fn main() void {
    var x: u32 = undefined;
    var y: u32 = undefined;
    var z: u32 = undefined;

    const tuple = .{ 1, 2, 3 };

    x, y, z = tuple;

    print("tuple: x = {}, y = {}, z = {}\n", .{x, y, z});

    const array = [_]u32{ 4, 5, 6 };

    x, y, z = array;

    print("array: x = {}, y = {}, z = {}\n", .{x, y, z});

    const vector: @Vector(3, u32) = .{ 7, 8, 9 };

    x, y, z = vector;

    print("vector: x = {}, y = {}, z = {}\n", .{x, y, z});
}
$ zig build-exe destructuring_to_existing.zig
$ ./destructuring_to_existing
tuple: x = 1, y = 2, z = 3
array: x = 4, y = 5, z = 6
vector: x = 7, y = 8, z = 9

A destructuring expression may only appear within a block (i.e. not at Namespace scope). The left hand side of the assignment must consist of a comma separated list, each element of which may be either an lvalue (for instance, an existing var) or a variable declaration:

destructuring_mixed.zig
const print = @import("std").debug.print;

pub fn main() void {
    var x: u32 = undefined;

    const tuple = .{ 1, 2, 3 };

    x, var y : u32, const z = tuple;

    print("x = {}, y = {}, z = {}\n", .{x, y, z});

    // y is mutable
    y = 100;

    // You can use _ to throw away unwanted values.
    _, x, _ = tuple;

    print("x = {}\n", .{x});
}
$ zig build-exe destructuring_mixed.zig
$ ./destructuring_mixed
x = 1, y = 2, z = 3
x = 2

A destructure may be prefixed with the comptime keyword, in which case the entire destructure expression is evaluated at comptime. All var s declared would be comptime var s and all expressions (both result locations and the assignee expression) are evaluated at comptime.

See also:

Zig Test

Code written within one or more test declarations can be used to ensure behavior meets expectations:

testing_introduction.zig
const std = @import("std");

test "expect addOne adds one to 41" {

    // The Standard Library contains useful functions to help create tests.
    // `expect` is a function that verifies its argument is true.
    // It will return an error if its argument is false to indicate a failure.
    // `try` is used to return an error to the test runner to notify it that the test failed.
    try std.testing.expect(addOne(41) == 42);

    // However, in most cases it is more convenient to use a more specific function like `expectEqual`.
    // This gives you much clearer and more helpful error messages when a test fails.
    try std.testing.expectEqual(42, addOne(41));
}

test addOne {
    // A test name can also be written using an identifier.
    // This is a doctest, and serves as documentation for `addOne`.
    try std.testing.expectEqual(42, addOne(41));
}

/// The function `addOne` adds one to the number given as its argument.
fn addOne(number: i32) i32 {
    return number + 1;
}
$ zig test testing_introduction.zig
1/2 testing_introduction.test.expect addOne adds one to 41...OK
2/2 testing_introduction.decltest.addOne...OK
All 2 tests passed.

The testing_introduction.zig code sample tests the function addOne to ensure that it returns 42 given the input 41. From this test's perspective, the addOne function is said to be code under test.

zig test is a tool that creates and runs a test build. By default, it builds and runs an executable program using the default test runner provided by the Zig Standard Library as its main entry point. During the build, test declarations found while resolving the given Zig source file are included for the default test runner to run and report on.

This documentation discusses the features of the default test runner as provided by the Zig Standard Library. Its source code is located in lib/compiler/test_runner.zig.

The shell output shown above displays two lines after the zig test command. These lines are printed to standard error by the default test runner:

1/2 testing_introduction.test.expect addOne adds one to 41...Lines like this indicate which test, out of the total number of tests, is being run. In this case, 1/2 indicates that the first test, out of a total of two tests, is being run. Note that, when the test runner program's standard error is output to the terminal, these lines are cleared when a test succeeds. 2/2 testing_introduction.decltest.addOne...When the test name is an identifier, the default test runner uses the text decltest instead of test. All 2 tests passed.This line indicates the total number of tests that have passed.

Test Declarations

Test declarations contain the keyword test, followed by an optional name written as a string literal or an identifier, followed by a block containing any valid Zig code that is allowed in a function.

Non-named test blocks always run during test builds and are exempt from Skip Tests.

Test declarations are similar to Functions: they have a return type and a block of code. The implicit return type of test is the Error Union Type anyerror!void, and it cannot be changed. When a Zig source file is not built using the zig test tool, the test declarations are omitted from the build.

Test declarations can be written in the same file, where code under test is written, or in a separate Zig source file. Since test declarations are top-level declarations, they are order-independent and can be written before or after the code under test.

See also:

Doctests

Test declarations named using an identifier are doctests. The identifier must refer to another declaration in scope. A doctest, like a doc comment, serves as documentation for the associated declaration, and will appear in the generated documentation for the declaration.

An effective doctest should be self-contained and focused on the declaration being tested, answering questions a new user might have about its interface or intended usage, while avoiding unnecessary or confusing details. A doctest is not a substitute for a doc comment, but rather a supplement and companion providing a testable, code-driven example, verified by zig test.

Test Failure

The default test runner checks for an error returned from a test. When a test returns an error, the test is considered a failure and its error return trace is output to standard error. The total number of failures will be reported after all tests have run.

testing_failure.zig
const std = @import("std");

test "expect this to fail" {
    try std.testing.expect(false);
}

test "expect this to succeed" {
    try std.testing.expect(true);
}
$ zig test testing_failure.zig
1/2 testing_failure.test.expect this to fail...FAIL (TestUnexpectedResult)
/home/andy/src/zig/lib/std/testing.zig:584:14: 0x124c589 in expect (std.zig)
    if (!ok) return error.TestUnexpectedResult;
             ^
/home/andy/src/zig/doc/langref/testing_failure.zig:4:5: 0x124c679 in test.expect this to fail (testing_failure.zig)
    try std.testing.expect(false);
    ^
2/2 testing_failure.test.expect this to succeed...OK
1 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/andy/src/zig/.zig-cache/o/91caaf9cb31d640883bbcdb04a2ab9c1/test --seed=0x7e1e9e30

Skip Tests

One way to skip tests is to filter them out by using the zig test command line parameter --test-filter [text]. This makes the test build only include tests whose name contains the supplied filter text. Note that non-named tests are run even when using the --test-filter [text] command line parameter.

To programmatically skip a test, make a test return the error error.SkipZigTest and the default test runner will consider the test as being skipped. The total number of skipped tests will be reported after all tests have run.

testing_skip.zig
test "this will be skipped" {
    return error.SkipZigTest;
}
$ zig test testing_skip.zig
1/1 testing_skip.test.this will be skipped...SKIP
0 passed; 1 skipped; 0 failed.

Report Memory Leaks

When code allocates Memory using the Zig Standard Library's testing allocator, std.testing.allocator, the default test runner will report any leaks that are found from using the testing allocator:

testing_detect_leak.zig
const std = @import("std");

test "detect leak" {
    const gpa = std.testing.allocator;
    var list: std.ArrayList(u21) = .empty;
    // missing `defer list.deinit(gpa);`
    try list.append(gpa, '☔');

    try std.testing.expectEqual(1, list.items.len);
}
$ zig test testing_detect_leak.zig
1/1 testing_detect_leak.test.detect leak...OK
[SafeAllocator] (err): leaked [addr: 79b90cfde010, len: 132 (0x84) align: 4] allocated at:
/home/andy/src/zig/lib/std/array_list.zig:1375:56: 0x124d711 in ensureTotalCapacityPrecise (std.zig)
                const new_memory = try gpa.alignedAlloc(T, alignment, new_capacity);
                                                       ^
/home/andy/src/zig/lib/std/array_list.zig:1349:51: 0x124d359 in ensureTotalCapacity (std.zig)
            return self.ensureTotalCapacityPrecise(gpa, growCapacity(new_capacity));
                                                  ^
/home/andy/src/zig/lib/std/array_list.zig:1406:41: 0x124c93d in addOne (std.zig)
            try self.ensureTotalCapacity(gpa, newlen);
                                        ^
/home/andy/src/zig/lib/std/array_list.zig:1027:49: 0x124c7d7 in append (std.zig)
            const new_item_ptr = try self.addOne(gpa);
                                                ^
/home/andy/src/zig/doc/langref/testing_detect_leak.zig:7:20: 0x124c638 in test.detect leak (testing_detect_leak.zig)
    try list.append(gpa, '☔');
                   ^
/home/andy/src/zig/lib/compiler/test_runner.zig:295:25: 0x1200951 in mainTerminal (test_runner.zig)
        if (test_fn.func()) |_| {
                        ^
/home/andy/src/zig/lib/compiler/test_runner.zig:74:28: 0x1200132 in main (test_runner.zig)
        return mainTerminal(init);
                           ^
(additional stack frames may have been skipped...)

All 1 tests passed.
1 errors were logged.
1 tests leaked memory.
error: the following test command failed with exit code 1:
/home/andy/src/zig/.zig-cache/o/22325635dab0f04291ec71ddf234835d/test --seed=0x9c9e612a

See also:

Detecting Test Build

Use the compile variable @import("builtin").is_test to detect a test build:

testing_detect_test.zig
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;

test "builtin.is_test" {
    try expect(isATest());
}

fn isATest() bool {
    return builtin.is_test;
}
$ zig test testing_detect_test.zig
1/1 testing_detect_test.test.builtin.is_test...OK
All 1 tests passed.

Test Output and Logging

The default test runner and the Zig Standard Library's testing namespace output messages to standard error.

The Testing Namespace

The Zig Standard Library's testing namespace contains useful functions to help you create tests. In addition to the expect function, this document uses a couple of more functions as exemplified here:

testing_namespace.zig
const std = @import("std");

test "expectEqual demo" {
    const expected: i32 = 42;
    const actual = 42;

    // The first argument to `expectEqual` is the known, expected, result.
    // The second argument is the result of some expression.
    // The actual's type is casted to the type of expected.
    try std.testing.expectEqual(expected, actual);
}

test "expectError demo" {
    const expected_error = error.DemoError;
    const actual_error_union: anyerror!void = error.DemoError;

    // `expectError` will fail when the actual error is different than
    // the expected error.
    try std.testing.expectError(expected_error, actual_error_union);
}
$ zig test testing_namespace.zig
1/2 testing_namespace.test.expectEqual demo...OK
2/2 testing_namespace.test.expectError demo...OK
All 2 tests passed.

The Zig Standard Library also contains functions to compare Slices, strings, and more. See the rest of the std.testing namespace in the Zig Standard Library for more available functions.

Test Tool Documentation

zig test has a few command line parameters which affect the compilation. See zig test --help for a full list.

Variables

A variable is a unit of Memory storage.

It is generally preferable to use const rather than var when declaring a variable. This causes less work for both humans and computers to do when reading code, and creates more optimization opportunities.

Variables are never allowed to shadow Identifiers from an outer scope.

The extern keyword or @extern builtin function can be used to link against a variable that is exported from another object. The export keyword or @export builtin function can be used to make a variable available to other objects at link time. In both cases, the type of the variable must be C ABI compatible.

See also:

Namespace Level Variables

Namespace level variables have global lifetime and are order-independent and lazily analyzed. The initialization value of namespace level variables is implicitly comptime. If a namespace level variable is const then its value is comptime-known, otherwise it is runtime-known.

test_namespace_level_variables.zig
var y: i32 = add(10, x);
const x: i32 = add(12, 34);

test "container level variables" {
    try expectEqual(46, x);
    try expectEqual(56, y);
}

fn add(a: i32, b: i32) i32 {
    return a + b;
}

const std = @import("std");
const expectEqual = std.testing.expectEqual;
$ zig test test_namespace_level_variables.zig
1/1 test_namespace_level_variables.test.container level variables...OK
All 1 tests passed.

Namespace level variables may be declared inside a struct, union, enum, or opaque:

test_namespaced_variable.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "namespaced container level variable" {
    try expectEqual(1235, foo());
    try expectEqual(1236, foo());
}

const S = struct {
    var x: i32 = 1234;
};

fn foo() i32 {
    S.x += 1;
    return S.x;
}
$ zig test test_namespaced_variable.zig
1/1 test_namespaced_variable.test.namespaced container level variable...OK
All 1 tests passed.

Locally-Scoped Global Variables

It is also possible to have local variables with global lifetime by using namespaces inside functions.

test_locally_scoped_global_variable.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "static local variable" {
    try expectEqual(1235, foo());
    try expectEqual(1236, foo());
}

fn foo() i32 {
    const S = struct {
        var x: i32 = 1234;
    };
    S.x += 1;
    return S.x;
}
$ zig test test_locally_scoped_global_variable.zig
1/1 test_locally_scoped_global_variable.test.static local variable...OK
All 1 tests passed.

Thread Local Variables

A variable may be specified to be a thread-local variable using the threadlocal keyword, which makes each thread work with a separate instance of the variable:

test_thread_local_variables.zig
const std = @import("std");
const assert = std.debug.assert;

threadlocal var x: i32 = 1234;

test "thread local storage" {
    const thread1 = try std.Thread.spawn(.{}, testTls, .{});
    const thread2 = try std.Thread.spawn(.{}, testTls, .{});
    testTls();
    thread1.join();
    thread2.join();
}

fn testTls() void {
    assert(x == 1234);
    x += 1;
    assert(x == 1235);
}
$ zig test test_thread_local_variables.zig
1/1 test_thread_local_variables.test.thread local storage...OK
All 1 tests passed.

For Single Threaded Builds, all thread local variables are treated as regular Namespace Level Variables.

Thread local variables may not be const.

Local Variables

Local variables occur inside Functions, comptime blocks, and labeled Blocks.

When a local variable is const, it means that after initialization, the variable's value will not change. If the initialization value of a const variable is comptime-known, then the variable is also comptime-known.

A local variable may be qualified with the comptime keyword. This causes the variable's value to be comptime-known, and all loads and stores of the variable to happen during semantic analysis of the program, rather than at runtime. All variables declared in a comptime expression are implicitly comptime variables.

test_comptime_variables.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "comptime vars" {
    var x: i32 = 1;
    comptime var y: i32 = 1;

    x += 1;
    y += 1;

    try expectEqual(2, x);
    try expectEqual(2, y);

    if (y != 2) {
        // This compile error never triggers because y is a comptime variable,
        // and so `y != 2` is a comptime value, and this if is statically evaluated.
        @compileError("wrong y value");
    }
}
$ zig test test_comptime_variables.zig
1/1 test_comptime_variables.test.comptime vars...OK
All 1 tests passed.

Integers

Integer Literals

integer_literals.zig
const decimal_int = 98222;
const hex_int = 0xff;
const another_hex_int = 0xFF;
const octal_int = 0o755;
const binary_int = 0b11110000;

// underscores may be placed between two digits as a visual separator
const one_billion = 1_000_000_000;
const binary_mask = 0b1_1111_1111;
const permissions = 0o7_5_5;
const big_address = 0xFF80_0000_0000_0000;

Runtime Integer Values

Integer literals have no size limitation, and if any Illegal Behavior occurs, the compiler catches it.

However, once an integer value is no longer known at compile-time, it must have a known size, and is vulnerable to safety-checked Illegal Behavior.

runtime_vs_comptime.zig
fn divide(a: i32, b: i32) i32 {
    return a / b;
}

In this function, values a and b are known only at runtime, and thus this division operation is vulnerable to both Integer Overflow and Division by Zero.

Operators such as + and - cause Illegal Behavior on integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets. +% and -% perform wrapping arithmetic while +| and -| perform saturating arithmetic.

Zig supports arbitrary bit-width integers, referenced by using an identifier of i or u followed by digits. For example, the identifier i7 refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is 65535. For signed integer types, Zig uses a two's complement representation.

See also:

Floats

Zig has the following floating point types:

  • f16 - IEEE-754-2008 binary16
  • f32 - IEEE-754-2008 binary32
  • f64 - IEEE-754-2008 binary64
  • f80 - IEEE-754-2008 80-bit extended precision
  • f128 - IEEE-754-2008 binary128
  • c_longdouble - matches long double for the target C ABI

Float Literals

Float literals have type comptime_float which is guaranteed to have the same precision and operations of the largest other floating point type, which is f128.

Float literals coerce to any floating point type, and to any integer type when there is no fractional component.

float_literals.zig
const floating_point = 123.0E+77;
const another_float = 123.0;
const yet_another = 123.0e+77;

const hex_floating_point = 0x103.70p-5;
const another_hex_float = 0x103.70;
const yet_another_hex_float = 0x103.70P-5;

// underscores may be placed between two digits as a visual separator
const lightspeed = 299_792_458.000_000;
const nanosecond = 0.000_000_001;
const more_hex = 0x1234_5678.9ABC_CDEFp-10;

There is no syntax for NaN, infinity, or negative infinity. For these special values, one must use the standard library:

float_special_values.zig
const std = @import("std");

const inf = std.math.inf(f32);
const negative_inf = -std.math.inf(f64);
const nan = std.math.nan(f128);

Floating Point Operations

By default floating point operations use .strict mode, but you can switch to .optimized mode on a per-block basis:

float_mode_obj.zig
const std = @import("std");
const big = @as(f64, 1 << 40);

export fn foo_strict(x: f64) f64 {
    return x + big - big;
}

export fn foo_optimized(x: f64) f64 {
    @setFloatMode(.optimized);
    return x + big - big;
}
$ zig build-obj float_mode_obj.zig -O fast

For this test we have to separate code into two object files - otherwise the optimizer figures out all the values at compile-time, which operates in strict mode.

float_mode_exe.zig
const print = @import("std").debug.print;

extern fn foo_strict(x: f64) f64;
extern fn foo_optimized(x: f64) f64;

pub fn main() void {
    const x = 0.001;
    print("optimized = {}\n", .{foo_optimized(x)});
    print("strict = {}\n", .{foo_strict(x)});
}
$ zig build-exe float_mode_exe.zig float_mode_obj.o -O fast
$ ./float_mode_exe
optimized = 0.001
strict = 0.0009765625

See also:

Operators

There is no operator overloading. When you see an operator in Zig, you know that it is doing something from this table, and nothing else.

Table of Operators

Name Syntax Types Remarks Example
Addition
a + b
a += b
- Integers
- Floats
- Can cause overflow for integers.
- Invokes Peer Type Resolution for the operands.
- See also @addWithOverflow.

2 + 5 == 7
Wrapping Addition
a +% b
a +%= b
- Integers - Twos-complement wrapping behavior.
- Invokes Peer Type Resolution for the operands.
- See also @addWithOverflow.

@as(u32, 0xffffffff) +% 1 == 0
Saturating Addition
a +| b
a +|= b
- Integers - Invokes Peer Type Resolution for the operands.
@as(u8, 255) +| 1 == @as(u8, 255)
Subtraction
a - b
a -= b
- Integers
- Floats
- Can cause overflow for integers.
- Invokes Peer Type Resolution for the operands.
- See also @subWithOverflow.

2 - 5 == -3
Wrapping Subtraction
a -% b
a -%= b
- Integers - Twos-complement wrapping behavior.
- Invokes Peer Type Resolution for the operands.
- See also @subWithOverflow.

@as(u8, 0) -% 1 == 255
Saturating