Movatterモバイル変換


[0]ホーム

URL:


0.1.1 |0.2.0 |0.3.0 |0.4.0 |0.5.0 |0.6.0 |0.7.1 |0.8.1 |0.9.1 |0.10.1 |0.11.0 |0.12.1 |0.13.0 |0.14.1 |0.15.2 |master

Contents

Introduction§

Zig is a general-purpose programming language and toolchain for maintainingrobust,optimal, andreusable software.

  • Robust - behavior is correct even for edge cases such as out of memory.
  • Optimal - write programs the best way they can behave and perform.
  • Reusable - the same code works in many environments which have different constraints.
  • Maintainable - precisely 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.

Where is the documentation for the Zig standard library?

Hello World§

hello.zig

const std =@import("std");pubfnmain() !void {const stdout = std.io.getStdOut().writer();try stdout.print("Hello, {s}!\n", .{"world"});}
$ zig build-exe hello.zig$ ./helloHello, world!

The Zig code sample above demonstrates one way to create a program that will outputHello, world!.

The code sample shows the contents of a file namedhello.zig. Files storing Zig source code areUTF-8 encoded text files. The files storing Zig source code are usually named with the.zig extension.

Following thehello.zig Zig code sample, theZig Build System is used to build an executable program from thehello.zig source code. Then, thehello program is executed showing its outputHello, world!. The lines beginning with$ represent command line prompts and a command. Everything else is program output.

The code sample begins by adding Zig's Standard Library to the build using the@import builtin function. The@import("std") function call creates a structure to represent the Standard Library. The code then declares aconstant identifier, namedstd, for easy access toZig's standard library.

Next, apublic function,pubfn, namedmain is declared. Themain function is necessary because it tells the Zig compiler where the start of the program exists. Programs designed to be executed will need apubfnmain function. For more advanced use cases, Zig offers other features to inform the compiler where the start of the program exists. Libraries, on the other hand, do not need amain function because library code is usually called by other programs.

A function is a block of any number of statements and expressions that, as a whole, perform a task. Functions may or may not return data after they are done performing their task. If a function cannot perform its task, it might return an error. Zig makes all of this explicit.

In thehello.zig code sample, themain function is declared with the!void return type. This return type is known as anError Union Type. This syntax tells the Zig compiler that the function will either return an error or a value. An error union type combines anError Set Type and aPrimitive Type. The full form of an error union type is<error set type>!<primitive type>. In the code sample, the error set type is not explicitly written on the left side of the! operator. When written this way, the error set type is a special kind of error union type that has aninferred error set type. Thevoid after the! operator tells the compiler that the function will not return a value under normal circumstances (i.e. no errors occur).

Note to experienced programmers: Zig also has the booleanoperator!a wherea is a value of typebool. Error union types contain the name of the type in the syntax:!<primitive type>.

In Zig, a function's block of statements and expressions are surrounded by{ and} curly-braces. Inside of themain function are expressions that perform the task of outputtingHello, world! to standard output.

First, a constant identifier,stdout, is initialized to represent standard output's writer. Then, the program tries to print theHello, world! message to standard output.

Functions sometimes need information to perform their task. In Zig, information is passed to functions between open( and close) parenthesis placed after the function's name. This information is also known as arguments. When there are multiple arguments passed to a function, they are separated by commas,.

The two arguments passed to thestdout.print() function,"Hello, {s}!\n" and.{"world"}, are evaluated atcompile-time. The code sample is purposely written to show how to performstring substitution in theprint function. The curly-braces inside of the first argument are substituted with the compile-time known value inside of the second argument (known as ananonymous struct literal). The\n inside of the double-quotes of the first argument is theescape sequence for the newline character. Thetry expression evaluates the result ofstdout.print. If the result is an error, then thetry expression will return frommain with the error. Otherwise, the program will continue. In this case, there are no more statements or expressions left to execute in themain function, so the program exits.

In Zig, the standard output writer'sprint function is allowed to fail because it is actually a function defined as part of a generic Writer. Consider a generic Writer that represents writing data to a file. When the disk is full, a write to the file will fail. However, we typically do not expect writing text to the standard output to fail. To avoid having to handle the failure case of printing to standard output, you can use alternate functions: the functions instd.log for proper logging or thestd.debug.print function. This documentation will use the latter option to print to standard error (stderr) and silently return on failure. The next code sample,hello_again.zig demonstrates the use ofstd.debug.print.

hello_again.zig

const print =@import("std").debug.print;pubfnmain()void {    print("Hello, world!\n", .{});}
$ zig build-exe hello_again.zig$ ./hello_againHello, world!

Note that you can leave off the! from the return type becausestd.debug.print cannot fail.

See also:

Comments§

comments.zig

const expect =@import("std").testing.expect;test"comments" {// Comments in Zig start with "//" and end at the next LF byte (end of line).// The below line is a comment, and won't be executed.//expect(false);const x =true;// another commenttry expect(x);}
$ zig test comments.zigTest [1/1] test "comments"... All 1 tests passed.

There are no multiline comments in Zig (e.g. like/* */ comments in C). This helps allow Zig to have the property that each line of code can be tokenized out of context.

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.

/// 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).pubfnunixEpoch() Timestamp {return Timestamp{            .seconds =0,            .nanos =0,        };    }};

Doc comments are only allowed in certain places; eventually, it will become 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.

Top-Level Doc Comments§

User documentation that doesn't belong to whatever immediately follows it, like package-level documentation, goes in top-level doc comments. A top-level doc comment is one that begins with two slashes and an exclamation point://!.

//! This module provides functions for retrieving the current date and//! time with varying degrees of precision and accuracy. It does not//! depend on libc, but will use functions from it if available.

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;pubfnmain()void {// integersconst one_plus_one:i32 =1 +1;    print("1 + 1 = {}\n", .{one_plus_one});// floatsconst seven_div_three:f32 =7.0 /3.0;    print("7.0 / 3.0 = {}\n", .{seven_div_three});// boolean    print("{}\n{}\n{}\n", .{trueandfalse,trueorfalse,        !true,    });// optionalvar optional_value: ?[]constu8 =null;    assert(optional_value ==null);    print("\noptional 1\ntype: {s}\nvalue: {s}\n", .{@typeName(@TypeOf(optional_value)),        optional_value,    });    optional_value ="hi";    assert(optional_value !=null);    print("\noptional 2\ntype: {s}\nvalue: {s}\n", .{@typeName(@TypeOf(optional_value)),        optional_value,    });// error unionvar number_or_error:anyerror!i32 =error.ArgNotFound;    print("\nerror union 1\ntype: {s}\nvalue: {}\n", .{@typeName(@TypeOf(number_or_error)),        number_or_error,    });    number_or_error =1234;    print("\nerror union 2\ntype: {s}\nvalue: {}\n", .{@typeName(@TypeOf(number_or_error)),        number_or_error,    });}
$ zig build-exe values.zig$ ./values1 + 1 = 27.0 / 3.0 = 2.33333325e+00falsetruefalseoptional 1type: ?[]const u8value: nulloptional 2type: ?[]const u8value: hierror union 1type: anyerror!i32value: error.ArgNotFounderror union 2type: anyerror!i32value: 1234

Primitive Types§

Name C Equivalent Description
i8int8_tsigned 8-bit integer
u8uint8_tunsigned 8-bit integer
i16int16_tsigned 16-bit integer
u16uint16_tunsigned 16-bit integer
i32int32_tsigned 32-bit integer
u32uint32_tunsigned 32-bit integer
i64int64_tsigned 64-bit integer
u64uint64_tunsigned 64-bit integer
i128__int128signed 128-bit integer
u128unsigned __int128unsigned 128-bit integer
isizeintptr_tsigned pointer sized integer
usizeuintptr_tunsigned pointer sized integer
c_shortshortfor ABI compatibility with C
c_ushortunsigned shortfor ABI compatibility with C
c_intintfor ABI compatibility with C
c_uintunsigned intfor ABI compatibility with C
c_longlongfor ABI compatibility with C
c_ulongunsigned longfor ABI compatibility with C
c_longlonglong longfor ABI compatibility with C
c_ulonglongunsigned long longfor ABI compatibility with C
c_longdoublelong doublefor ABI compatibility with C
c_voidvoidfor ABI compatibility with C
f16_Float1616-bit floating point (10-bit mantissa) IEEE-754-2008 binary16
f32float32-bit floating point (23-bit mantissa) IEEE-754-2008 binary32
f64double64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64
f128_Float128128-bit floating point (112-bit mantissa) IEEE-754-2008 binary128
boolbooltrue orfalse
void(none)0 bit type
noreturn(none)the type ofbreak,continue,return,unreachable, andwhile (true) {}
type(none)the type of types
anyerror(none)an error code
comptime_int(none)Only allowed forcomptime-known values. The type of integer literals.
comptime_float(none)Only allowed forcomptime-known values. The type of float literals.

In addition to the integer types above, arbitrary bit-width integers can be referenced by using an identifier ofi oru followed by digits. For example, the identifieri7 refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is65535.

See also:

Primitive Values§

Name Description
true andfalsebool values
nullused to set an optional type tonull
undefinedused to leave a value unspecified

See also:

String Literals and Unicode Code Point Literals§

String literals are constant single-itemPointers 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 becoerced to bothSlices andNull-Terminated Pointers. Dereferencing string literals converts them toArrays.

The encoding of a string in Zig is de-facto assumed to be UTF-8. Because Zig source code isUTF-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. However, it is possible to embed non-UTF-8 bytes into a string literal using\xNN notation.

Unicode code point literals have typecomptime_int, the same asInteger Literals. AllEscape Sequences are valid in both string literals and Unicode code point literals.

In many other programming languages, a Unicode code point literal is called a "character literal". However, there isno precise technical definition of a "character" in recent versions of the Unicode specification (as of Unicode 13.0). In Zig, a Unicode code point literal corresponds to the Unicode definition of a code point.

test.zig

const expect =@import("std").testing.expect;const mem =@import("std").mem;test"string literals" {const bytes ="hello";try expect(@TypeOf(bytes) == *const [5:0]u8);try expect(bytes.len ==5);try expect(bytes[1] =='e');try expect(bytes[5] ==0);try expect('e' =='\x65');try expect('\u{1f4a9}' ==128169);try expect('💯' ==128175);try expect(mem.eql(u8,"hello","h\x65llo"));try expect("\xff"[0] ==0xff);// non-UTF-8 strings are possible with \xNN notation.}
$ zig test test.zigTest [1/1] test "string literals"... All 1 tests passed.

See also:

Escape Sequences§

Escape Sequence Name
\nNewline
\rCarriage Return
\tTab
\\Backslash
\'Single Quote
\"Double Quote
\xNNhexadecimal 8-bit byte value (2 digits)
\u{NNNNNN}hexadecimal Unicode code point UTF-8 encoded (1 or more digits)

Note that the maximum valid Unicode point is0x10ffff.

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.

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

See also:

Assignment§

Use theconst keyword to assign a value to an identifier:

test.zig

const x =1234;fnfoo()void {// It works at file scope as well as inside functions.const y =5678;// Once assigned, an identifier cannot be changed.    y +=1;}test"assignment" {    foo();}
$ zig test test.zig./docgen_tmp/test.zig:8:7:error:cannot assign to constant    y += 1;^

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 thevar keyword:

test.zig

const expect =@import("std").testing.expect;test"var" {var y:i32 =5678;    y +=1;try expect(y ==5679);}
$ zig test test.zigTest [1/1] test "var"... All 1 tests passed.

Variables must be initialized:

test.zig

test"initialization" {var x:i32;    x =1;}
$ zig test test.zig./docgen_tmp/test.zig:2:5:error:variables must be initialized    var x: i32;^

undefined§

Useundefined to leave variables uninitialized:

test.zig

const expect =@import("std").testing.expect;test"init with undefined" {var x:i32 =undefined;    x =1;try expect(x ==1);}
$ zig test test.zigTest [1/1] test "init with undefined"... All 1 tests passed.

undefined can becoerced to any type. Once this happens, it is no longer possible to detect that the value isundefined.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."

InDebug mode, Zig writes0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger.

Variables§

A variable is a unit ofMemory storage.

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

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

Container Level Variables§

Container level variables have static lifetime and are order-independent and lazily analyzed. The initialization value of container level variables is implicitlycomptime. If a container level variable isconst then its value iscomptime-known, otherwise it is runtime-known.

container_level_variables.zig

var y:i32 = add(10, x);const x:i32 = add(12,34);test"container level variables" {try expect(x ==46);try expect(y ==56);}fnadd(a:i32, b:i32)i32 {return a + b;}const std =@import("std");const expect = std.testing.expect;
$ zig test container_level_variables.zigTest [1/1] test "container level variables"... All 1 tests passed.

Container level variables may be declared inside astruct,union, orenum:

namespaced_container_level_variable.zig

const std =@import("std");const expect = std.testing.expect;test"namespaced container level variable" {try expect(foo() ==1235);try expect(foo() ==1236);}const S =struct {var x:i32 =1234;};fnfoo()i32 {    S.x +=1;return S.x;}
$ zig test namespaced_container_level_variable.zigTest [1/1] test "namespaced container level variable"... All 1 tests passed.

Static Local Variables§

It is also possible to have local variables with static lifetime by using containers inside functions.

static_local_variable.zig

const std =@import("std");const expect = std.testing.expect;test"static local variable" {try expect(foo() ==1235);try expect(foo() ==1236);  }fnfoo()i32 {const S =struct {var x:i32 =1234;      };      S.x +=1;return S.x;  }
$ zig test static_local_variable.zigTest [1/1] test "static local variable"... All 1 tests passed.

Theextern keyword or@extern builtin function can be used to link against a variable that is exported from another object. Theexport 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:

Thread Local Variables§

A variable may be specified to be a thread-local variable using thethreadlocal keyword:

tls.zig

const std =@import("std");const assert = std.debug.assert;threadlocalvar x:i32 =1234;test"thread local storage" {const thread1 =try std.Thread.spawn(testTls, {});const thread2 =try std.Thread.spawn(testTls, {});    testTls({});    thread1.wait();    thread2.wait();}fntestTls(context:void)void {    assert(x ==1234);    x +=1;    assert(x ==1235);}
$ zig test tls.zigTest [1/1] test "thread local storage"... All 1 tests passed.

ForSingle Threaded Builds, all thread local variables are treated as regularContainer Level Variables.

Thread local variables may not beconst.

Local Variables§

Local variables occur insideFunctions,comptime blocks, and@cImport blocks.

When a local variable isconst, it means that after initialization, the variable's value will not change. If the initialization value of aconst variable iscomptime-known, then the variable is alsocomptime-known.

A local variable may be qualified with thecomptime keyword. This causes the variable's value to becomptime-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 acomptime expression are implicitlycomptime variables.

comptime_vars.zig

const std =@import("std");const expect = std.testing.expect;test"comptime vars" {var x:i32 =1;comptimevar y:i32 =1;    x +=1;    y +=1;try expect(x ==2);try expect(y ==2);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 comptime_vars.zigTest [1/1] test "comptime vars"... All 1 tests passed.

Integers§

Integer Literals§

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 separatorconst 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 undefined 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 undefined behavior.

fndivide(a:i32, b:i32)i32 {return a / b;}

In this function, valuesa andb are known only at runtime, and thus this division operation is vulnerable to bothInteger Overflow andDivision by Zero.

Operators such as+ and- cause undefined behavior on integer overflow. Also available are operations such as+% and-% which are defined to have wrapping arithmetic on all targets.

Zig supports arbitrary bit-width integers, referenced by using an identifier ofi oru followed by digits. For example, the identifieri7 refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is65535.

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
  • f128 - IEEE-754-2008 binary128
  • c_longdouble - matcheslong double for the target C ABI

Float Literals§

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

Float literalscoerce to any floating point type, and to anyinteger type when there is no fractional component.

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 separatorconst 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:

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 useStrict mode, but you can switch toOptimized mode on a per-block basis:

foo.zig

const std =@import("std");const big =@as(f64,1 <<40);exportfnfoo_strict(x:f64)f64 {return x + big - big;}exportfnfoo_optimized(x:f64)f64 {@setFloatMode(.Optimized);return x + big - big;}
$ zig build-obj foo.zig -O ReleaseFast

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.zig

const print =@import("std").debug.print;externfnfoo_strict(x:f64)f64;externfnfoo_optimized(x:f64)f64;pubfnmain()void {const x =0.001;    print("optimized = {}\n", .{foo_optimized(x)});    print("strict = {}\n", .{foo_strict(x)});}
$ zig build-exe float_mode.zig foo.o$ ./float_modeoptimized = 1.0e-03strict = 9.765625e-04

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§

Syntax Relevant Types Description Example
a + ba += b
Addition.
2 +5 ==7
a +% ba +%= b
Wrapping Addition.
@as(u32, std.math.maxInt(u32)) +%1 ==0
a - ba -= b
Subtraction.
2 -5 == -3
a -% ba -%= b
Wrapping Subtraction.
@as(u32,0) -%1 == std.math.maxInt(u32)
-a
Negation.
-1 ==0 -1
-%a
Wrapping Negation.
  • Guaranteed to have twos-complement wrapping behavior.
-%@as(i32, std.math.minInt(i32)) == std.math.minInt(i32)
a * ba *= b
Multiplication.
2 *5 ==10
a *% ba *%= b
Wrapping Multiplication.
@as(u8,200) *%2 ==144
a / ba /= b
Division.
10 /5 ==2
a % ba %= b
Remainder Division.
10 %3 ==1
a << ba <<= b
Bit Shift Left.
1 <<8 ==256
a >> ba >>= b
Bit Shift Right.
10 >>1 ==5
a & ba &= b
Bitwise AND.
0b011 &0b101 ==0b001
a | ba |= b
Bitwise OR.
0b010 |0b100 ==0b110
a ^ ba ^= b
Bitwise XOR.
0b011 ^0b101 ==0b110
~a
Bitwise NOT.
~@as(u8,0b10101111) ==0b01010000
aorelse b
Ifa isnull, returnsb ("default value"), otherwise returns the unwrapped value ofa. Note thatb may be a value of typenoreturn.
const value: ?u32 =null;const unwrapped = valueorelse1234;unwrapped ==1234
a.?
Equivalent to:
aorelseunreachable
const value: ?u32 =5678;value.? ==5678
acatch bacatch |err| b
Ifa is anerror, returnsb ("default value"), otherwise returns the unwrapped value ofa. Note thatb may be a value of typenoreturn.err is theerror and is in scope of the expressionb.
const value:anyerror!u32 =error.Broken;const unwrapped = valuecatch1234;unwrapped ==1234
aand b
Ifa isfalse, returnsfalse without evaluatingb. Otherwise, returnsb.
(falseandtrue) ==false
aor b
Ifa istrue, returnstrue without evaluatingb. Otherwise, returnsb.
falseortrue ==true
!a
Boolean NOT.
!false ==true
a == b
Returnstrue if a and b are equal, otherwise returnsfalse. InvokesPeer Type Resolution for the operands.
(1 ==1) ==true
a ==null
Returnstrue if a isnull, otherwise returnsfalse.
const value: ?u32 =null;value ==null
a != b
Returnsfalse if a and b are equal, otherwise returnstrue. InvokesPeer Type Resolution for the operands.
(1 !=1) ==false
a > b
Returnstrue if a is greater than b, otherwise returnsfalse. InvokesPeer Type Resolution for the operands.
(2 >1) ==true
a >= b
Returnstrue if a is greater than or equal to b, otherwise returnsfalse. InvokesPeer Type Resolution for the operands.
(2 >=1) ==true
a < b
Returnstrue if a is less than b, otherwise returnsfalse. InvokesPeer Type Resolution for the operands.
(1 <2) ==true>
a <= b
Returnstrue if a is less than or equal to b, otherwise returnsfalse. InvokesPeer Type Resolution for the operands.
(1 <=2) ==true
a ++ b
Array concatenation.
const mem =@import("std").mem;const array1 = [_]u32{1,2};const array2 = [_]u32{3,4};const together = array1 ++ array2;mem.eql(u32, &together, &[_]u32{1,2,3,4})
a ** b
Array multiplication.
const mem =@import("std").mem;const pattern ="ab" **3;mem.eql(u8, pattern,"ababab")
a.*
Pointer dereference.
const x:u32 =1234;const ptr = &x;ptr.* ==1234
&a
All types Address of.
const x:u32 =1234;const ptr = &x;ptr.* ==1234
a || b
Merging Error Sets
const A =error{One};const B =error{Two};(A || B) ==error{One, Two}

Precedence§

x() x[] x.y x.* x.?a!bx{}!x -x -%x ~x &x ?x* / % ** *% ||+ - ++ +% -%<< >>& ^ |orelsecatch== != < > <= >=andor= *= /= %= += -= <<= >>= &= ^= |=

Arrays§

arrays.zig

const expect =@import("std").testing.expect;const assert =@import("std").debug.assert;const mem =@import("std").mem;// array literalconst message = [_]u8{'h','e','l','l','o' };// get the size of an arraycomptime {    assert(message.len ==5);}// A string literal is a single-item pointer to an array literal.const same_message ="hello";comptime {    assert(mem.eql(u8, &message, same_message));}test"iterate over an array" {var sum:usize =0;for (message) |byte| {        sum += byte;    }try expect(sum =='h' +'e' +'l' *2 +'o');}// modifiable arrayvar some_integers: [100]i32 =undefined;test"modify an array" {for (some_integers) |*item, i| {        item.* =@intCast(i32, i);    }try expect(some_integers[10] ==10);try expect(some_integers[99] ==99);}// array concatenation works if the values are known// at compile timeconst part_one = [_]i32{1,2,3,4 };const part_two = [_]i32{5,6,7,8 };const all_of_it = part_one ++ part_two;comptime {    assert(mem.eql(i32, &all_of_it, &[_]i32{1,2,3,4,5,6,7,8 }));}// remember that string literals are arraysconst hello ="hello";const world ="world";const hello_world = hello ++" " ++ world;comptime {    assert(mem.eql(u8, hello_world,"hello world"));}// ** does repeating patternsconst pattern ="ab" **3;comptime {    assert(mem.eql(u8, pattern,"ababab"));}// initialize an array to zeroconst all_zero = [_]u16{0} **10;comptime {    assert(all_zero.len ==10);    assert(all_zero[5] ==0);}// use compile-time code to initialize an arrayvar fancy_array = init: {var initial_value: [10]Point =undefined;for (initial_value) |*pt, i| {        pt.* = Point{            .x =@intCast(i32, i),            .y =@intCast(i32, i) *2,        };    }break :init initial_value;};const Point =struct {    x:i32,    y:i32,};test"compile-time array initialization" {try expect(fancy_array[4].x ==4);try expect(fancy_array[4].y ==8);}// call a function to initialize an arrayvar more_points = [_]Point{makePoint(3)} **10;fnmakePoint(x:i32) Point {return Point{        .x = x,        .y = x *2,    };}test"array initialization with function calls" {try expect(more_points[4].x ==3);try expect(more_points[4].y ==6);try expect(more_points.len ==10);}
$ zig test arrays.zigTest [1/4] test "iterate over an array"... Test [2/4] test "modify an array"... Test [3/4] test "compile-time array initialization"... Test [4/4] test "array initialization with function calls"... All 4 tests passed.

See also:

Anonymous List Literals§

Similar toEnum Literals andAnonymous Struct Literals the type can be omitted from array literals:

anon_list.zig

const std =@import("std");const expect = std.testing.expect;test"anonymous list literal syntax" {var array: [4]u8 = .{11,22,33,44};try expect(array[0] ==11);try expect(array[1] ==22);try expect(array[2] ==33);try expect(array[3] ==44);}
$ zig test anon_list.zigTest [1/1] test "anonymous list literal syntax"... All 1 tests passed.

If there is no type in the result location then an anonymous list literal actually turns into astruct with numbered field names:

infer_list_literal.zig

const std =@import("std");const expect = std.testing.expect;test"fully anonymous list literal" {try dump(.{@as(u32,1234),@as(f64,12.34),true,"hi"});}fndump(args:anytype) !void {try expect(args.@"0" ==1234);try expect(args.@"1" ==12.34);try expect(args.@"2");try expect(args.@"3"[0] =='h');try expect(args.@"3"[1] =='i');}
$ zig test infer_list_literal.zigTest [1/1] test "fully anonymous list literal"... All 1 tests passed.

Multidimensional Arrays§

Mutlidimensional arrays can be created by nesting arrays:

multidimensional.zig

const std =@import("std");const expect = std.testing.expect;const mat4x4 = [4][4]f32{    [_]f32{1.0,0.0,0.0,0.0 },    [_]f32{0.0,1.0,0.0,1.0 },    [_]f32{0.0,0.0,1.0,0.0 },    [_]f32{0.0,0.0,0.0,1.0 },};test"multidimensional arrays" {// Access the 2D array by indexing the outer array, and then the inner array.try expect(mat4x4[1][1] ==1.0);// Here we iterate with for loops.for (mat4x4) |row, row_index| {for (row) |cell, column_index| {if (row_index == column_index) {try expect(cell ==1.0);            }        }    }}
$ zig test multidimensional.zigTest [1/1] test "multidimensional arrays"... All 1 tests passed.

Sentinel-Terminated Arrays§

The syntax[N:x]T describes an array which has a sentinel element of valuex at the index corresponding tolen.

null_terminated_array.zig

const std =@import("std");const expect = std.testing.expect;test"null terminated array" {const array = [_:0]u8 {1,2,3,4};try expect(@TypeOf(array) == [4:0]u8);try expect(array.len ==4);try expect(array[4] ==0);}
$ zig test null_terminated_array.zigTest [1/1] test "null terminated array"... All 1 tests passed.

See also:

Vectors§

A vector is a group of booleans,Integers,Floats, orPointers which are operated on in parallel using a single instruction (SIMD). Vector types are created with the builtin function@Type, or using the shorthand asstd.meta.Vector.

TODO talk about C ABI interop

SIMD§

TODO Zig's SIMD abilities are just beginning to be fleshed out. Here are some talking points to update the docs with: * What kind of operations can you do? All the operations on integers and floats? What about mixing scalar and vector? * How to convert to/from vectors/arrays * How to access individual elements from vectors, how to loop over the elements * "shuffle" * Advice on writing high perf software, how to abstract the best way

Pointers§

Zig has two kinds of pointers: single-item and many-item.

  • *T - single-item pointer to exactly one item.
    • Supports deref syntax:ptr.*
  • [*]T - many-item pointer to unknown number of items.
    • Supports index syntax:ptr[i]
    • Supports slice syntax:ptr[start..end]
    • Supports pointer arithmetic:ptr + x,ptr - x
    • T must have a known size, which means that it cannot bec_void or any otheropaque type.

These types are closely related toArrays andSlices:

  • *[N]T - pointer to N items, same as single-item pointer to an array.
    • Supports index syntax:array_ptr[i]
    • Supports slice syntax:array_ptr[start..end]
    • Supports len property:array_ptr.len
  • []T - pointer to runtime-known number of items.
    • Supports index syntax:slice[i]
    • Supports slice syntax:slice[start..end]
    • Supports len property:slice.len

Use&x to obtain a single-item pointer:

test.zig

const expect =@import("std").testing.expect;test"address of syntax" {// Get the address of a variable:const x:i32 =1234;const x_ptr = &x;// Dereference a pointer:try expect(x_ptr.* ==1234);// When you get the address of a const variable, you get a const single-item pointer.try expect(@TypeOf(x_ptr) == *consti32);// If you want to mutate the value, you'd need an address of a mutable variable:var y:i32 =5678;const y_ptr = &y;try expect(@TypeOf(y_ptr) == *i32);    y_ptr.* +=1;try expect(y_ptr.* ==5679);}test"pointer array access" {// Taking an address of an individual element gives a// single-item pointer. This kind of pointer// does not support pointer arithmetic.var array = [_]u8{1,2,3,4,5,6,7,8,9,10 };const ptr = &array[2];try expect(@TypeOf(ptr) == *u8);try expect(array[2] ==3);    ptr.* +=1;try expect(array[2] ==4);}
$ zig test test.zigTest [1/2] test "address of syntax"... Test [2/2] test "pointer array access"... All 2 tests passed.

In Zig, we generally preferSlices rather thanSentinel-Terminated Pointers. You can turn an array or pointer into a slice using slice syntax.

Slices have bounds checking and are therefore protected against this kind of undefined behavior. This is one reason we prefer slices to pointers.

test.zig

const expect =@import("std").testing.expect;test"pointer slicing" {var array = [_]u8{1,2,3,4,5,6,7,8,9,10 };const slice = array[2..4];try expect(slice.len ==2);try expect(array[3] ==4);    slice[1] +=1;try expect(array[3] ==5);}
$ zig test test.zigTest [1/1] test "pointer slicing"... All 1 tests passed.

Pointers work at compile-time too, as long as the code does not depend on an undefined memory layout:

test.zig

const expect =@import("std").testing.expect;test"comptime pointers" {comptime {var x:i32 =1;const ptr = &x;        ptr.* +=1;        x +=1;try expect(ptr.* ==3);    }}
$ zig test test.zigTest [1/1] test "comptime pointers"... All 1 tests passed.

To convert an integer address into a pointer, use@intToPtr. To convert a pointer to an integer, use@ptrToInt:

test.zig

const expect =@import("std").testing.expect;test"@ptrToInt and @intToPtr" {const ptr =@intToPtr(*i32,0xdeadbee0);const addr =@ptrToInt(ptr);try expect(@TypeOf(addr) ==usize);try expect(addr ==0xdeadbee0);}
$ zig test test.zigTest [1/1] test "@ptrToInt and @intToPtr"... All 1 tests passed.

Zig is able to preserve memory addresses in comptime code, as long as the pointer is never dereferenced:

test.zig

const expect =@import("std").testing.expect;test"comptime @intToPtr" {comptime {// Zig is able to do this at compile-time, as long as// ptr is never dereferenced.const ptr =@intToPtr(*i32,0xdeadbee0);const addr =@ptrToInt(ptr);try expect(@TypeOf(addr) ==usize);try expect(addr ==0xdeadbee0);    }}
$ zig test test.zigTest [1/1] test "comptime @intToPtr"... All 1 tests passed.

See also:

volatile§

Loads and stores are assumed to not have side effects. If a given load or store should have side effects, such as Memory Mapped Input/Output (MMIO), usevolatile. In the following code, loads and stores withmmio_ptr are guaranteed to all happen and in the same order as in source code:

test.zig

const expect =@import("std").testing.expect;test"volatile" {const mmio_ptr =@intToPtr(*volatileu8,0x12345678);try expect(@TypeOf(mmio_ptr) == *volatileu8);}
$ zig test test.zigTest [1/1] test "volatile"... All 1 tests passed.

Note thatvolatile is unrelated to concurrency andAtomics. If you see code that is usingvolatile for something other than Memory Mapped Input/Output, it is probably a bug.

To convert one pointer type to another, use@ptrCast. This is an unsafe operation that Zig cannot protect you against. Use@ptrCast only when other conversions are not possible.

test.zig

const std =@import("std");const expect = std.testing.expect;test"pointer casting" {const bytesalign(@alignOf(u32)) = [_]u8{0x12,0x12,0x12,0x12 };const u32_ptr =@ptrCast(*constu32, &bytes);try expect(u32_ptr.* ==0x12121212);// Even this example is contrived - there are better ways to do the above than// pointer casting. For example, using a slice narrowing cast:const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];try expect(u32_value ==0x12121212);// And even another way, the most straightforward way to do it:try expect(@bitCast(u32, bytes) ==0x12121212);}test"pointer child type" {// pointer types have a `child` field which tells you the type they point to.try expect(@typeInfo(*u32).Pointer.child ==u32);}
$ zig test test.zigTest [1/2] test "pointer casting"... Test [2/2] test "pointer child type"... All 2 tests passed.

Alignment§

Each type has analignment - a number of bytes such that, when a value of the type is loaded from or stored to memory, the memory address must be evenly divisible by this number. You can use@alignOf to find out this value for any type.

Alignment depends on the CPU architecture, but is always a power of two, and less than1 <<29.

In Zig, a pointer type has an alignment value. If the value is equal to the alignment of the underlying type, it can be omitted from the type:

test.zig

const std =@import("std");const expect = std.testing.expect;test"variable alignment" {var x:i32 =1234;const align_of_i32 =@alignOf(@TypeOf(x));try expect(@TypeOf(&x) == *i32);try expect(*i32 == *align(align_of_i32)i32);if (std.Target.current.cpu.arch == .x86_64) {try expect(@typeInfo(*i32).Pointer.alignment ==4);    }}
$ zig test test.zigTest [1/1] test "variable alignment"... All 1 tests passed.

In the same way that a*i32 can becoerced to a*consti32, a pointer with a larger alignment can be implicitly cast to a pointer with a smaller alignment, but not vice versa.

You can specify alignment on variables and functions. If you do this, then pointers to them get the specified alignment:

test.zig

const expect =@import("std").testing.expect;var foo:u8align(4) =100;test"global variable alignment" {try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment ==4);try expect(@TypeOf(&foo) == *align(4)u8);const as_pointer_to_array: *[1]u8 = &foo;const as_slice: []u8 = as_pointer_to_array;try expect(@TypeOf(as_slice) == []align(4)u8);}fnderp()align(@sizeOf(usize) *2)i32 {return1234; }fnnoop1()align(1)void {}fnnoop4()align(4)void {}test"function alignment" {try expect(derp() ==1234);try expect(@TypeOf(noop1) ==fn()align(1)void);try expect(@TypeOf(noop4) ==fn()align(4)void);    noop1();    noop4();}
$ zig test test.zigTest [1/2] test "global variable alignment"... Test [2/2] test "function alignment"... All 2 tests passed.

If you have a pointer or a slice that has a small alignment, but you know that it actually has a bigger alignment, use@alignCast to change the pointer into a more aligned pointer. This is a no-op at runtime, but inserts asafety check:

test.zig

const std =@import("std");test"pointer alignment safety" {var arrayalign(4) = [_]u32{0x11111111,0x11111111 };const bytes = std.mem.sliceAsBytes(array[0..]);try std.testing.expect(foo(bytes) ==0x11111111);}fnfoo(bytes: []u8)u32 {const slice4 = bytes[1..5];const int_slice = std.mem.bytesAsSlice(u32,@alignCast(4, slice4));return int_slice[0];}
$ zig test test.zig Test [1/1] test "pointer alignment safety"... thread 10521 panic: incorrect alignment/home/andy/Downloads/zig/docgen_tmp/test.zig:10:63:0x2081bc in foo (test)    const int_slice = std.mem.bytesAsSlice(u32, @alignCast(4, slice4));^/home/andy/Downloads/zig/docgen_tmp/test.zig:6:31:0x206799 in test "pointer alignment safety" (test)    try std.testing.expect(foo(bytes) == 0x11111111);^/home/andy/Downloads/zig/lib/std/special/test_runner.zig:76:28:0x22d2da in std.special.main (test)        } else test_fn.func();^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x22583a in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20970e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2086a6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x2084b2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^error: the following test command crashed:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

allowzero§

This pointer attribute allows a pointer to have address zero. This is only ever needed on the freestanding OS target, where the address zero is mappable. If you want to represent null pointers, useOptional Pointers instead.Optional Pointers withallowzero are not the same size as pointers. In this code example, if the pointer did not have theallowzero attribute, this would be aPointer Cast Invalid Null panic:

allowzero.zig

const std =@import("std");const expect = std.testing.expect;test"allowzero" {var zero:usize =0;var ptr =@intToPtr(*allowzeroi32, zero);try expect(@ptrToInt(ptr) ==0);}
$ zig test allowzero.zigTest [1/1] test "allowzero"... All 1 tests passed.

Sentinel-Terminated Pointers§

The syntax[*:x]T describes a pointer that has a length determined by a sentinel value. This provides protection against buffer overflow and overreads.

test.zig

const std =@import("std");// This is also available as `std.c.printf`.pubextern"c"fnprintf(format: [*:0]constu8, ...)c_int;pubfnmain()anyerror!void {    _ = printf("Hello, world!\n");// OKconst msg ="Hello, world!\n";const non_null_terminated_msg: [msg.len]u8 = msg.*;    _ = printf(&non_null_terminated_msg);}
$ zig build-exe test.zig -lc./docgen_tmp/test.zig:11:17:error:expected type '[*:0]const u8', found '*const [14]u8'    _ = printf(&non_null_terminated_msg);^./docgen_tmp/test.zig:11:17:note:destination pointer requires a terminating '0' sentinel    _ = printf(&non_null_terminated_msg);^

See also:

Slices§

test.zig

const expect =@import("std").testing.expect;test"basic slices" {var array = [_]i32{1,2,3,4 };// A slice is a pointer and a length. The difference between an array and// a slice is that the array's length is part of the type and known at// compile-time, whereas the slice's length is known at runtime.// Both can be accessed with the `len` field.var known_at_runtime_zero:usize =0;const slice = array[known_at_runtime_zero..array.len];try expect(&slice[0] == &array[0]);try expect(slice.len == array.len);// Using the address-of operator on a slice gives a single-item pointer,// while using the `ptr` field gives a many-item pointer.try expect(@TypeOf(slice.ptr) == [*]i32);try expect(@TypeOf(&slice[0]) == *i32);try expect(@ptrToInt(slice.ptr) ==@ptrToInt(&slice[0]));// Slices have array bounds checking. If you try to access something out// of bounds, you'll get a safety check failure:    slice[10] +=1;// Note that `slice.ptr` does not invoke safety checking, while `&slice[0]`// asserts that the slice has len >= 1.}
$ zig test test.zig Test [1/1] test "basic slices"... thread 10827 panic: index out of bounds/home/andy/Downloads/zig/docgen_tmp/test.zig:22:10:0x2069b3 in test "basic slices" (test)    slice[10] += 1;^/home/andy/Downloads/zig/lib/std/special/test_runner.zig:76:28:0x22d2da in std.special.main (test)        } else test_fn.func();^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x22583a in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20970e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x208736 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x208542 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^error: the following test command crashed:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

This is one reason we prefer slices to pointers.

slices.zig

const std =@import("std");const expect = std.testing.expect;const mem = std.mem;const fmt = std.fmt;test"using slices for strings" {// Zig has no concept of strings. String literals are const pointers// to null-terminated arrays of u8, and by convention parameters// that are "strings" are expected to be UTF-8 encoded slices of u8.// Here we coerce *const [5:0]u8 and *const [6:0]u8 to []const u8const hello: []constu8 ="hello";const world: []constu8 ="世界";var all_together: [100]u8 =undefined;// You can use slice syntax on an array to convert an array into a slice.const all_together_slice = all_together[0..];// String concatenation example.const hello_world =try fmt.bufPrint(all_together_slice,"{s} {s}", .{ hello, world });// Generally, you can use UTF-8 and not worry about whether something is a// string. If you don't need to deal with individual characters, no need// to decode.try expect(mem.eql(u8, hello_world,"hello 世界"));}test"slice pointer" {var array: [10]u8 =undefined;const ptr = &array;// You can use slicing syntax to convert a pointer into a slice:const slice = ptr[0..5];    slice[2] =3;try expect(slice[2] ==3);// The slice is mutable because we sliced a mutable pointer.// Furthermore, it is actually a pointer to an array, since the start// and end indexes were both comptime-known.try expect(@TypeOf(slice) == *[5]u8);// You can also slice a slice:const slice2 = slice[2..3];try expect(slice2.len ==1);try expect(slice2[0] ==3);}
$ zig test slices.zigTest [1/2] test "using slices for strings"... Test [2/2] test "slice pointer"... All 2 tests passed.

See also:

Sentinel-Terminated Slices§

The syntax[:x]T is a slice which has a runtime known length and also guarantees a sentinel value at the element indexed by the length. The type does not guarantee that there are no sentinel elements before that. Sentinel-terminated slices allow element access to thelen index.

null_terminated_slice.zig

const std =@import("std");const expect = std.testing.expect;test"null terminated slice" {const slice: [:0]constu8 ="hello";try expect(slice.len ==5);try expect(slice[5] ==0);}
$ zig test null_terminated_slice.zigTest [1/1] test "null terminated slice"... All 1 tests passed.

See also:

struct§

structs.zig

// Declare a struct.// Zig gives no guarantees about the order of fields and the size of// the struct but the fields are guaranteed to be ABI-aligned.const Point =struct {    x:f32,    y:f32,};// Maybe we want to pass it to OpenGL so we want to be particular about// how the bytes are arranged.const Point2 =packedstruct {    x:f32,    y:f32,};// Declare an instance of a struct.const p = Point {    .x =0.12,    .y =0.34,};// Maybe we're not ready to fill out some of the fields.var p2 = Point {    .x =0.12,    .y =undefined,};// Structs can have methods// Struct methods are not special, they are only namespaced// functions that you can call with dot syntax.const Vec3 =struct {    x:f32,    y:f32,    z:f32,pubfninit(x:f32, y:f32, z:f32) Vec3 {return Vec3 {            .x = x,            .y = y,            .z = z,        };    }pubfndot(self: Vec3, other: Vec3)f32 {return self.x * other.x + self.y * other.y + self.z * other.z;    }};const expect =@import("std").testing.expect;test"dot product" {const v1 = Vec3.init(1.0,0.0,0.0);const v2 = Vec3.init(0.0,1.0,0.0);try expect(v1.dot(v2) ==0.0);// Other than being available to call with dot syntax, struct methods are// not special. You can reference them as any other declaration inside// the struct:try expect(Vec3.dot(v1, v2) ==0.0);}// Structs can have declarations.// Structs can have 0 fields.const Empty =struct {pubconst PI =3.14;};test"struct namespaced variable" {try expect(Empty.PI ==3.14);try expect(@sizeOf(Empty) ==0);// you can still instantiate an empty structconst does_nothing = Empty {};}// struct field order is determined by the compiler for optimal performance.// however, you can still calculate a struct base pointer given a field pointer:fnsetYBasedOnX(x: *f32, y:f32)void {const point =@fieldParentPtr(Point,"x", x);    point.y = y;}test"field parent pointer" {var point = Point {        .x =0.1234,        .y =0.5678,    };    setYBasedOnX(&point.x,0.9);try expect(point.y ==0.9);}// You can return a struct from a function. This is how we do generics// in Zig:fnLinkedList(comptime T:type)type {returnstruct {pubconst Node =struct {            prev: ?*Node,            next: ?*Node,            data: T,        };        first: ?*Node,        last:  ?*Node,        len:usize,    };}test"linked list" {// Functions called at compile-time are memoized. This means you can// do this:try expect(LinkedList(i32) == LinkedList(i32));var list = LinkedList(i32) {        .first =null,        .last =null,        .len =0,    };try expect(list.len ==0);// Since types are first class values you can instantiate the type// by assigning it to a variable:const ListOfInts = LinkedList(i32);try expect(ListOfInts == LinkedList(i32));var node = ListOfInts.Node {        .prev =null,        .next =null,        .data =1234,    };var list2 = LinkedList(i32) {        .first = &node,        .last = &node,        .len =1,    };try expect(list2.first.?.data ==1234);}
$ zig test structs.zigTest [1/4] test "dot product"... Test [2/4] test "struct namespaced variable"... Test [3/4] test "field parent pointer"... Test [4/4] test "linked list"... All 4 tests passed.

Default Field Values§

Each struct field may have an expression indicating the default field value. Such expressions are executed atcomptime, and allow the field to be omitted in a struct literal expression:

test.zig

const Foo =struct {    a:i32 =1234,    b:i32,};test"default struct initialization fields" {const x = Foo{        .b =5,    };if (x.a + x.b !=1239) {@compileError("it's even comptime known!");    }}
$ zig test test.zigTest [1/1] test "default struct initialization fields"... All 1 tests passed.

extern struct§

Anexternstruct has in-memory layout guaranteed to match the C ABI for the target.

This kind of struct should only be used for compatibility with the C ABI. Every other use case should be solved withpacked struct or normalstruct.

See also:

packed struct§

Unlike normal structs,packed structs have guaranteed in-memory layout:

  • Fields remain in the order declared.
  • There is no padding between fields.
  • Zig supports arbitrary widthIntegers and although normally, integers with fewer than 8 bits will still use 1 byte of memory, in packed structs, they use exactly their bit width.
  • bool fields use exactly 1 bit.
  • Anenum field uses exactly the bit width of its integer tag type.
  • Apacked union field uses exactly the bit width of the union field with the largest bit width.
  • Non-ABI-aligned fields are packed into the smallest possible ABI-aligned integers in accordance with the target endianness.

This means that apackedstruct can participate in a@bitCast or a@ptrCast to reinterpret memory. This even works atcomptime:

test.zig

const std =@import("std");const native_endian =@import("builtin").target.cpu.arch.endian();const expect = std.testing.expect;const Full =packedstruct {    number:u16,};const Divided =packedstruct {    half1:u8,    quarter3:u4,    quarter4:u4,};test"@bitCast between packed structs" {try doTheTest();comptimetry doTheTest();}fndoTheTest() !void {try expect(@sizeOf(Full) ==2);try expect(@sizeOf(Divided) ==2);var full = Full{ .number =0x1234 };var divided =@bitCast(Divided, full);switch (native_endian) {        .Big => {try expect(divided.half1 ==0x12);try expect(divided.quarter3 ==0x3);try expect(divided.quarter4 ==0x4);        },        .Little => {try expect(divided.half1 ==0x34);try expect(divided.quarter3 ==0x2);try expect(divided.quarter4 ==0x1);        },    }}
$ zig test test.zigTest [1/1] test "@bitCast between packed structs"... All 1 tests passed.

Zig allows the address to be taken of a non-byte-aligned field:

test.zig

const std =@import("std");const expect = std.testing.expect;const BitField =packedstruct {    a:u3,    b:u3,    c:u2,};var foo = BitField{    .a =1,    .b =2,    .c =3,};test"pointer to non-byte-aligned field" {const ptr = &foo.b;try expect(ptr.* ==2);}
$ zig test test.zigTest [1/1] test "pointer to non-byte-aligned field"... All 1 tests passed.

However, the pointer to a non-byte-aligned field has special properties and cannot be passed when a normal pointer is expected:

test.zig

const std =@import("std");const expect = std.testing.expect;const BitField =packedstruct {    a:u3,    b:u3,    c:u2,};var bit_field = BitField{    .a =1,    .b =2,    .c =3,};test"pointer to non-bit-aligned field" {try expect(bar(&bit_field.b) ==2);}fnbar(x: *constu3)u3 {return x.*;}
$ zig test test.zig./docgen_tmp/test.zig:17:30:error:expected type '*const u3', found '*align(:3:1) u3'    try expect(bar(&bit_field.b) == 2);^

In this case, the functionbar cannot be called becuse the pointer to the non-ABI-aligned field mentions the bit offset, but the function expects an ABI-aligned pointer.

Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:

test.zig

const std =@import("std");const expect = std.testing.expect;const BitField =packedstruct {    a:u3,    b:u3,    c:u2,};var bit_field = BitField{    .a =1,    .b =2,    .c =3,};test"pointer to non-bit-aligned field" {try expect(@ptrToInt(&bit_field.a) ==@ptrToInt(&bit_field.b));try expect(@ptrToInt(&bit_field.a) ==@ptrToInt(&bit_field.c));}
$ zig test test.zigTest [1/1] test "pointer to non-bit-aligned field"... All 1 tests passed.

This can be observed with@bitOffsetOf andbyteOffsetOf:

test.zig

const std =@import("std");const expect = std.testing.expect;const BitField =packedstruct {    a:u3,    b:u3,    c:u2,};test"pointer to non-bit-aligned field" {comptime {try expect(@bitOffsetOf(BitField,"a") ==0);try expect(@bitOffsetOf(BitField,"b") ==3);try expect(@bitOffsetOf(BitField,"c") ==6);try expect(@byteOffsetOf(BitField,"a") ==0);try expect(@byteOffsetOf(BitField,"b") ==0);try expect(@byteOffsetOf(BitField,"c") ==0);    }}
$ zig test test.zigTest [1/1] test "pointer to non-bit-aligned field"... All 1 tests passed.

Packed structs have 1-byte alignment. However if you have an overaligned pointer to a packed struct, Zig should correctly understand the alignment of fields. However there isa bug:

test.zig

const S =packedstruct {    a:u32,    b:u32,};test"overaligned pointer to packed struct" {var foo: Salign(4) =undefined;const ptr: *align(4) S = &foo;const ptr_to_b: *u32 = &ptr.b;}
$ zig test test.zig./docgen_tmp/test.zig:8:32:error:expected type '*u32', found '*align(1) u32'    const ptr_to_b: *u32 = &ptr.b;^

When this bug is fixed, the above test in the documentation will unexpectedly pass, which will cause the test suite to fail, notifying the bug fixer to update these docs.

It's also possible to set alignment of struct fields:

test.zig

const std =@import("std");const expectEqual = std.testing.expectEqual;test"aligned struct fields" {const S =struct {        a:u32align(2),        b:u32align(64),    };var foo = S{ .a =1, .b =2 };try expectEqual(64,@alignOf(S));try expectEqual(*align(2)u32,@TypeOf(&foo.a));try expectEqual(*align(64)u32,@TypeOf(&foo.b));}
$ zig test test.zigTest [1/1] test "aligned struct fields"... All 1 tests passed.

Using packed structs withvolatile is problematic, and may be a compile error in the future. For details on this subscribe tothis issue. TODO update these docs with a recommendation on how to use packed structs with MMIO (the use case for volatile packed structs) once this issue is resolved. Don't worry, there will be a good solution for this use case in zig.

Struct Naming§

Since all structs are anonymous, Zig infers the type name based on a few rules.

  • If the struct is in the initialization expression of a variable, it gets named after that variable.
  • If the struct is in thereturn expression, it gets named after the function it is returning from, with the parameter values serialized.
  • Otherwise, the struct gets a name such as(anonymous struct at file.zig:7:38).

struct_name.zig

const std =@import("std");pubfnmain()void {const Foo =struct {};    std.debug.print("variable: {s}\n", .{@typeName(Foo)});    std.debug.print("anonymous: {s}\n", .{@typeName(struct {})});    std.debug.print("function: {s}\n", .{@typeName(List(i32))});}fnList(comptime T:type)type {returnstruct {        x: T,    };}
$ zig build-exe struct_name.zig$ ./struct_namevariable: Fooanonymous: struct:6:53function: List(i32)

Anonymous Struct Literals§

Zig allows omitting the struct type of a literal. When the result iscoerced, the struct literal will directly instantiate the result location, with no copy:

struct_result.zig

const std =@import("std");const expect = std.testing.expect;const Point =struct {x:i32, y:i32};test"anonymous struct literal" {var pt: Point = .{        .x =13,        .y =67,    };try expect(pt.x ==13);try expect(pt.y ==67);}
$ zig test struct_result.zigTest [1/1] test "anonymous struct literal"... All 1 tests passed.

The struct type can be inferred. Here the result location does not include a type, and so Zig infers the type:

struct_anon.zig

const std =@import("std");const expect = std.testing.expect;test"fully anonymous struct" {try dump(.{        .int =@as(u32,1234),        .float =@as(f64,12.34),        .b =true,        .s ="hi",    });}fndump(args:anytype) !void {try expect(args.int ==1234);try expect(args.float ==12.34);try expect(args.b);try expect(args.s[0] =='h');try expect(args.s[1] =='i');}
$ zig test struct_anon.zigTest [1/1] test "fully anonymous struct"... All 1 tests passed.

Anonymous structs can be created without specifying field names, and are referred to as "tuples".

The fields are implicitly named using numbers starting from 0. Because their names are integers, the@"0" syntax must be used to access them. Names inside@"" are always recognised as identifiers.

Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over withinline for.

tuple.zig

const std =@import("std");const expect = std.testing.expect;test"tuple" {const values = .{@as(u32,1234),@as(f64,12.34),true,"hi",    } ++ .{false} **2;try expect(values[0] ==1234);try expect(values[4] ==false);inlinefor (values) |v, i| {if (i !=2)continue;try expect(v);    }try expect(values.len ==6);try expect(values.@"3"[0] =='h');}
$ zig test tuple.zigTest [1/1] test "tuple"... All 1 tests passed.

See also:

enum§

enums.zig

const expect =@import("std").testing.expect;const mem =@import("std").mem;// Declare an enum.const Type =enum {    ok,    not_ok,};// Declare a specific instance of the enum variant.const c = Type.ok;// If you want access to the ordinal value of an enum, you// can specify the tag type.const Value =enum(u2) {    zero,    one,    two,};// Now you can cast between u2 and Value.// The ordinal value starts from 0, counting up for each member.test"enum ordinal value" {try expect(@enumToInt(Value.zero) ==0);try expect(@enumToInt(Value.one) ==1);try expect(@enumToInt(Value.two) ==2);}// You can override the ordinal value for an enum.const Value2 =enum(u32) {    hundred =100,    thousand =1000,    million =1000000,};test"set enum ordinal value" {try expect(@enumToInt(Value2.hundred) ==100);try expect(@enumToInt(Value2.thousand) ==1000);try expect(@enumToInt(Value2.million) ==1000000);}// Enums can have methods, the same as structs and unions.// Enum methods are not special, they are only namespaced// functions that you can call with dot syntax.const Suit =enum {    clubs,    spades,    diamonds,    hearts,pubfnisClubs(self: Suit)bool {return self == Suit.clubs;    }};test"enum method" {const p = Suit.spades;try expect(!p.isClubs());}// An enum variant of different types can be switched upon.const Foo =enum {    string,    number,    none,};test"enum variant switch" {const p = Foo.number;const what_is_it =switch (p) {        Foo.string =>"this is a string",        Foo.number =>"this is a number",        Foo.none =>"this is a none",    };try expect(mem.eql(u8, what_is_it,"this is a number"));}// @typeInfo can be used to access the integer tag type of an enum.const Small =enum {    one,    two,    three,    four,};test"std.meta.Tag" {try expect(@typeInfo(Small).Enum.tag_type ==u2);}// @typeInfo tells us the field count and the fields names:test"@typeInfo" {try expect(@typeInfo(Small).Enum.fields.len ==4);try expect(mem.eql(u8,@typeInfo(Small).Enum.fields[1].name,"two"));}// @tagName gives a []const u8 representation of an enum value:test"@tagName" {try expect(mem.eql(u8,@tagName(Small.three),"three"));}
$ zig test enums.zigTest [1/7] test "enum ordinal value"... Test [2/7] test "set enum ordinal value"... Test [3/7] test "enum method"... Test [4/7] test "enum variant switch"... Test [5/7] test "std.meta.Tag"... Test [6/7] test "@typeInfo"... Test [7/7] test "@tagName"... All 7 tests passed.

See also:

extern enum§

By default, enums are not guaranteed to be compatible with the C ABI:

test.zig

const Foo =enum { a, b, c };exportfnentry(foo: Foo)void { }
$ zig build-obj test.zig./docgen_tmp/test.zig:2:22:error:parameter of type 'Foo' not allowed in function with calling convention 'C'export fn entry(foo: Foo) void { }^

For a C-ABI-compatible enum, useexternenum:

test.zig

const Foo =externenum { a, b, c };exportfnentry(foo: Foo)void { }
$ zig build-obj test.zig

Enum Literals§

Enum literals allow specifying the name of an enum field without specifying the enum type:

test.zig

const std =@import("std");const expect = std.testing.expect;const Color =enum {    auto,    off,    on,};test"enum literals" {const color1: Color = .auto;const color2 = Color.auto;try expect(color1 == color2);}test"switch using enum literals" {const color = Color.on;const result =switch (color) {        .auto =>false,        .on =>true,        .off =>false,    };try expect(result);}
$ zig test test.zigTest [1/2] test "enum literals"... Test [2/2] test "switch using enum literals"... All 2 tests passed.

Non-exhaustive enum§

A Non-exhaustive enum can be created by adding a trailing '_' field. It must specify a tag type and cannot consume every enumeration value.

@intToEnum on a non-exhaustive enum cannot fail.

A switch on a non-exhaustive enum can include a '_' prong as an alternative to anelse prong with the difference being that it makes it a compile error if all the known tag names are not handled by the switch.

test.zig

const std =@import("std");const expect = std.testing.expect;const Number =enum(u8) {    one,    two,    three,    _,};test"switch on non-exhaustive enum" {const number = Number.one;const result =switch (number) {        .one =>true,        .two,        .three =>false,        _ =>false,    };try expect(result);const is_one =switch (number) {        .one =>true,else =>false,    };try expect(is_one);}
$ zig test test.zigTest [1/1] test "switch on non-exhaustive enum"... All 1 tests passed.

union§

A bareunion defines a set of possible types that a value can be as a list of fields. Only one field can be active at a time. The in-memory representation of bare unions is not guaranteed. Bare unions cannot be used to reinterpret memory. For that, use@ptrCast, or use anextern union or apacked union which have guaranteed in-memory layout.Accessing the non-active field is safety-checkedUndefined Behavior:

test.zig

const Payload =union {    int:i64,    float:f64,    boolean:bool,};test"simple union" {var payload = Payload{ .int =1234 };    payload.float =12.34;}
$ zig test test.zigTest [1/1] test "simple union"... thread 11537 panic: access of inactive union field/home/andy/Downloads/zig/docgen_tmp/test.zig:8:12:0x20677b in test "simple union" (test)    payload.float = 12.34;^/home/andy/Downloads/zig/lib/std/special/test_runner.zig:76:28:0x22d05a in std.special.main (test)        } else test_fn.func();^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x2255ba in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20943e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x208466 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x208272 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^error: the following test command crashed:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

You can activate another field by assigning the entire union:

test.zig

const std =@import("std");const expect = std.testing.expect;const Payload =union {    int:i64,    float:f64,    boolean:bool,};test"simple union" {var payload = Payload{ .int =1234 };try expect(payload.int ==1234);    payload = Payload{ .float =12.34 };try expect(payload.float ==12.34);}
$ zig test test.zigTest [1/1] test "simple union"... All 1 tests passed.

In order to useswitch with a union, it must be aTagged union.

To initialize a union when the tag is acomptime-known name, see@unionInit.

Tagged union§

Unions can be declared with an enum tag type. This turns the union into atagged union, which makes it eligible to use withswitch expressions. Tagged unions coerce to their tag type:Type Coercion: unions and enums.

test.zig

const std =@import("std");const expect = std.testing.expect;const ComplexTypeTag =enum {    ok,    not_ok,};const ComplexType =union(ComplexTypeTag) {    ok:u8,    not_ok:void,};test"switch on tagged union" {const c = ComplexType{ .ok =42 };try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);switch (c) {        ComplexTypeTag.ok => |value|try expect(value ==42),        ComplexTypeTag.not_ok =>unreachable,    }}test"get tag type" {try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);}test"coerce to enum" {const c1 = ComplexType{ .ok =42 };const c2 = ComplexType.not_ok;try expect(c1 == .ok);try expect(c2 == .not_ok);}
$ zig test test.zigTest [1/3] test "switch on tagged union"... Test [2/3] test "get tag type"... Test [3/3] test "coerce to enum"... All 3 tests passed.

In order to modify the payload of a tagged union in a switch expression, place a* before the variable name to make it a pointer:

test.zig

const std =@import("std");const expect = std.testing.expect;const ComplexTypeTag =enum {    ok,    not_ok,};const ComplexType =union(ComplexTypeTag) {    ok:u8,    not_ok:void,};test"modify tagged union in switch" {var c = ComplexType{ .ok =42 };try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);switch (c) {        ComplexTypeTag.ok => |*value| value.* +=1,        ComplexTypeTag.not_ok =>unreachable,    }try expect(c.ok ==43);}
$ zig test test.zigTest [1/1] test "modify tagged union in switch"... All 1 tests passed.

Unions can be made to infer the enum tag type. Further, unions can have methods just like structs and enums.

test.zig

const std =@import("std");const expect = std.testing.expect;const Variant =union(enum) {    int:i32,    boolean:bool,// void can be omitted when inferring enum tag type.    none,fntruthy(self: Variant)bool {returnswitch (self) {            Variant.int => |x_int| x_int !=0,            Variant.boolean => |x_bool| x_bool,            Variant.none =>false,        };    }};test"union method" {var v1 = Variant{ .int =1 };var v2 = Variant{ .boolean =false };try expect(v1.truthy());try expect(!v2.truthy());}
$ zig test test.zigTest [1/1] test "union method"... All 1 tests passed.

@tagName can be used to return acomptime[]constu8 value representing the field name:

test.zig

const std =@import("std");const expect = std.testing.expect;const Small2 =union(enum) {    a:i32,    b:bool,    c:u8,};test"@tagName" {try expect(std.mem.eql(u8,@tagName(Small2.a),"a"));}
$ zig test test.zigTest [1/1] test "@tagName"... All 1 tests passed.

extern union§

Anexternunion has memory layout guaranteed to be compatible with the target C ABI.

See also:

packed union§

Apackedunion has well-defined in-memory layout and is eligible to be in apacked struct.

Anonymous Union Literals§

Anonymous Struct Literals syntax can be used to initialize unions without specifying the type:

anon_union.zig

const std =@import("std");const expect = std.testing.expect;const Number =union {    int:i32,    float:f64,};test"anonymous union literal syntax" {var i: Number = .{.int =42};var f = makeNumber();try expect(i.int ==42);try expect(f.float ==12.34);}fnmakeNumber() Number {return .{.float =12.34};}
$ zig test anon_union.zigTest [1/1] test "anonymous union literal syntax"... All 1 tests passed.

opaque§

opaque {} declares a new type with an unknown (but non-zero) size and alignment. It can contain declarations the same asstructs,unions, andenums.

This is typically used for type safety when interacting with C code that does not expose struct details. Example:

test.zig

const Derp =opaque {};const Wat =opaque {};externfnbar(d: *Derp)void;fnfoo(w: *Wat)callconv(.C)void {    bar(w);}test"call foo" {    foo(undefined);}
$ zig test test.zig./docgen_tmp/test.zig:6:9:error:expected type '*Derp', found '*Wat'    bar(w);^./docgen_tmp/test.zig:6:9:note:pointer type child 'Wat' cannot cast into pointer type child 'Derp'    bar(w);^

blocks§

Blocks are used to limit the scope of variable declarations:

test.zig

test"access variable after block scope" {    {var x:i32 =1;    }    x +=1;}
$ zig test test.zig./docgen_tmp/test.zig:5:5:error:use of undeclared identifier 'x'    x += 1;^

Blocks are expressions. When labeled,break can be used to return a value from the block:

test.zig

const std =@import("std");const expect = std.testing.expect;test"labeled break from labeled block expression" {var y:i32 =123;const x = blk: {        y +=1;break :blk y;    };try expect(x ==124);try expect(y ==124);}
$ zig test test.zigTest [1/1] test "labeled break from labeled block expression"... All 1 tests passed.

Here,blk can be any name.

See also:

Shadowing§

It is never allowed for an identifier to "hide" another one by using the same name:

test.zig

const pi =3.14;test"inside test block" {// Let's even go inside another block    {var pi:i32 =1234;    }}
$ zig test test.zig./docgen_tmp/test.zig:6:9:error:redefinition of 'pi'        var pi: i32 = 1234;^./docgen_tmp/test.zig:1:1:note:previous definition is hereconst pi = 3.14;^

Because of this, when you read Zig code you can rely on an identifier always meaning the same thing, within the scope it is defined. Note that you can, however use the same name if the scopes are separate:

test.zig

test"separate scopes" {    {const pi =3.14;    }    {var pi:bool =true;    }}
$ zig test test.zigTest [1/1] test "separate scopes"... All 1 tests passed.

switch§

switch.zig

const std =@import("std");const expect = std.testing.expect;test"switch simple" {const a:u64 =10;const zz:u64 =103;// All branches of a switch expression must be able to be coerced to a// common type.//// Branches cannot fallthrough. If fallthrough behavior is desired, combine// the cases and use an if.const b =switch (a) {// Multiple cases can be combined via a ','1,2,3 =>0,// Ranges can be specified using the ... syntax. These are inclusive// both ends.5...100 =>1,// Branches can be arbitrarily complex.101 => blk: {const c:u64 =5;break :blk c *2 +1;        },// Switching on arbitrary expressions is allowed as long as the// expression is known at compile-time.        zz => zz,comptime blk: {const d:u32 =5;const e:u32 =100;break :blk d + e;        } =>107,// The else branch catches everything not already captured.// Else branches are mandatory unless the entire range of values// is handled.else =>9,    };try expect(b ==1);}// Switch expressions can be used outside a function:const os_msg =switch (std.Target.current.os.tag) {    .linux =>"we found a linux user",else =>"not a linux user",};// Inside a function, switch statements implicitly are compile-time// evaluated if the target expression is compile-time known.test"switch inside function" {switch (std.Target.current.os.tag) {        .fuchsia => {// On an OS other than fuchsia, block is not even analyzed,// so this compile error is not triggered.// On fuchsia this compile error would be triggered.@compileError("fuchsia not supported");        },else => {},    }}
$ zig test switch.zigTest [1/2] test "switch simple"... Test [2/2] test "switch inside function"... All 2 tests passed.

switch can be used to capture the field values of aTagged union. Modifications to the field values can be done by placing a* before the capture variable name, turning it into a pointer.

test.zig

const expect =@import("std").testing.expect;test"switch on tagged union" {const Point =struct {        x:u8,        y:u8,    };const Item =union(enum) {        a:u32,        c: Point,        d,        e:u32,    };var a = Item{ .c = Point{ .x =1, .y =2 } };// Switching on more complex enums is allowed.const b =switch (a) {// A capture group is allowed on a match, and will return the enum// value matched. If the payload types of both cases are the same// they can be put into the same switch prong.        Item.a, Item.e => |item| item,// A reference to the matched value can be obtained using `*` syntax.        Item.c => |*item| blk: {            item.*.x +=1;break :blk6;        },// No else is required if the types cases was exhaustively handled        Item.d =>8,    };try expect(b ==6);try expect(a.c.x ==2);}
$ zig test test.zigTest [1/1] test "switch on tagged union"... All 1 tests passed.

See also:

Exhaustive Switching§

When aswitch expression does not have anelse clause, it must exhaustively list all the possible values. Failure to do so is a compile error:

test.zig

const Color =enum {    auto,    off,    on,};test"exhaustive switching" {const color = Color.off;switch (color) {        Color.auto => {},        Color.on => {},    }}
$ zig test test.zig./docgen_tmp/test.zig:9:5:error:enumeration value 'Color.off' not handled in switch    switch (color) {^./docgen_tmp/test.zig:7:29:note:referenced heretest "exhaustive switching" {^

Switching with Enum Literals§

Enum Literals can be useful to use withswitch to avoid repetitively specifyingenum orunion types:

test.zig

const std =@import("std");const expect = std.testing.expect;const Color =enum {    auto,    off,    on,};test"enum literals with switch" {const color = Color.off;const result =switch (color) {        .auto =>false,        .on =>false,        .off =>true,    };try expect(result);}
$ zig test test.zigTest [1/1] test "enum literals with switch"... All 1 tests passed.

while§

A while loop is used to repeatedly execute an expression until some condition is no longer true.

while.zig

const expect =@import("std").testing.expect;test"while basic" {var i:usize =0;while (i <10) {        i +=1;    }try expect(i ==10);}
$ zig test while.zigTest [1/1] test "while basic"... All 1 tests passed.

Usebreak to exit a while loop early.

while.zig

const expect =@import("std").testing.expect;test"while break" {var i:usize =0;while (true) {if (i ==10)break;        i +=1;    }try expect(i ==10);}
$ zig test while.zigTest [1/1] test "while break"... All 1 tests passed.

Usecontinue to jump back to the beginning of the loop.

while.zig

const expect =@import("std").testing.expect;test"while continue" {var i:usize =0;while (true) {        i +=1;if (i <10)continue;break;    }try expect(i ==10);}
$ zig test while.zigTest [1/1] test "while continue"... All 1 tests passed.

While loops support a continue expression which is executed when the loop is continued. Thecontinue keyword respects this expression.

while.zig

const expect =@import("std").testing.expect;test"while loop continue expression" {var i:usize =0;while (i <10) : (i +=1) {}try expect(i ==10);}test"while loop continue expression, more complicated" {var i:usize =1;var j:usize =1;while (i * j <2000) : ({ i *=2; j *=3; }) {const my_ij = i * j;try expect(my_ij <2000);    }}
$ zig test while.zigTest [1/2] test "while loop continue expression"... Test [2/2] test "while loop continue expression, more complicated"... All 2 tests passed.

While loops are expressions. The result of the expression is the result of theelse clause of a while loop, which is executed when the condition of the while loop is tested as false.

break, likereturn, accepts a value parameter. This is the result of thewhile expression. When youbreak from a while loop, theelse branch is not evaluated.

while.zig

const expect =@import("std").testing.expect;test"while else" {try expect(rangeHasNumber(0,10,5));try expect(!rangeHasNumber(0,10,15));}fnrangeHasNumber(begin:usize, end:usize, number:usize)bool {var i = begin;returnwhile (i < end) : (i +=1) {if (i == number) {breaktrue;        }    }elsefalse;}
$ zig test while.zigTest [1/1] test "while else"... All 1 tests passed.

Labeled while§

When awhile loop is labeled, it can be referenced from abreak orcontinue from within a nested loop:

test.zig

test"nested break" {    outer:while (true) {while (true) {break :outer;        }    }}test"nested continue" {var i:usize =0;    outer:while (i <10) : (i +=1) {while (true) {continue :outer;        }    }}
$ zig test test.zigTest [1/2] test "nested break"... Test [2/2] test "nested continue"... All 2 tests passed.

while with Optionals§

Just likeif expressions, while loops can take an optional as the condition and capture the payload. Whennull is encountered the loop exits.

When the|x| syntax is present on awhile expression, the while condition must have anOptional Type.

Theelse branch is allowed on optional iteration. In this case, it will be executed on the first null value encountered.

while.zig

const expect =@import("std").testing.expect;test"while null capture" {var sum1:u32 =0;    numbers_left =3;while (eventuallyNullSequence()) |value| {        sum1 += value;    }try expect(sum1 ==3);var sum2:u32 =0;    numbers_left =3;while (eventuallyNullSequence()) |value| {        sum2 += value;    }else {try expect(sum2 ==3);    }}var numbers_left:u32 =undefined;fneventuallyNullSequence() ?u32 {returnif (numbers_left ==0)nullelse blk: {        numbers_left -=1;break :blk numbers_left;    };}
$ zig test while.zigTest [1/1] test "while null capture"... All 1 tests passed.

while with Error Unions§

Just likeif expressions, while loops can take an error union as the condition and capture the payload or the error code. When the condition results in an error code the else branch is evaluated and the loop is finished.

When theelse |x| syntax is present on awhile expression, the while condition must have anError Union Type.

while.zig

const expect =@import("std").testing.expect;test"while error union capture" {var sum1:u32 =0;    numbers_left =3;while (eventuallyErrorSequence()) |value| {        sum1 += value;    }else |err| {try expect(err ==error.ReachedZero);    }}var numbers_left:u32 =undefined;fneventuallyErrorSequence()anyerror!u32 {returnif (numbers_left ==0)error.ReachedZeroelse blk: {        numbers_left -=1;break :blk numbers_left;    };}
$ zig test while.zigTest [1/1] test "while error union capture"... All 1 tests passed.

inline while§

While loops can be inlined. This causes the loop to be unrolled, which allows the code to do some things which only work at compile time, such as use types as first class values.

test.zig

const expect =@import("std").testing.expect;test"inline while loop" {comptimevar i =0;var sum:usize =0;inlinewhile (i <3) : (i +=1) {const T =switch (i) {0 =>f32,1 =>i8,2 =>bool,else =>unreachable,        };        sum += typeNameLength(T);    }try expect(sum ==9);}fntypeNameLength(comptime T:type)usize {return@typeName(T).len;}
$ zig test test.zigTest [1/1] test "inline while loop"... All 1 tests passed.

It is recommended to useinline loops only for one of these reasons:

  • You need the loop to execute atcomptime for the semantics to work.
  • You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.

See also:

for§

for.zig

const expect =@import("std").testing.expect;test"for basics" {const items = [_]i32 {4,5,3,4,0 };var sum:i32 =0;// For loops iterate over slices and arrays.for (items) |value| {// Break and continue are supported.if (value ==0) {continue;        }        sum += value;    }try expect(sum ==16);// To iterate over a portion of a slice, reslice.for (items[0..1]) |value| {        sum += value;    }try expect(sum ==20);// To access the index of iteration, specify a second capture value.// This is zero-indexed.var sum2:i32 =0;for (items) |value, i| {try expect(@TypeOf(i) ==usize);        sum2 +=@intCast(i32, i);    }try expect(sum2 ==10);}test"for reference" {var items = [_]i32 {3,4,2 };// Iterate over the slice by reference by// specifying that the capture value is a pointer.for (items) |*value| {        value.* +=1;    }try expect(items[0] ==4);try expect(items[1] ==5);try expect(items[2] ==3);}test"for else" {// For allows an else attached to it, the same as a while loop.var items = [_]?i32 {3,4,null,5 };// For loops can also be used as expressions.// Similar to while loops, when you break from a for loop, the else branch is not evaluated.var sum:i32 =0;const result =for (items) |value| {if (value !=null) {            sum += value.?;        }    }else blk: {try expect(sum ==12);break :blk sum;    };try expect(result ==12);}
$ zig test for.zigTest [1/3] test "for basics"... Test [2/3] test "for reference"... Test [3/3] test "for else"... All 3 tests passed.

Labeled for§

When afor loop is labeled, it can be referenced from abreak orcontinue from within a nested loop:

test.zig

const std =@import("std");const expect = std.testing.expect;test"nested break" {var count:usize =0;    outer:for ([_]i32{1,2,3,4,5 }) |_| {for ([_]i32{1,2,3,4,5 }) |_| {            count +=1;break :outer;        }    }try expect(count ==1);}test"nested continue" {var count:usize =0;    outer:for ([_]i32{1,2,3,4,5,6,7,8 }) |_| {for ([_]i32{1,2,3,4,5 }) |_| {            count +=1;continue :outer;        }    }try expect(count ==8);}
$ zig test test.zigTest [1/2] test "nested break"... Test [2/2] test "nested continue"... All 2 tests passed.

inline for§

For loops can be inlined. This causes the loop to be unrolled, which allows the code to do some things which only work at compile time, such as use types as first class values. The capture value and iterator value of inlined for loops are compile-time known.

test.zig

const expect =@import("std").testing.expect;test"inline for loop" {const nums = [_]i32{2,4,6};var sum:usize =0;inlinefor (nums) |i| {const T =switch (i) {2 =>f32,4 =>i8,6 =>bool,else =>unreachable,        };        sum += typeNameLength(T);    }try expect(sum ==9);}fntypeNameLength(comptime T:type)usize {return@typeName(T).len;}
$ zig test test.zigTest [1/1] test "inline for loop"... All 1 tests passed.

It is recommended to useinline loops only for one of these reasons:

  • You need the loop to execute atcomptime for the semantics to work.
  • You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.

See also:

if§

if.zig

// If expressions have three uses, corresponding to the three types:// * bool// * ?T// * anyerror!Tconst expect =@import("std").testing.expect;test"if expression" {// If expressions are used instead of a ternary expression.const a:u32 =5;const b:u32 =4;const result =if (a != b)47else3089;try expect(result ==47);}test"if boolean" {// If expressions test boolean conditions.const a:u32 =5;const b:u32 =4;if (a != b) {try expect(true);    }elseif (a ==9) {unreachable;    }else {unreachable;    }}test"if optional" {// If expressions test for null.const a: ?u32 =0;if (a) |value| {try expect(value ==0);    }else {unreachable;    }const b: ?u32 =null;if (b) |value| {unreachable;    }else {try expect(true);    }// The else is not required.if (a) |value| {try expect(value ==0);    }// To test against null only, use the binary equality operator.if (b ==null) {try expect(true);    }// Access the value by reference using a pointer capture.var c: ?u32 =3;if (c) |*value| {        value.* =2;    }if (c) |value| {try expect(value ==2);    }else {unreachable;    }}test"if error union" {// If expressions test for errors.// Note the |err| capture on the else.const a:anyerror!u32 =0;if (a) |value| {try expect(value ==0);    }else |err| {unreachable;    }const b:anyerror!u32 =error.BadValue;if (b) |value| {unreachable;    }else |err| {try expect(err ==error.BadValue);    }// The else and |err| capture is strictly required.if (a) |value| {try expect(value ==0);    }else |_| {}// To check only the error value, use an empty block expression.if (b) |_| {}else |err| {try expect(err ==error.BadValue);    }// Access the value by reference using a pointer capture.var c:anyerror!u32 =3;if (c) |*value| {        value.* =9;    }else |err| {unreachable;    }if (c) |value| {try expect(value ==9);    }else |err| {unreachable;    }}test"if error union with optional" {// If expressions test for errors before unwrapping optionals.// The |optional_value| capture's type is ?u32.const a:anyerror!?u32 =0;if (a) |optional_value| {try expect(optional_value.? ==0);    }else |err| {unreachable;    }const b:anyerror!?u32 =null;if (b) |optional_value| {try expect(optional_value ==null);    }else |err| {unreachable;    }const c:anyerror!?u32 =error.BadValue;if (c) |optional_value| {unreachable;    }else |err| {try expect(err ==error.BadValue);    }// Access the value by reference by using a pointer capture each time.var d:anyerror!?u32 =3;if (d) |*optional_value| {if (optional_value.*) |*value| {            value.* =9;        }    }else |err| {unreachable;    }if (d) |optional_value| {try expect(optional_value.? ==9);    }else |err| {unreachable;    }}
$ zig test if.zigTest [1/5] test "if expression"... Test [2/5] test "if boolean"... Test [3/5] test "if optional"... Test [4/5] test "if error union"... Test [5/5] test "if error union with optional"... All 5 tests passed.

See also:

defer§

defer.zig

const std =@import("std");const expect = std.testing.expect;const print = std.debug.print;// defer will execute an expression at the end of the current scope.fndeferExample() !usize {var a:usize =1;    {defer a =2;        a =1;    }try expect(a ==2);    a =5;return a;}test"defer basics" {try expect((try deferExample()) ==5);}// If multiple defer statements are specified, they will be executed in// the reverse order they were run.fndeferUnwindExample()void {    print("\n", .{});defer {        print("1 ", .{});    }defer {        print("2 ", .{});    }if (false) {// defers are not run if they are never executed.defer {            print("3 ", .{});        }    }}test"defer unwinding" {    deferUnwindExample();}// The errdefer keyword is similar to defer, but will only execute if the// scope returns with an error.//// This is especially useful in allowing a function to clean up properly// on error, and replaces goto error handling tactics as seen in c.fndeferErrorExample(is_error:bool) !void {    print("\nstart of function\n", .{});// This will always be executed on exitdefer {        print("end of function\n", .{});    }errdefer {        print("encountered an error!\n", .{});    }if (is_error) {returnerror.DeferError;    }}test"errdefer unwinding" {    deferErrorExample(false)catch {};    deferErrorExample(true)catch {};}
$ zig test defer.zigTest [1/3] test "defer basics"... Test [2/3] test "defer unwinding"... 2 1 Test [3/3] test "errdefer unwinding"... start of functionend of functionstart of functionencountered an error!end of functionAll 3 tests passed.

See also:

unreachable§

InDebug andReleaseSafe mode, and when usingzig test,unreachable emits a call topanic with the messagereached unreachable code.

InReleaseFast mode, the optimizer uses the assumption thatunreachable code will never be hit to perform optimizations. However,zig test even inReleaseFast mode still emitsunreachable as calls topanic.

Basics§

test.zig

// unreachable is used to assert that control flow will never happen upon a// particular location:test"basic math" {const x =1;const y =2;if (x + y !=3) {unreachable;    }}
$ zig test test.zigTest [1/1] test "basic math"... All 1 tests passed.

In fact, this is howstd.debug.assert is implemented:

test.zig

// This is how std.debug.assert is implementedfnassert(ok:bool)void {if (!ok)unreachable;// assertion failure}// This test will fail because we hit unreachable.test"this will fail" {    assert(false);}
$ zig test test.zigTest [1/1] test "this will fail"... thread 12592 panic: reached unreachable code/home/andy/Downloads/zig/docgen_tmp/test.zig:3:14:0x207feb in assert (test)    if (!ok) unreachable; // assertion failure^/home/andy/Downloads/zig/docgen_tmp/test.zig:8:11:0x2066de in test "this will fail" (test)    assert(false);^/home/andy/Downloads/zig/lib/std/special/test_runner.zig:76:28:0x22d00a in std.special.main (test)        } else test_fn.func();^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x22556a in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2093ee in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x208416 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x208222 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^error: the following test command crashed:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

At Compile-Time§

test.zig

const assert =@import("std").debug.assert;test"type of unreachable" {comptime {// The type of unreachable is noreturn.// However this assertion will still fail because// evaluating unreachable at compile-time is a compile error.        assert(@TypeOf(unreachable) ==noreturn);    }}
$ zig test test.zig./docgen_tmp/test.zig:10:16:error:unreachable code        assert(@TypeOf(unreachable) == noreturn);^./docgen_tmp/test.zig:3:28:note:referenced heretest "type of unreachable" {^

See also:

noreturn§

noreturn is the type of:

  • break
  • continue
  • return
  • unreachable
  • while (true) {}

When resolving types together, such asif clauses orswitch prongs, thenoreturn type is compatible with every other type. Consider:

test.zig

fnfoo(condition:bool, b:u32)void {const a =if (condition) belsereturn;@panic("do something with a");}test"noreturn" {    foo(false,1);}
$ zig test test.zigTest [1/1] test "noreturn"... All 1 tests passed.

Another use case fornoreturn is theexit function:

test.zig

pubextern"kernel32"fnExitProcess(exit_code:c_uint)callconv(if (@import("builtin").target.cpu.arch == .i386) .Stdcallelse .C)noreturn;test"foo" {const value = bar()catch ExitProcess(1);try expect(value ==1234);}fnbar()anyerror!u32 {return1234;}const expect =@import("std").testing.expect;
$ zig test test.zig -target x86_64-windowswarning: created docgen_tmp/zig-cache/o/c42fd17f6f3b779d958fa42345e9399d/test.exe but skipping execution because it is non-native

Functions§

functions.zig

const expect =@import("std").testing.expect;// Functions are declared like thisfnadd(a:i8, b:i8)i8 {if (a ==0) {return b;    }return a + b;}// The export specifier makes a function externally visible in the generated// object file, and makes it use the C ABI.exportfnsub(a:i8, b:i8)i8 {return a - b; }// The extern specifier is used to declare a function that will be resolved// at link time, when linking statically, or at runtime, when linking// dynamically.// The callconv specifier changes the calling convention of the function.extern"kernel32"fnExitProcess(exit_code:u32)callconv(if (@import("builtin").target.cpu.arch == .i386) .Stdcallelse .C)noreturn;extern"c"fnatan2(a:f64, b:f64)f64;// The @setCold builtin tells the optimizer that a function is rarely called.fnabort()noreturn {@setCold(true);while (true) {}}// The naked calling convention makes a function not have any function prologue or epilogue.// This can be useful when integrating with assembly.fn_start()callconv(.Naked)noreturn {    abort();}// The inline calling convention forces a function to be inlined at all call sites.// If the function cannot be inlined, it is a compile-time error.fnshiftLeftOne(a:u32)callconv(.Inline)u32 {return a <<1;}// The pub specifier allows the function to be visible when importing.// Another file can use @import and call sub2pubfnsub2(a:i8, b:i8)i8 {return a - b; }// Functions can be used as values and are equivalent to pointers.const call2_op =fn (a:i8, b:i8)i8;fndo_op(fn_call: call2_op, op1:i8, op2:i8)i8 {return fn_call(op1, op2);}test"function" {try expect(do_op(add,5,6) ==11);try expect(do_op(sub2,5,6) == -1);}
$ zig test functions.zigTest [1/1] test "function"... All 1 tests passed.

Function values are like pointers:

test.zig

const assert =@import("std").debug.assert;comptime {    assert(@TypeOf(foo) ==fn()void);    assert(@sizeOf(fn()void) ==@sizeOf(?fn()void));}fnfoo()void { }
$ zig build-obj test.zig

Pass-by-value Parameters§

Primitive types such asIntegers andFloats passed as parameters are copied, and then the copy is available in the function body. This is called "passing by value". Copying a primitive type is essentially free and typically involves nothing more than setting a register.

Structs, unions, and arrays can sometimes be more efficiently passed as a reference, since a copy could be arbitrarily expensive depending on the size. When these types are passed as parameters, Zig may choose to copy and pass by value, or pass by reference, whichever way Zig decides will be faster. This is made possible, in part, by the fact that parameters are immutable.

test.zig

const Point =struct {    x:i32,    y:i32,};fnfoo(point: Point)i32 {// Here, `point` could be a reference, or a copy. The function body// can ignore the difference and treat it as a value. Be very careful// taking the address of the parameter - it should be treated as if// the address will become invalid when the function returns.return point.x + point.y;}const expect =@import("std").testing.expect;test"pass struct to function" {try expect(foo(Point{ .x =1, .y =2 }) ==3);}
$ zig test test.zigTest [1/1] test "pass struct to function"... All 1 tests passed.

For extern functions, Zig follows the C ABI for passing structs and unions by value.

Function Parameter Type Inference§

Function parameters can be declared withanytype in place of the type. In this case the parameter types will be inferred when the function is called. Use@TypeOf and@typeInfo to get information about the inferred type.

test.zig

const expect =@import("std").testing.expect;fnaddFortyTwo(x:anytype)@TypeOf(x) {return x +42;}test"fn type inference" {try expect(addFortyTwo(1) ==43);try expect(@TypeOf(addFortyTwo(1)) ==comptime_int);var y:i64 =2;try expect(addFortyTwo(y) ==44);try expect(@TypeOf(addFortyTwo(y)) ==i64);}
$ zig test test.zigTest [1/1] test "fn type inference"... All 1 tests passed.

Function Reflection§

test.zig

const expect =@import("std").testing.expect;test"fn reflection" {try expect(@typeInfo(@TypeOf(expect)).Fn.args[0].arg_type.? ==bool);try expect(@typeInfo(@TypeOf(expect)).Fn.is_var_args ==false);}
$ zig test test.zigTest [1/1] test "fn reflection"... All 1 tests passed.

Errors§

Error Set Type§

An error set is like anenum. However, each error name across the entire compilation gets assigned an unsigned integer greater than 0. You are allowed to declare the same error name more than once, and if you do, it gets assigned the same integer value.

The number of unique error values across the entire compilation should determine the size of the error set type. However right now it is hard coded to be au16. See#768.

You cancoerce an error from a subset to a superset:

test.zig

const std =@import("std");const FileOpenError =error {    AccessDenied,    OutOfMemory,    FileNotFound,};const AllocationError =error {    OutOfMemory,};test"coerce subset to superset" {const err = foo(AllocationError.OutOfMemory);try std.testing.expect(err == FileOpenError.OutOfMemory);}fnfoo(err: AllocationError) FileOpenError {return err;}
$ zig test test.zigTest [1/1] test "coerce subset to superset"... All 1 tests passed.

But you cannotcoerce an error from a superset to a subset:

test.zig

const FileOpenError =error {    AccessDenied,    OutOfMemory,    FileNotFound,};const AllocationError =error {    OutOfMemory,};test"coerce superset to subset" {    foo(FileOpenError.OutOfMemory)catch {};}fnfoo(err: FileOpenError) AllocationError {return err;}
$ zig test test.zig./docgen_tmp/test.zig:16:12:error:expected type 'AllocationError', found 'FileOpenError'    return err;^./docgen_tmp/test.zig:2:5:note:'error.AccessDenied' not a member of destination error set    AccessDenied,^./docgen_tmp/test.zig:4:5:note:'error.FileNotFound' not a member of destination error set    FileNotFound,^

There is a shortcut for declaring an error set with only 1 value, and then getting that value:

const err =error.FileNotFound;

This is equivalent to:

const err = (error {FileNotFound}).FileNotFound;

This becomes useful when usingInferred Error Sets.

The Global Error Set§

anyerror refers to the global error set. This is the error set that contains all errors in the entire compilation unit. It is a superset of all other error sets and a subset of none of them.

You cancoerce any error set to the global one, and you can explicitly cast an error of the global error set to a non-global one. This inserts a language-level assert to make sure the error value is in fact in the destination error set.

The global error set should generally be avoided because it prevents the compiler from knowing what errors are possible at compile-time. Knowing the error set at compile-time is better for generated documentation and helpful error messages, such as forgetting a possible error value in aswitch.

Error Union Type§

An error set type and normal type can be combined with the! binary operator to form an error union type. You are likely to use an error union type more often than an error set type by itself.

Here is a function to parse a string into a 64-bit integer:

test.zig

const std =@import("std");const maxInt = std.math.maxInt;pubfnparseU64(buf: []constu8, radix:u8) !u64 {var x:u64 =0;for (buf) |c| {const digit = charToDigit(c);if (digit >= radix) {returnerror.InvalidChar;        }// x *= radixif (@mulWithOverflow(u64, x, radix, &x)) {returnerror.Overflow;        }// x += digitif (@addWithOverflow(u64, x, digit, &x)) {returnerror.Overflow;        }    }return x;}fncharToDigit(c:u8)u8 {returnswitch (c) {'0' ...'9' => c -'0','A' ...'Z' => c -'A' +10,'a' ...'z' => c -'a' +10,else => maxInt(u8),    };}test"parse u64" {const result =try parseU64("1234",10);try std.testing.expect(result ==1234);}
$ zig test test.zigTest [1/1] test "parse u64"... All 1 tests passed.

Notice the return type is!u64. This means that the function either returns an unsigned 64 bit integer, or an error. We left off the error set to the left of the!, so the error set is inferred.

Within the function definition, you can see some return statements that return an error, and at the bottom a return statement that returns au64. Both typescoerce toanyerror!u64.

What it looks like to use this function varies depending on what you're trying to do. One of the following:

  • You want to provide a default value if it returned an error.
  • If it returned an error then you want to return the same error.
  • You know with complete certainty it will not return an error, so want to unconditionally unwrap it.
  • You want to take a different action for each possible error.

catch§

If you want to provide a default value, you can use thecatch binary operator:

fndoAThing(str: []u8)void {const number = parseU64(str,10)catch13;// ...}

In this code,number will be equal to the successfully parsed string, or a default value of 13. The type of the right hand side of the binarycatch operator must match the unwrapped error union type, or be of typenoreturn.

try§

Let's say you wanted to return the error if you got one, otherwise continue with the function logic:

fndoAThing(str: []u8) !void {const number = parseU64(str,10)catch |err|return err;// ...}

There is a shortcut for this. Thetry expression:

fndoAThing(str: []u8) !void {const number =try parseU64(str,10);// ...}

try evaluates an error union expression. If it is an error, it returns from the current function with the same error. Otherwise, the expression results in the unwrapped value.

Maybe you know with complete certainty that an expression will never be an error. In this case you can do this:

const number = parseU64("1234",10)catchunreachable;

Here we know for sure that "1234" will parse successfully. So we put theunreachable value on the right hand side.unreachable generates a panic in Debug and ReleaseSafe modes and undefined behavior in ReleaseFast mode. So, while we're debugging the application, if therewas a surprise error here, the application would crash appropriately.

Finally, you may want to take a different action for every situation. For that, we combine theif andswitch expression:

fndoAThing(str: []u8)void {if (parseU64(str,10)) |number| {        doSomethingWithNumber(number);    }else |err|switch (err) {error.Overflow => {// handle overflow...        },// we promise that InvalidChar won't happen (or crash in debug mode if it does)error.InvalidChar =>unreachable,    }}

errdefer§

The other component to error handling is defer statements. In addition to an unconditionaldefer, Zig haserrdefer, which evaluates the deferred expression on block exit path if and only if the function returned with an error from the block.

Example:

fncreateFoo(param:i32) !Foo {const foo =try tryToAllocateFoo();// now we have allocated foo. we need to free it if the function fails.// but we want to return it if the function succeeds.errdefer deallocateFoo(foo);const tmp_buf = allocateTmpBuffer()orelsereturnerror.OutOfMemory;// tmp_buf is truly a temporary resource, and we for sure want to clean it up// before this block leaves scopedefer deallocateTmpBuffer(tmp_buf);if (param >1337)returnerror.InvalidParam;// here the errdefer will not run since we're returning success from the function.// but the defer will run!return foo;}

The neat thing about this is that you get robust error handling without the verbosity and cognitive overhead of trying to make sure every exit path is covered. The deallocation code is always directly following the allocation code.

A couple of other tidbits about error handling:

  • These primitives give enough expressiveness that it's completely practical to have failing to check for an error be a compile error. If you really want to ignore the error, you can addcatchunreachable and get the added benefit of crashing in Debug and ReleaseSafe modes if your assumption was wrong.
  • Since Zig understands error types, it can pre-weight branches in favor of errors not occurring. Just a small optimization benefit that is not available in other languages.

See also:

An error union is created with the! binary operator. You can use compile-time reflection to access the child type of an error union:

test.zig

const expect =@import("std").testing.expect;test"error union" {var foo:anyerror!i32 =undefined;// Coerce from child type of an error union:    foo =1234;// Coerce from an error set:    foo =error.SomeError;// Use compile-time reflection to access the payload type of an error union:comptimetry expect(@typeInfo(@TypeOf(foo)).ErrorUnion.payload ==i32);// Use compile-time reflection to access the error set type of an error union:comptimetry expect(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set ==anyerror);}
$ zig test test.zigTest [1/1] test "error union"... All 1 tests passed.

Merging Error Sets§

Use the|| operator to merge two error sets together. The resulting error set contains the errors of both error sets. Doc comments from the left-hand side override doc comments from the right-hand side. In this example, the doc comments forC.PathNotFound isA doc comment.

This is especially useful for functions which return different error sets depending oncomptime branches. For example, the Zig standard library usesLinuxFileOpenError || WindowsFileOpenError for the error set of opening files.

test.zig

const A =error{    NotDir,/// A doc comment    PathNotFound,};const B =error{    OutOfMemory,/// B doc comment    PathNotFound,};const C = A || B;fnfoo() C!void {returnerror.NotDir;}test"merge error sets" {if (foo()) {@panic("unexpected");    }else |err|switch (err) {error.OutOfMemory =>@panic("unexpected"),error.PathNotFound =>@panic("unexpected"),error.NotDir => {},    }}
$ zig test test.zigTest [1/1] test "merge error sets"... All 1 tests passed.

Inferred Error Sets§

Because many functions in Zig return a possible error, Zig supports inferring the error set. To infer the error set for a function, use this syntax:

test.zig

// With an inferred error setpubfnadd_inferred(comptime T:type, a: T, b: T) !T {var answer: T =undefined;returnif (@addWithOverflow(T, a, b, &answer))error.Overflowelse answer;}// With an explicit error setpubfnadd_explicit(comptime T:type, a: T, b: T) Error!T {var answer: T =undefined;returnif (@addWithOverflow(T, a, b, &answer))error.Overflowelse answer;}const Error =error {    Overflow,};const std =@import("std");test"inferred error set" {if (add_inferred(u8,255,1)) |_|unreachableelse |err|switch (err) {error.Overflow => {},// ok    }}
$ zig test test.zigTest [1/1] test "inferred error set"... All 1 tests passed.

When a function has an inferred error set, that function becomes generic and thus it becomes trickier to do certain things with it, such as obtain a function pointer, or have an error set that is consistent across different build targets. Additionally, inferred error sets are incompatible with recursion.

In these situations, it is recommended to use an explicit error set. You can generally start with an empty error set and let compile errors guide you toward completing the set.

These limitations may be overcome in a future version of Zig.

Error Return Traces§

Error Return Traces show all the points in the code that an error was returned to the calling function. This makes it practical to usetry everywhere and then still be able to know what happened if an error ends up bubbling all the way out of your application.

test.zig

pubfnmain() !void {try foo(12);}fnfoo(x:i32) !void {if (x >=5) {try bar();    }else {try bang2();    }}fnbar() !void {if (baz()) {try quux();    }else |err|switch (err) {error.FileNotFound =>try hello(),else =>try another(),    }}fnbaz() !void {try bang1();}fnquux() !void {try bang2();}fnhello() !void {try bang2();}fnanother() !void {try bang1();}fnbang1() !void {returnerror.FileNotFound;}fnbang2() !void {returnerror.PermissionDenied;}
$ zig build-exe test.zig$ ./testerror: PermissionDenied/home/andy/Downloads/zig/docgen_tmp/test.zig:39:5:0x231632 in bang1 (test)    return error.FileNotFound;^/home/andy/Downloads/zig/docgen_tmp/test.zig:23:5:0x23150f in baz (test)    try bang1();^/home/andy/Downloads/zig/docgen_tmp/test.zig:43:5:0x2314d2 in bang2 (test)    return error.PermissionDenied;^/home/andy/Downloads/zig/docgen_tmp/test.zig:31:5:0x2315ff in hello (test)    try bang2();^/home/andy/Downloads/zig/docgen_tmp/test.zig:17:31:0x23149e in bar (test)        error.FileNotFound => try hello(),^/home/andy/Downloads/zig/docgen_tmp/test.zig:7:9:0x23138c in foo (test)        try bar();^/home/andy/Downloads/zig/docgen_tmp/test.zig:2:5:0x22a0c4 in main (test)    try foo(12);^

Look closely at this example. This is no stack trace.

You can see that the final error bubbled up wasPermissionDenied, but the original error that started this whole thing wasFileNotFound. In thebar function, the code handles the original error code, and then returns another one, from the switch statement. Error Return Traces make this clear, whereas a stack trace would look like this:

test.zig

pubfnmain()void {    foo(12);}fnfoo(x:i32)void {if (x >=5) {        bar();    }else {        bang2();    }}fnbar()void {if (baz()) {        quux();    }else {        hello();    }}fnbaz()bool {return bang1();}fnquux()void {    bang2();}fnhello()void {    bang2();}fnbang1()bool {returnfalse;}fnbang2()void {@panic("PermissionDenied");}
$ zig build-exe test.zig$ ./testthread 13421 panic: PermissionDenied/home/andy/Downloads/zig/docgen_tmp/test.zig:38:5:0x232a66 in bang2 (test)    @panic("PermissionDenied");^/home/andy/Downloads/zig/docgen_tmp/test.zig:30:10:0x2331a8 in hello (test)    bang2();^/home/andy/Downloads/zig/docgen_tmp/test.zig:17:14:0x232a4a in bar (test)        hello();^/home/andy/Downloads/zig/docgen_tmp/test.zig:7:12:0x231155 in foo (test)        bar();^/home/andy/Downloads/zig/docgen_tmp/test.zig:2:8:0x229efd in main (test)    foo(12);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x2222dc in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20616e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205196 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fa2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Here, the stack trace does not explain how the control flow inbar got to thehello() call. One would have to open a debugger or further instrument the application in order to find out. The error return trace, on the other hand, shows exactly how the error bubbled up.

This debugging feature makes it easier to iterate quickly on code that robustly handles all error conditions. This means that Zig developers will naturally find themselves writing correct, robust code in order to increase their development pace.

Error Return Traces are enabled by default inDebug andReleaseSafe builds and disabled by default inReleaseFast andReleaseSmall builds.

There are a few ways to activate this error return tracing feature:

  • Return an error from main
  • An error makes its way tocatchunreachable and you have not overridden the default panic handler
  • UseerrorReturnTrace to access the current return trace. You can usestd.debug.dumpStackTrace to print it. This function returns comptime-knownnull when building without error return tracing support.

Implementation Details§

To analyze performance cost, there are two cases:

  • when no errors are returned
  • when returning errors

For the case when no errors are returned, the cost is a single memory write operation, only in the first non-failable function in the call graph that calls a failable function, i.e. when a function returningvoid calls a function returningerror. This is to initialize this struct in the stack memory:

pubconst StackTrace =struct {    index:usize,    instruction_addresses: [N]usize,};

Here, N is the maximum function call depth as determined by call graph analysis. Recursion is ignored and counts for 2.

A pointer toStackTrace is passed as a secret parameter to every function that can return an error, but it's always the first parameter, so it can likely sit in a register and stay there.

That's it for the path when no errors occur. It's practically free in terms of performance.

When generating the code for a function that returns an error, just before thereturn statement (only for thereturn statements that return errors), Zig generates a call to this function:

// marked as "no-inline" in LLVM IRfn__zig_return_error(stack_trace: *StackTrace)void {    stack_trace.instruction_addresses[stack_trace.index] =@returnAddress();    stack_trace.index = (stack_trace.index +1) % N;}

The cost is 2 math operations plus some memory reads and writes. The memory accessed is constrained and should remain cached for the duration of the error return bubbling.

As for code size cost, 1 function call before a return statement is no big deal. Even so, I havea plan to make the call to__zig_return_error a tail call, which brings the code size cost down to actually zero. What is a return statement in code without error return tracing can become a jump instruction in code with error return tracing.

Optionals§

One area that Zig provides safety without compromising efficiency or readability is with the optional type.

The question mark symbolizes the optional type. You can convert a type to an optional type by putting a question mark in front of it, like this:

// normal integerconst normal_int:i32 =1234;// optional integerconst optional_int: ?i32 =5678;

Now the variableoptional_int could be ani32, ornull.

Instead of integers, let's talk about pointers. Null references are the source of many runtime exceptions, and even stand accused of beingthe worst mistake of computer science.

Zig does not have them.

Instead, you can use an optional pointer. This secretly compiles down to a normal pointer, since we know we can use 0 as the null value for the optional type. But the compiler can check your work and make sure you don't assign null to something that can't be null.

Typically the downside of not having null is that it makes the code more verbose to write. But, let's compare some equivalent C code and Zig code.

Task: call malloc, if the result is null, return null.

C code

// malloc prototype included for referencevoid *malloc(size_t size);struct Foo *do_a_thing(void) {    char *ptr = malloc(1234);    if (!ptr) return NULL;    // ...}

Zig code

// malloc prototype included for referenceexternfnmalloc(size: size_t) ?*u8;fndoAThing() ?*Foo {const ptr = malloc(1234)orelsereturnnull;// ...}

Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr" is*u8not?*u8. Theorelse keyword unwrapped the optional type and thereforeptr is guaranteed to be non-null everywhere it is used in the function.

The other form of checking against NULL you might see looks like this:

void do_a_thing(struct Foo *foo) {    // do some stuff    if (foo) {        do_something_with_foo(foo);    }    // do some stuff}

In Zig you can accomplish the same thing:

fndoAThing(optional_foo: ?*Foo)void {// do some stuffif (optional_foo) |foo| {      doSomethingWithFoo(foo);    }// do some stuff}

Once again, the notable thing here is that inside the if block,foo is no longer an optional pointer, it is a pointer, which cannot be null.

One benefit to this is that functions which take pointers as arguments can be annotated with the "nonnull" attribute -__attribute__((nonnull)) inGCC. The optimizer can sometimes make better decisions knowing that pointer arguments cannot be null.

Optional Type§

An optional is created by putting? in front of a type. You can use compile-time reflection to access the child type of an optional:

test.zig

const expect =@import("std").testing.expect;test"optional type" {// Declare an optional and coerce from null:var foo: ?i32 =null;// Coerce from child type of an optional    foo =1234;// Use compile-time reflection to access the child type of the optional:comptimetry expect(@typeInfo(@TypeOf(foo)).Optional.child ==i32);}
$ zig test test.zigTest [1/1] test "optional type"... All 1 tests passed.

null§

Just likeundefined,null has its own type, and the only way to use it is to cast it to a different type:

const optional_value: ?i32 =null;

Optional Pointers§

An optional pointer is guaranteed to be the same size as a pointer. Thenull of the optional is guaranteed to be address 0.

test.zig

const expect =@import("std").testing.expect;test"optional pointers" {// Pointers cannot be null. If you want a null pointer, use the optional// prefix `?` to make the pointer type optional.var ptr: ?*i32 =null;var x:i32 =1;    ptr = &x;try expect(ptr.?.* ==1);// Optional pointers are the same size as normal pointers, because pointer// value 0 is used as the null value.try expect(@sizeOf(?*i32) ==@sizeOf(*i32));}
$ zig test test.zigTest [1/1] test "optional pointers"... All 1 tests passed.

Casting§

Atype cast converts a value of one type to another. Zig hasType Coercion for conversions that are known to be completely safe and unambiguous, andExplicit Casts for conversions that one would not want to happen on accident. There is also a third kind of type conversion calledPeer Type Resolution for the case when a result type must be decided given multiple operand types.

Type Coercion§

Type coercion occurs when one type is expected, but different type is provided:

test.zig

test"type coercion - variable declaration" {var a:u8 =1;var b:u16 = a;}test"type coercion - function call" {var a:u8 =1;    foo(a);}fnfoo(b:u16)void {}test"type coercion - @as builtin" {var a:u8 =1;var b =@as(u16, a);}
$ zig test test.zigTest [1/3] test "type coercion - variable declaration"... Test [2/3] test "type coercion - function call"... Test [3/3] test "type coercion - @as builtin"... All 3 tests passed.

Type coercions are only allowed when it is completely unambiguous how to get from one type to another, and the transformation is guaranteed to be safe. There is one exception, which isC Pointers.

Type Coercion: Stricter Qualification§

Values which have the same representation at runtime can be cast to increase the strictness of the qualifiers, no matter how nested the qualifiers are:

  • const - non-const to const is allowed
  • volatile - non-volatile to volatile is allowed
  • align - bigger to smaller alignment is allowed
  • error sets to supersets is allowed

These casts are no-ops at runtime since the value representation does not change.

test.zig

test"type coercion - const qualification" {var a:i32 =1;var b: *i32 = &a;    foo(b);}fnfoo(a: *consti32)void {}
$ zig test test.zigTest [1/1] test "type coercion - const qualification"... All 1 tests passed.

In addition, pointers coerce to const optional pointers:

test.zig

const std =@import("std");const expect = std.testing.expect;const mem = std.mem;test"cast *[1][*]const u8 to [*]const ?[*]const u8" {const window_name = [1][*]constu8{"window name"};const x: [*]const ?[*]constu8 = &window_name;try expect(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]constu8, x[0].?)),"window name"));}
$ zig test test.zigTest [1/1] test "cast *[1][*]const u8 to [*]const ?[*]const u8"... All 1 tests passed.

Type Coercion: Integer and Float Widening§

Integers coerce to integer types which can represent every value of the old type, and likewiseFloats coerce to float types which can represent every value of the old type.

test.zig

const std =@import("std");const expect = std.testing.expect;const mem = std.mem;test"integer widening" {var a:u8 =250;var b:u16 = a;var c:u32 = b;var d:u64 = c;var e:u64 = d;var f:u128 = e;try expect(f == a);}test"implicit unsigned integer to signed integer" {var a:u8 =250;var b:i16 = a;try expect(b ==250);}test"float widening" {// Note: there is an open issue preventing this from working on aarch64:// https://github.com/ziglang/zig/issues/3282if (std.Target.current.cpu.arch == .aarch64)returnerror.SkipZigTest;var a:f16 =12.34;var b:f32 = a;var c:f64 = b;var d:f128 = c;try expect(d == a);}
$ zig test test.zigTest [1/3] test "integer widening"... Test [2/3] test "implicit unsigned integer to signed integer"... Test [3/3] test "float widening"... All 3 tests passed.

Type Coercion: Coercion Float to Int§

A compiler error is appropriate because this ambiguous expression leaves the compiler two choices about the coercion.

  • Cast54.0 tocomptime_int resulting in@as(comptime_int,10), which is casted to@as(f32,10)
  • Cast5 tocomptime_float resulting in@as(comptime_float,10.8), which is casted to@as(f32,10.8)

test.zig

// Compile time coercion of float to inttest"implicit cast to comptime_int" {var f:f32 =54.0 /5;}
$ zig test test.zig./docgen_tmp/test.zig:3:18:error:float value 54.000000 cannot be coerced to type 'comptime_int'    var f: f32 = 54.0 / 5;^./docgen_tmp/test.zig:3:23:note:referenced here    var f: f32 = 54.0 / 5;^

Type Coercion: Slices, Arrays and Pointers§

coerce__slices_arrays_and_ptrs.zig

const std =@import("std");const expect = std.testing.expect;// You can assign constant pointers to arrays to a slice with// const modifier on the element type. Useful in particular for// String literals.test"*const [N]T to []const T" {var x1: []constu8 ="hello";var x2: []constu8 = &[5]u8{'h','e','l','l',111 };try expect(std.mem.eql(u8, x1, x2));var y: []constf32 = &[2]f32{1.2,3.4 };try expect(y[0] ==1.2);}// Likewise, it works when the destination type is an error union.test"*const [N]T to E![]const T" {var x1:anyerror![]constu8 ="hello";var x2:anyerror![]constu8 = &[5]u8{'h','e','l','l',111 };try expect(std.mem.eql(u8,try x1,try x2));var y:anyerror![]constf32 = &[2]f32{1.2,3.4 };try expect((try y)[0] ==1.2);}// Likewise, it works when the destination type is an optional.test"*const [N]T to ?[]const T" {var x1: ?[]constu8 ="hello";var x2: ?[]constu8 = &[5]u8{'h','e','l','l',111 };try expect(std.mem.eql(u8, x1.?, x2.?));var y: ?[]constf32 = &[2]f32{1.2,3.4 };try expect(y.?[0] ==1.2);}// In this cast, the array length becomes the slice length.test"*[N]T to []T" {var buf: [5]u8 ="hello".*;const x: []u8 = &buf;try expect(std.mem.eql(u8, x,"hello"));const buf2 = [2]f32{1.2,3.4 };const x2: []constf32 = &buf2;try expect(std.mem.eql(f32, x2, &[2]f32{1.2,3.4 }));}// Single-item pointers to arrays can be coerced to many-item pointers.test"*[N]T to [*]T" {var buf: [5]u8 ="hello".*;const x: [*]u8 = &buf;try expect(x[4] =='o');// x[5] would be an uncaught out of bounds pointer dereference!}// Likewise, it works when the destination type is an optional.test"*[N]T to ?[*]T" {var buf: [5]u8 ="hello".*;const x: ?[*]u8 = &buf;try expect(x.?[4] =='o');}// Single-item pointers can be cast to len-1 single-item arrays.test"*T to *[1]T" {var x:i32 =1234;const y: *[1]i32 = &x;const z: [*]i32 = y;try expect(z[0] ==1234);}
$ zig test coerce__slices_arrays_and_ptrs.zigTest [1/7] test "*const [N]T to []const T"... Test [2/7] test "*const [N]T to E![]const T"... Test [3/7] test "*const [N]T to ?[]const T"... Test [4/7] test "*[N]T to []T"... Test [5/7] test "*[N]T to [*]T"... Test [6/7] test "*[N]T to ?[*]T"... Test [7/7] test "*T to *[1]T"... All 7 tests passed.

See also:

Type Coercion: Optionals§

The payload type ofOptionals, as well asnull, coerce to the optional type.

test.zig

const std =@import("std");const expect = std.testing.expect;test"coerce to optionals" {const x: ?i32 =1234;const y: ?i32 =null;try expect(x.? ==1234);try expect(y ==null);}
$ zig test test.zigTest [1/1] test "coerce to optionals"... All 1 tests passed.

It works nested inside theError Union Type, too:

test.zig

const std =@import("std");const expect = std.testing.expect;test"coerce to optionals wrapped in error union" {const x:anyerror!?i32 =1234;const y:anyerror!?i32 =null;try expect((try x).? ==1234);try expect((try y) ==null);}
$ zig test test.zigTest [1/1] test "coerce to optionals wrapped in error union"... All 1 tests passed.

Type Coercion: Error Unions§

The payload type of anError Union Type as well as theError Set Type coerce to the error union type:

test.zig

const std =@import("std");const expect = std.testing.expect;test"coercion to error unions" {const x:anyerror!i32 =1234;const y:anyerror!i32 =error.Failure;try expect((try x) ==1234);try std.testing.expectError(error.Failure, y);}
$ zig test test.zigTest [1/1] test "coercion to error unions"... All 1 tests passed.

Type Coercion: Compile-Time Known Numbers§

When a number iscomptime-known to be representable in the destination type, it may be coerced:

test.zig

const std =@import("std");const expect = std.testing.expect;test"coercing large integer type to smaller one when value is comptime known to fit" {const x:u64 =255;const y:u8 = x;try expect(y ==255);}
$ zig test test.zigTest [1/1] test "coercing large integer type to smaller one when value is comptime known ... All 1 tests passed.

Type Coercion: unions and enums§

Tagged unions can be coerced to enums, and enums can be coerced to tagged unions when they arecomptime-known to be a field of the union that has only one possible value, such asvoid:

test.zig

const std =@import("std");const expect = std.testing.expect;const E =enum {    one,    two,    three,};const U =union(E) {    one:i32,    two:f32,    three,};test"coercion between unions and enums" {var u = U{ .two =12.34 };var e: E = u;try expect(e == E.two);const three = E.three;var another_u: U = three;try expect(another_u == E.three);}
$ zig test test.zigTest [1/1] test "coercion between unions and enums"... All 1 tests passed.

See also:

Type Coercion: Zero Bit Types§

Zero Bit Types may be coerced to single-itemPointers, regardless of const.

TODO document the reasoning for this

TODO document whether vice versa should work and why

test.zig

test"coercion of zero bit types" {var x:void = {};var y: *void = x;//var z: void = y; // TODO}
$ zig test test.zigTest [1/1] test "coercion of zero bit types"... All 1 tests passed.

Type Coercion: undefined§

undefined can be cast to any type.

Explicit Casts§

Explicit casts are performed viaBuiltin Functions. Some explicit casts are safe; some are not. Some explicit casts perform language-level assertions; some do not. Some explicit casts are no-ops at runtime; some are not.

  • @bitCast - change type but maintain bit representation
  • @alignCast - make a pointer have more alignment
  • @boolToInt - convert true to 1 and false to 0
  • @enumToInt - obtain the integer tag value of an enum or tagged union
  • @errSetCast - convert to a smaller error set
  • @errorToInt - obtain the integer value of an error code
  • @floatCast - convert a larger float to a smaller float
  • @floatToInt - obtain the integer part of a float value
  • @intCast - convert between integer types
  • @intToEnum - obtain an enum value based on its integer tag value
  • @intToError - obtain an error code based on its integer value
  • @intToFloat - convert an integer to a float value
  • @intToPtr - convert an address to a pointer
  • @ptrCast - convert between pointer types
  • @ptrToInt - obtain the address of a pointer
  • @truncate - convert between integer types, chopping off bits

Peer Type Resolution§

Peer Type Resolution occurs in these places:

This kind of type resolution chooses a type that all peer types can coerce into. Here are some examples:

peer_type_resolution.zig

const std =@import("std");const expect = std.testing.expect;const mem = std.mem;test"peer resolve int widening" {var a:i8 =12;var b:i16 =34;var c = a + b;try expect(c ==46);try expect(@TypeOf(c) ==i16);}test"peer resolve arrays of different size to const slice" {try expect(mem.eql(u8, boolToStr(true),"true"));try expect(mem.eql(u8, boolToStr(false),"false"));comptimetry expect(mem.eql(u8, boolToStr(true),"true"));comptimetry expect(mem.eql(u8, boolToStr(false),"false"));}fnboolToStr(b:bool) []constu8 {returnif (b)"true"else"false";}test"peer resolve array and const slice" {try testPeerResolveArrayConstSlice(true);comptimetry testPeerResolveArrayConstSlice(true);}fntestPeerResolveArrayConstSlice(b:bool) !void {const value1 =if (b)"aoeu"else@as([]constu8,"zz");const value2 =if (b)@as([]constu8,"zz")else"aoeu";try expect(mem.eql(u8, value1,"aoeu"));try expect(mem.eql(u8, value2,"zz"));}test"peer type resolution: ?T and T" {try expect(peerTypeTAndOptionalT(true,false).? ==0);try expect(peerTypeTAndOptionalT(false,false).? ==3);comptime {try expect(peerTypeTAndOptionalT(true,false).? ==0);try expect(peerTypeTAndOptionalT(false,false).? ==3);    }}fnpeerTypeTAndOptionalT(c:bool, b:bool) ?usize {if (c) {returnif (b)nullelse@as(usize,0);    }return@as(usize,3);}test"peer type resolution: *[0]u8 and []const u8" {try expect(peerTypeEmptyArrayAndSlice(true,"hi").len ==0);try expect(peerTypeEmptyArrayAndSlice(false,"hi").len ==1);comptime {try expect(peerTypeEmptyArrayAndSlice(true,"hi").len ==0);try expect(peerTypeEmptyArrayAndSlice(false,"hi").len ==1);    }}fnpeerTypeEmptyArrayAndSlice(a:bool, slice: []constu8) []constu8 {if (a) {return &[_]u8{};    }return slice[0..1];}test"peer type resolution: *[0]u8, []const u8, and anyerror![]u8" {    {var data ="hi".*;const slice = data[0..];try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len ==0);try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len ==1);    }comptime {var data ="hi".*;const slice = data[0..];try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len ==0);try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len ==1);    }}fnpeerTypeEmptyArrayAndSliceAndError(a:bool, slice: []u8)anyerror![]u8 {if (a) {return &[_]u8{};    }return slice[0..1];}test"peer type resolution: *const T and ?*T" {const a =@intToPtr(*constusize,0x123456780);const b =@intToPtr(?*usize,0x123456780);try expect(a == b);try expect(b == a);}
$ zig test peer_type_resolution.zigTest [1/7] test "peer resolve int widening"... Test [2/7] test "peer resolve arrays of different size to const slice"... Test [3/7] test "peer resolve array and const slice"... Test [4/7] test "peer type resolution: ?T and T"... Test [5/7] test "peer type resolution: *[0]u8 and []const u8"... Test [6/7] test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8"... Test [7/7] test "peer type resolution: *const T and ?*T"... All 7 tests passed.

Zero Bit Types§

For some types,@sizeOf is 0:

These types can only ever have one possible value, and thus require 0 bits to represent. Code that makes use of these types is not included in the final generated code:

exportfnentry()void {var x:void = {};var y:void = {};    x = y;}

When this turns into machine code, there is no code generated in the body ofentry, even inDebug mode. For example, on x86_64:

0000000000000010 <entry>:  10:55                   push   %rbp  11:48 89 e5             mov    %rsp,%rbp  14:5d                   pop    %rbp  15:c3                   retq

These assembly instructions do not have any code associated with the void values - they only perform the function call prologue and epilog.

void§

void can be useful for instantiating generic types. For example, given aMap(Key, Value), one can passvoid for theValue type to make it into aSet:

test.zig

const std =@import("std");const expect = std.testing.expect;test"turn HashMap into a set with void" {var map = std.AutoHashMap(i32,void).init(std.testing.allocator);defer map.deinit();try map.put(1, {});try map.put(2, {});try expect(map.contains(2));try expect(!map.contains(3));    _ = map.remove(2);try expect(!map.contains(2));}
$ zig test test.zigTest [1/1] test "turn HashMap into a set with void"... All 1 tests passed.

Note that this is different from using a dummy value for the hash map value. By usingvoid as the type of the value, the hash map entry type has no value field, and thus the hash map takes up less space. Further, all the code that deals with storing and loading the value is deleted, as seen above.

void is distinct fromc_void.void has a known size of 0 bytes, andc_void has an unknown, but non-zero, size.

Expressions of typevoid are the only ones whose value can be ignored. For example:

test.zig

test"ignoring expression value" {    foo();}fnfoo()i32 {return1234;}
$ zig test test.zig./docgen_tmp/test.zig:2:8:error:expression value is ignored    foo();^./docgen_tmp/test.zig:1:34:note:referenced heretest "ignoring expression value" {^

However, if the expression has typevoid, there will be no error. Function return values can also be explicitly ignored by assigning them to_.

test.zig

test"void is ignored" {    returnsVoid();}test"explicitly ignoring expression value" {    _ = foo();}fnreturnsVoid()void {}fnfoo()i32 {return1234;}
$ zig test test.zigTest [1/2] test "void is ignored"... Test [2/2] test "explicitly ignoring expression value"... All 2 tests passed.

Pointers to Zero Bit Types§

Pointers to zero bit types also have zero bits. They always compare equal to each other:

test.zig

const std =@import("std");const expect = std.testing.expect;test"pointer to empty struct" {const Empty =struct {};var a = Empty{};var b = Empty{};var ptr_a = &a;var ptr_b = &b;comptimetry expect(ptr_a == ptr_b);}
$ zig test test.zigTest [1/1] test "pointer to empty struct"... All 1 tests passed.

The type being pointed to can only ever be one value; therefore loads and stores are never generated.ptrToInt andintToPtr are not allowed:

test.zig

const Empty =struct {};test"@ptrToInt for pointer to zero bit type" {var a = Empty{};    _ =@ptrToInt(&a);}test"@intToPtr for pointer to zero bit type" {    _ =@intToPtr(*Empty,0x1);}
$ zig test test.zig./docgen_tmp/test.zig:4:5:error:pointer to size 0 type has no address    var a = Empty{};^./docgen_tmp/test.zig:5:9:note:referenced here    _ = @ptrToInt(&a);^./docgen_tmp/test.zig:9:19:error:type '*Empty' has 0 bits and cannot store information    _ = @intToPtr(*Empty, 0x1);^./docgen_tmp/test.zig:9:9:note:referenced here    _ = @intToPtr(*Empty, 0x1);^

Result Location Semantics§

TODO add documentation for this

usingnamespace§

usingnamespace is a declaration that imports all the public declarations of the operand, which must be astruct,union, orenum, into the current scope:

usingnamespace.zig

usingnamespace@import("std");test"using std namespace" {try testing.expect(true);}
$ zig test usingnamespace.zigTest [1/1] test "using std namespace"... All 1 tests passed.

usingnamespace can also be used in containers:

usingnamespace_inside_struct.zig

test"using namespace inside struct" {const L =struct {usingnamespacestruct {pubfnf()void {}        };    };    L.f();}
$ zig test usingnamespace_inside_struct.zigTest [1/1] test "using namespace inside struct"... All 1 tests passed.

Instead of the above pattern, it is generally recommended to explicitly alias individual declarations. However,usingnamespace has an important use case when organizing the public API of a file or package. For example, one might havec.zig with all of theC imports:

pubusingnamespace@cImport({@cInclude("epoxy/gl.h");@cInclude("GLFW/glfw3.h");@cDefine("STBI_ONLY_PNG","");@cDefine("STBI_NO_STDIO","");@cInclude("stb_image.h");});

The above example demonstrates usingpub to qualify theusingnamespace additionally makes the imported declarationspub. This can be used to forward declarations, giving precise control over what declarations a given file exposes.

comptime§

Zig places importance on the concept of whether an expression is known at compile-time. There are a few different places this concept is used, and these building blocks are used to keep the language small, readable, and powerful.

Introducing the Compile-Time Concept§

Compile-Time Parameters§

Compile-time parameters is how Zig implements generics. It is compile-time duck typing.

fnmax(comptime T:type, a: T, b: T) T {returnif (a > b) aelse b;}fngimmeTheBiggerFloat(a:f32, b:f32)f32 {return max(f32, a, b);}fngimmeTheBiggerInteger(a:u64, b:u64)u64 {return max(u64, a, b);}

In Zig, types are first-class citizens. They can be assigned to variables, passed as parameters to functions, and returned from functions. However, they can only be used in expressions which are known atcompile-time, which is why the parameterT in the above snippet must be marked withcomptime.

Acomptime parameter means that:

  • At the callsite, the value must be known at compile-time, or it is a compile error.
  • In the function definition, the value is known at compile-time.

For example, if we were to introduce another function to the above snippet:

test.zig

fnmax(comptime T:type, a: T, b: T) T {returnif (a > b) aelse b;}test"try to pass a runtime type" {    foo(false);}fnfoo(condition:bool)void {const result = max(if (condition)f32elseu64,1234,5678);}
$ zig test test.zig./docgen_tmp/test.zig:9:9:error:values of type 'type' must be comptime known        if (condition) f32 else u64,^

This is an error because the programmer attempted to pass a value only known at run-time to a function which expects a value known at compile-time.

Another way to get an error is if we pass a type that violates the type checker when the function is analyzed. This is what it means to havecompile-time duck typing.

For example:

test.zig

fnmax(comptime T:type, a: T, b: T) T {returnif (a > b) aelse b;}test"try to compare bools" {    _ = max(bool,true,false);}
$ zig test test.zig./docgen_tmp/test.zig:2:18:error:operator not allowed for type 'bool'    return if (a > b) a else b;^./docgen_tmp/test.zig:5:12:note:called from here    _ = max(bool, true, false);^./docgen_tmp/test.zig:4:29:note:called from heretest "try to compare bools" {^

On the flip side, inside the function definition with thecomptime parameter, the value is known at compile-time. This means that we actually could make this work for the bool type if we wanted to:

test.zig

fnmax(comptime T:type, a: T, b: T) T {if (T ==bool) {return aor b;    }elseif (a > b) {return a;    }else {return b;    }}test"try to compare bools" {try@import("std").testing.expect(max(bool,false,true) ==true);}
$ zig test test.zigTest [1/1] test "try to compare bools"... All 1 tests passed.

This works because Zig implicitly inlinesif expressions when the condition is known at compile-time, and the compiler guarantees that it will skip analysis of the branch not taken.

This means that the actual function generated formax in this situation looks like this:

fnmax(a:bool, b:bool)bool {return aor b;}

All the code that dealt with compile-time known values is eliminated and we are left with only the necessary run-time code to accomplish the task.

This works the same way forswitch expressions - they are implicitly inlined when the target expression is compile-time known.

Compile-Time Variables§

In Zig, the programmer can label variables ascomptime. This guarantees to the compiler that every load and store of the variable is performed at compile-time. Any violation of this results in a compile error.

This combined with the fact that we caninline loops allows us to write a function which is partially evaluated at compile-time and partially at run-time.

For example:

comptime_vars.zig

const expect =@import("std").testing.expect;const CmdFn =struct {    name: []constu8,    func:fn(i32)i32,};const cmd_fns = [_]CmdFn{    CmdFn {.name ="one", .func = one},    CmdFn {.name ="two", .func = two},    CmdFn {.name ="three", .func = three},};fnone(value:i32)i32 {return value +1; }fntwo(value:i32)i32 {return value +2; }fnthree(value:i32)i32 {return value +3; }fnperformFn(comptime prefix_char:u8, start_value:i32)i32 {var result:i32 = start_value;comptimevar i =0;inlinewhile (i < cmd_fns.len) : (i +=1) {if (cmd_fns[i].name[0] == prefix_char) {            result = cmd_fns[i].func(result);        }    }return result;}test"perform fn" {try expect(performFn('t',1) ==6);try expect(performFn('o',0) ==1);try expect(performFn('w',99) ==99);}
$ zig test comptime_vars.zigTest [1/1] test "perform fn"... All 1 tests passed.

This example is a bit contrived, because the compile-time evaluation component is unnecessary; this code would work fine if it was all done at run-time. But it does end up generating different code. In this example, the functionperformFn is generated three different times, for the different values ofprefix_char provided:

// From the line:// expect(performFn('t', 1) == 6);fnperformFn(start_value:i32)i32 {var result:i32 = start_value;    result = two(result);    result = three(result);return result;}
// From the line:// expect(performFn('o', 0) == 1);fnperformFn(start_value:i32)i32 {var result:i32 = start_value;    result = one(result);return result;}
// From the line:// expect(performFn('w', 99) == 99);fnperformFn(start_value:i32)i32 {var result:i32 = start_value;return result;}

Note that this happens even in a debug build; in a release build these generated functions still pass through rigorous LLVM optimizations. The important thing to note, however, is not that this is a way to write more optimized code, but that it is a way to make sure that whatshould happen at compile-time,does happen at compile-time. This catches more errors and as demonstrated later in this article, allows expressiveness that in other languages requires using macros, generated code, or a preprocessor to accomplish.

Compile-Time Expressions§

In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can use acomptime expression to guarantee that the expression will be evaluated at compile-time. If this cannot be accomplished, the compiler will emit an error. For example:

test.zig

externfnexit()noreturn;test"foo" {comptime {        exit();    }}
$ zig test test.zig./docgen_tmp/test.zig:5:9:error:unable to evaluate constant expression        exit();^./docgen_tmp/test.zig:5:13:note:referenced here        exit();^

It doesn't make sense that a program could callexit() (or any other external function) at compile-time, so this is a compile error. However, acomptime expression does much more than sometimes cause a compile error.

Within acomptime expression:

  • All variables arecomptime variables.
  • Allif,while,for, andswitch expressions are evaluated at compile-time, or emit a compile error if this is not possible.
  • All function calls cause the compiler to interpret the function at compile-time, emitting a compile error if the function tries to do something that has global run-time side effects.

This means that a programmer can create a function which is called both at compile-time and run-time, with no modification to the function required.

Let's look at an example:

test.zig

const expect =@import("std").testing.expect;fnfibonacci(index:u32)u32 {if (index <2)return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {// test fibonacci at run-timetry expect(fibonacci(7) ==13);// test fibonacci at compile-timecomptime {try expect(fibonacci(7) ==13);    }}
$ zig test test.zigTest [1/1] test "fibonacci"... All 1 tests passed.

Imagine if we had forgotten the base case of the recursive function and tried to run the tests:

test.zig

const expect =@import("std").testing.expect;fnfibonacci(index:u32)u32 {//if (index < 2) return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {comptime {try expect(fibonacci(7) ==13);    }}
$ zig test test.zig./docgen_tmp/test.zig:5:28:error:operation caused overflow    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:10:29:note:called from here        try expect(fibonacci(7) == 13);^./docgen_tmp/test.zig:8:18:note:called from heretest "fibonacci" {^./docgen_tmp/test.zig:5:21:note:referenced here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:10:29:note:referenced here        try expect(fibonacci(7) == 13);^

The compiler produces an error which is a stack trace from trying to evaluate the function at compile-time.

Luckily, we used an unsigned integer, and so when we tried to subtract 1 from 0, it triggered undefined behavior, which is always a compile error if the compiler knows it happened. But what would have happened if we used a signed integer?

test.zig

const expect =@import("std").testing.expect;fnfibonacci(index:i32)i32 {//if (index < 2) return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {comptime {try expect(fibonacci(7) ==13);    }}
$ zig test test.zig./docgen_tmp/test.zig:5:21:error:evaluation exceeded 1000 backwards branches    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:5:21:note:called from here    return fibonacci(index - 1) + fibonacci(index - 2);^./docgen_tmp/test.zig:10:29:note:referenced here        try expect(fibonacci(7) == 13);^

The compiler noticed that evaluating this function at compile-time took a long time, and thus emitted a compile error and gave up. If the programmer wants to increase the budget for compile-time computation, they can use a built-in function called@setEvalBranchQuota to change the default number 1000 to something else.

What if we fix the base case, but put the wrong value in theexpect line?

test.zig

const expect =@import("std").testing.expect;fnfibonacci(index:i32)i32 {if (index <2)return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {comptime {try expect(fibonacci(7) ==99999);    }}
$ zig test test.zigTest [1/1] test "fibonacci"... Test [2/1] test "fibonacci"... FAIL (TestUnexpectedResult)0 passed; 0 skipped; 1 failed.error: the following test command failed with exit code 1:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

What happened is Zig started interpreting theexpect function with the parameterok set tofalse. When the interpreter hit@panic it emitted a compile error because a panic during compile causes a compile error if it is detected at compile-time.

At container level (outside of any function), all expressions are implicitlycomptime expressions. This means that we can use functions to initialize complex static data. For example:

test.zig

const first_25_primes = firstNPrimes(25);const sum_of_first_25_primes = sum(&first_25_primes);fnfirstNPrimes(comptime n:usize) [n]i32 {var prime_list: [n]i32 =undefined;var next_index:usize =0;var test_number:i32 =2;while (next_index < prime_list.len) : (test_number +=1) {var test_prime_index:usize =0;var is_prime =true;while (test_prime_index < next_index) : (test_prime_index +=1) {if (test_number % prime_list[test_prime_index] ==0) {                is_prime =false;break;            }        }if (is_prime) {            prime_list[next_index] = test_number;            next_index +=1;        }    }return prime_list;}fnsum(numbers: []consti32)i32 {var result:i32 =0;for (numbers) |x| {        result += x;    }return result;}test"variable values" {try@import("std").testing.expect(sum_of_first_25_primes ==1060);}
$ zig test test.zigTest [1/1] test "variable values"... All 1 tests passed.

When we compile this program, Zig generates the constants with the answer pre-computed. Here are the lines from the generated LLVM IR:

@0 = internal unnamed_addr constant [25 x i32] [i32 2, i32 3, i32 5, i32 7, i32 11, i32 13, i32 17, i32 19, i32 23, i32 29, i32 31, i32 37, i32 41, i32 43, i32 47, i32 53, i32 59, i32 61, i32 67, i32 71, i32 73, i32 79, i32 83, i32 89, i32 97]@1 = internal unnamed_addr constant i32 1060

Note that we did not have to do anything special with the syntax of these functions. For example, we could call thesum function as is with a slice of numbers whose length and values were only known at run-time.

Generic Data Structures§

Zig uses these capabilities to implement generic data structures without introducing any special-case syntax. If you followed along so far, you may already know how to create a generic data structure.

Here is an example of a genericList data structure, that we will instantiate with the typei32. In Zig we refer to the type asList(i32).

fnList(comptime T:type)type {returnstruct {        items: []T,        len:usize,    };}

That's it. It's a function that returns an anonymousstruct. For the purposes of error messages and debugging, Zig infers the name"List(i32)" from the function name and parameters invoked when creating the anonymous struct.

To keep the language small and uniform, all aggregate types in Zig are anonymous. To give a type a name, we assign it to a constant:

const Node =struct {    next: *Node,    name: []u8,};

This works because all top level declarations are order-independent, and as long as there isn't an actual infinite regression, values can refer to themselves, directly or indirectly. In this case,Node refers to itself as a pointer, which is not actually an infinite regression, so it works fine.

Case Study: printf in Zig§

Putting all of this together, let's see howprintf works in Zig.

printf.zig

const print =@import("std").debug.print;const a_number:i32 =1234;const a_string ="foobar";pubfnmain()void {    print("here is a string: '{s}' here is a number: {}\n", .{a_string, a_number});}
$ zig build-exe printf.zig$ ./printfhere is a string: 'foobar' here is a number: 1234

Let's crack open the implementation of this and see how it works:

/// Calls print and then flushes the buffer.pubfnprintf(self: *Writer,comptime format: []constu8, args:anytype)anyerror!void {const State =enum {        start,        open_brace,        close_brace,    };comptimevar start_index:usize =0;comptimevar state = State.start;comptimevar next_arg:usize =0;inlinefor (format) |c, i| {switch (state) {            State.start =>switch (c) {'{' => {if (start_index < i)try self.write(format[start_index..i]);                    state = State.open_brace;                },'}' => {if (start_index < i)try self.write(format[start_index..i]);                    state = State.close_brace;                },else => {},            },            State.open_brace =>switch (c) {'{' => {                    state = State.start;                    start_index = i;                },'}' => {try self.printValue(args[next_arg]);                    next_arg +=1;                    state = State.start;                    start_index = i +1;                },else =>@compileError("Unknown format character: " ++ c),            },            State.close_brace =>switch (c) {'}' => {                    state = State.start;                    start_index = i;                },else =>@compileError("Single '}' encountered in format string"),            },        }    }comptime {if (args.len != next_arg) {@compileError("Unused arguments");        }if (state != State.Start) {@compileError("Incomplete format string: " ++ format);        }    }if (start_index < format.len) {try self.write(format[start_index..format.len]);    }try self.flush();}

This is a proof of concept implementation; the actual function in the standard library has more formatting capabilities.

Note that this is not hard-coded into the Zig compiler; this is userland code in the standard library.

When this function is analyzed from our example code above, Zig partially evaluates the function and emits a function that actually looks like this:

pubfnprintf(self: *Writer, arg0:i32, arg1: []constu8) !void {try self.write("here is a string: '");try self.printValue(arg0);try self.write("' here is a number: ");try self.printValue(arg1);try self.write("\n");try self.flush();}

printValue is a function that takes a parameter of any type, and does different things depending on the type:

pubfnprintValue(self: *Writer, value:anytype) !void {switch (@typeInfo(@TypeOf(value))) {        .Int => {return self.printInt(T, value);        },        .Float => {return self.printFloat(T, value);        },else => {@compileError("Unable to print type '" ++@typeName(T) ++"'");        },    }}

And now, what happens if we give too many arguments toprintf?

test.zig

const print =@import("std").debug.print;const a_number:i32 =1234;const a_string ="foobar";test"printf too many arguments" {    print("here is a string: '{s}' here is a number: {}\n", .{        a_string,        a_number,        a_number,    });}
$ zig test test.zig./lib/std/fmt.zig:362:9:error:Unused arguments        @compileError("Unused arguments");^./lib/std/io/writer.zig:34:34:note:called from here            return std.fmt.format(self, format, args);^./lib/std/debug.zig:68:27:note:called from here    nosuspend stderr.print(fmt, args) catch return;^./docgen_tmp/test.zig:7:10:note:called from here    print("here is a string: '{s}' here is a number: {}\n", .{^./docgen_tmp/test.zig:6:34:note:called from heretest "printf too many arguments" {^./lib/std/io/writer.zig:34:34:error:expected type 'std.os.WriteError!void', found '@typeInfo(@typeInfo(@TypeOf(std.fmt.format)).Fn.return_type.?).ErrorUnion.error_set!void'            return std.fmt.format(self, format, args);^./lib/std/debug.zig:68:27:note:called from here    nosuspend stderr.print(fmt, args) catch return;^./docgen_tmp/test.zig:7:10:note:called from here    print("here is a string: '{s}' here is a number: {}\n", .{^./docgen_tmp/test.zig:6:34:note:called from heretest "printf too many arguments" {^./lib/std/io/writer.zig:34:34:note:error set '@typeInfo(@typeInfo(@TypeOf(std.fmt.format)).Fn.return_type.?).ErrorUnion.error_set' cannot cast into error set 'std.os.WriteError'            return std.fmt.format(self, format, args);^

Zig gives programmers the tools needed to protect themselves against their own mistakes.

Zig doesn't care whether the format argument is a string literal, only that it is a compile-time known value that can be coerced to a[]constu8:

printf.zig

const print =@import("std").debug.print;const a_number:i32 =1234;const a_string ="foobar";const fmt ="here is a string: '{s}' here is a number: {}\n";pubfnmain()void {    print(fmt, .{a_string, a_number});}
$ zig build-exe printf.zig$ ./printfhere is a string: 'foobar' here is a number: 1234

This works fine.

Zig does not special case string formatting in the compiler and instead exposes enough power to accomplish this task in userland. It does so without introducing another language on top of Zig, such as a macro language or a preprocessor language. It's Zig all the way down.

See also:

Assembly§

For some use cases, it may be necessary to directly control the machine code generated by Zig programs, rather than relying on Zig's code generation. For these cases, one can use inline assembly. Here is an example of implementing Hello, World on x86_64 Linux using inline assembly:

test.zig

pubfnmain()noreturn {const msg ="hello world\n";    _ = syscall3(SYS_write, STDOUT_FILENO,@ptrToInt(msg), msg.len);    _ = syscall1(SYS_exit,0);unreachable;}pubconst SYS_write =1;pubconst SYS_exit =60;pubconst STDOUT_FILENO =1;pubfnsyscall1(number:usize, arg1:usize)usize {returnasmvolatile ("syscall"        : [ret]"={rax}" (->usize)        : [number]"{rax}" (number),          [arg1]"{rdi}" (arg1)        :"rcx","r11"    );}pubfnsyscall3(number:usize, arg1:usize, arg2:usize, arg3:usize)usize {returnasmvolatile ("syscall"        : [ret]"={rax}" (->usize)        : [number]"{rax}" (number),          [arg1]"{rdi}" (arg1),          [arg2]"{rsi}" (arg2),          [arg3]"{rdx}" (arg3)        :"rcx","r11"    );}
$ zig build-exe test.zig -target x86_64-linux$ ./testhello world

Dissecting the syntax:

// Inline assembly is an expression which returns a value.// the `asm` keyword begins the expression._ =asm// `volatile` is an optional modifier that tells Zig this// inline assembly expression has side-effects. Without// `volatile`, Zig is allowed to delete the inline assembly// code if the result is unused.volatile (// Next is a comptime string which is the assembly code.// Inside this string one may use `%[ret]`, `%[number]`,// or `%[arg1]` where a register is expected, to specify// the register that Zig uses for the argument or return value,// if the register constraint strings are used. However in// the below code, this is not used. A literal `%` can be// obtained by escaping it with a double percent: `%%`.// Often multiline string syntax comes in handy here.\\syscall// Next is the output. It is possible in the future Zig will// support multiple outputs, depending on how// https://github.com/ziglang/zig/issues/215 is resolved.// It is allowed for there to be no outputs, in which case// this colon would be directly followed by the colon for the inputs.    :// This specifies the name to be used in `%[ret]` syntax in// the above assembly string. This example does not use it,// but the syntax is mandatory.    [ret]// Next is the output constraint string. This feature is still// considered unstable in Zig, and so LLVM/GCC documentation// must be used to understand the semantics.// http://releases.llvm.org/10.0.0/docs/LangRef.html#inline-asm-constraint-string// https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html// In this example, the constraint string means "the result value of// this inline assembly instruction is whatever is in $rax"."={rax}"// Next is either a value binding, or `->` and then a type. The// type is the result type of the inline assembly expression.// If it is a value binding, then `%[ret]` syntax would be used// to refer to the register bound to the value.    (->usize)// Next is the list of inputs.// The constraint for these inputs means, "when the assembly code is// executed, $rax shall have the value of `number` and $rdi shall have// the value of `arg1`". Any number of input parameters is allowed,// including none.    : [number]"{rax}" (number),        [arg1]"{rdi}" (arg1)// Next is the list of clobbers. These declare a set of registers whose// values will not be preserved by the execution of this assembly code.// These do not include output or input registers. The special clobber// value of "memory" means that the assembly writes to arbitrary undeclared// memory locations - not only the memory pointed to by a declared indirect// output. In this example we list $rcx and $r11 because it is known the// kernel syscall does not preserve these registers.    :"rcx","r11");

For i386 and x86_64 targets, the syntax is AT&T syntax, rather than the more popular Intel syntax. This is due to technical constraints; assembly parsing is provided by LLVM and its support for Intel syntax is buggy and not well tested.

Some day Zig may have its own assembler. This would allow it to integrate more seamlessly into the language, as well as be compatible with the popular NASM syntax. This documentation section will be updated before 1.0.0 is released, with a conclusive statement about the status of AT&T vs Intel/NASM syntax.

Output Constraints§

Output constraints are still considered to be unstable in Zig, and soLLVM documentation andGCC documentation must be used to understand the semantics.

Note that some breaking changes to output constraints are planned withissue #215.

Input Constraints§

Input constraints are still considered to be unstable in Zig, and soLLVM documentation andGCC documentation must be used to understand the semantics.

Note that some breaking changes to input constraints are planned withissue #215.

Clobbers§

Clobbers are the set of registers whose values will not be preserved by the execution of the assembly code. These do not include output or input registers. The special clobber value of"memory" means that the assembly causes writes to arbitrary undeclared memory locations - not only the memory pointed to by a declared indirect output.

Failure to declare the full set of clobbers for a given inline assembly expression is uncheckedUndefined Behavior.

Global Assembly§

When an assembly expression occurs in a container levelcomptime block, this isglobal assembly.

This kind of assembly has different rules than inline assembly. First,volatile is not valid because all global assembly is unconditionally included. Second, there are no inputs, outputs, or clobbers. All global assembly is concatenated verbatim into one long string and assembled together. There are no template substitution rules regarding% as there are in inline assembly expressions.

global-asm.zig

const std =@import("std");const expect = std.testing.expect;comptime {asm (\\.global my_func;\\.type my_func, @function;\\my_func:\\  lea (%rdi,%rsi,1),%eax\\  retq    );}externfnmy_func(a:i32, b:i32)i32;test"global assembly" {try expect(my_func(12,34) ==46);}
$ zig test global-asm.zig -target x86_64-linuxTest [1/1] test "global assembly"... All 1 tests passed.

Atomics§

TODO: @fence()

TODO: @atomic rmw

TODO: builtin atomic memory ordering enum

Async Functions§

When a function is called, a frame is pushed to the stack, the function runs until it reaches a return statement, and then the frame is popped from the stack. At the callsite, the following code does not run until the function returns.

An async function is a function whose callsite is split into anasync initiation, followed by anawait completion. Its frame is provided explicitly by the caller, and it can be suspended and resumed any number of times.

Zig infers that a function isasync when it observes that the function contains asuspension point. Async functions can be called the same as normal functions. A function call of an async function is a suspend point.

Suspend and Resume§

At any point, a function may suspend itself. This causes control flow to return to the callsite (in the case of the first suspension), or resumer (in the case of subsequent suspensions).

test.zig

const std =@import("std");const expect = std.testing.expect;var x:i32 =1;test"suspend with no resume" {var frame =async func();try expect(x ==2);}fnfunc()void {    x +=1;suspend {}// This line is never reached because the suspend has no matching resume.    x +=1;}
$ zig test test.zigTest [1/1] test "suspend with no resume"... All 1 tests passed.

In the same way that each allocation should have a corresponding free, Eachsuspend should have a correspondingresume. Asuspend block allows a function to put a pointer to its own frame somewhere, for example into an event loop, even if that action will perform aresume operation on a different thread.@frame provides access to the async function frame pointer.

test.zig

const std =@import("std");const expect = std.testing.expect;var the_frame:anyframe =undefined;var result =false;test"async function suspend with block" {    _ =async testSuspendBlock();try expect(!result);resume the_frame;try expect(result);}fntestSuspendBlock()void {suspend {comptimetry expect(@TypeOf(@frame()) == *@Frame(testSuspendBlock));        the_frame =@frame();    }    result =true;}
$ zig test test.zigTest [1/1] test "async function suspend with block"... All 1 tests passed.

suspend causes a function to beasync.

Resuming from Suspend Blocks§

Upon entering asuspend block, the async function is already considered suspended, and can be resumed. For example, if you started another kernel thread, and had that thread callresume on the frame pointer provided by the@frame, the new thread would begin executing after the suspend block, while the old thread continued executing the suspend block.

However, the async function can be directly resumed from the suspend block, in which case it never returns to its resumer and continues executing.

test.zig

const std =@import("std");const expect = std.testing.expect;test"resume from suspend" {var my_result:i32 =1;    _ =async testResumeFromSuspend(&my_result);try std.testing.expect(my_result ==2);}fntestResumeFromSuspend(my_result: *i32)void {suspend {resume@frame();    }    my_result.* +=1;suspend {}    my_result.* +=1;}
$ zig test test.zigTest [1/1] test "resume from suspend"... All 1 tests passed.

This is guaranteed to tail call, and therefore will not cause a new stack frame.

Async and Await§

In the same way that everysuspend has a matchingresume, everyasync has a matchingawait.

test.zig

const std =@import("std");const expect = std.testing.expect;test"async and await" {// The test block is not async and so cannot have a suspend// point in it. By using the nosuspend keyword, we promise that// the code in amain will finish executing without suspending// back to the test block.nosuspend amain();}fnamain()void {var frame =async func();comptimetry expect(@TypeOf(frame) ==@Frame(func));const ptr:anyframe->void = &frame;const any_ptr:anyframe = ptr;resume any_ptr;await ptr;}fnfunc()void {suspend {}}
$ zig test test.zigTest [1/1] test "async and await"... All 1 tests passed.

Theawait keyword is used to coordinate with an async function'sreturn statement.

await is a suspend point, and takes as an operand anything that coerces toanyframe->T.

There is a common misconception thatawait resumes the target function. It is the other way around: it suspends until the target function completes. In the event that the target function has already completed,await does not suspend; instead it copies the return value directly from the target function's frame.

test.zig

const std =@import("std");const expect = std.testing.expect;var the_frame:anyframe =undefined;var final_result:i32 =0;test"async function await" {    seq('a');    _ =async amain();    seq('f');resume the_frame;    seq('i');try expect(final_result ==1234);try expect(std.mem.eql(u8, &seq_points,"abcdefghi"));}fnamain()void {    seq('b');var f =async another();    seq('e');    final_result =await f;    seq('h');}fnanother()i32 {    seq('c');suspend {        seq('d');        the_frame =@frame();    }    seq('g');return1234;}var seq_points = [_]u8{0} **"abcdefghi".len;var seq_index:usize =0;fnseq(c:u8)void {    seq_points[seq_index] = c;    seq_index +=1;}
$ zig test test.zigTest [1/1] test "async function await"... All 1 tests passed.

In general,suspend is lower level thanawait. Most application code will use onlyasync andawait, but event loop implementations will make use ofsuspend internally.

Async Function Example§

Putting all of this together, here is an example of typicalasync/await usage:

async.zig

const std =@import("std");const Allocator = std.mem.Allocator;pubfnmain()void {    _ =async amainWrap();// Typically we would use an event loop to manage resuming async functions,// but in this example we hard code what the event loop would do,// to make things deterministic.resume global_file_frame;resume global_download_frame;}fnamainWrap()void {    amain()catch |e| {        std.debug.print("{}\n", .{e});if (@errorReturnTrace()) |trace| {            std.debug.dumpStackTrace(trace.*);        }        std.process.exit(1);    };}fnamain() !void {const allocator = std.heap.page_allocator;var download_frame =async fetchUrl(allocator,"https://example.com/");var awaited_download_frame =false;errdeferif (!awaited_download_frame) {if (await download_frame) |r| allocator.free(r)else |_| {}    };var file_frame =async readFile(allocator,"something.txt");var awaited_file_frame =false;errdeferif (!awaited_file_frame) {if (await file_frame) |r| allocator.free(r)else |_| {}    };    awaited_file_frame =true;const file_text =tryawait file_frame;defer allocator.free(file_text);    awaited_download_frame =true;const download_text =tryawait download_frame;defer allocator.free(download_text);    std.debug.print("download_text: {s}\n", .{download_text});    std.debug.print("file_text: {s}\n", .{file_text});}var global_download_frame:anyframe =undefined;fnfetchUrl(allocator: *Allocator, url: []constu8) ![]u8 {const result =try std.mem.dupe(allocator,u8,"this is the downloaded url contents");errdefer allocator.free(result);suspend {        global_download_frame =@frame();    }    std.debug.print("fetchUrl returning\n", .{});return result;}var global_file_frame:anyframe =undefined;fnreadFile(allocator: *Allocator, filename: []constu8) ![]u8 {const result =try std.mem.dupe(allocator,u8,"this is the file contents");errdefer allocator.free(result);suspend {        global_file_frame =@frame();    }    std.debug.print("readFile returning\n", .{});return result;}
$ zig build-exe async.zig$ ./asyncreadFile returningfetchUrl returningdownload_text: this is the downloaded url contentsfile_text: this is the file contents

Now we remove thesuspend andresume code, and observe the same behavior, with one tiny difference:

blocking.zig

const std =@import("std");const Allocator = std.mem.Allocator;pubfnmain()void {    _ =async amainWrap();}fnamainWrap()void {    amain()catch |e| {        std.debug.print("{}\n", .{e});if (@errorReturnTrace()) |trace| {            std.debug.dumpStackTrace(trace.*);        }        std.process.exit(1);    };}fnamain() !void {const allocator = std.heap.page_allocator;var download_frame =async fetchUrl(allocator,"https://example.com/");var awaited_download_frame =false;errdeferif (!awaited_download_frame) {if (await download_frame) |r| allocator.free(r)else |_| {}    };var file_frame =async readFile(allocator,"something.txt");var awaited_file_frame =false;errdeferif (!awaited_file_frame) {if (await file_frame) |r| allocator.free(r)else |_| {}    };    awaited_file_frame =true;const file_text =tryawait file_frame;defer allocator.free(file_text);    awaited_download_frame =true;const download_text =tryawait download_frame;defer allocator.free(download_text);    std.debug.print("download_text: {s}\n", .{download_text});    std.debug.print("file_text: {s}\n", .{file_text});}fnfetchUrl(allocator: *Allocator, url: []constu8) ![]u8 {const result =try std.mem.dupe(allocator,u8,"this is the downloaded url contents");errdefer allocator.free(result);    std.debug.print("fetchUrl returning\n", .{});return result;}fnreadFile(allocator: *Allocator, filename: []constu8) ![]u8 {const result =try std.mem.dupe(allocator,u8,"this is the file contents");errdefer allocator.free(result);    std.debug.print("readFile returning\n", .{});return result;}
$ zig build-exe blocking.zig$ ./blockingfetchUrl returningreadFile returningdownload_text: this is the downloaded url contentsfile_text: this is the file contents

Previously, thefetchUrl andreadFile functions suspended, and were resumed in an order determined by themain function. Now, since there are no suspend points, the order of the printed "... returning" messages is determined by the order ofasync callsites.

Builtin Functions§

Builtin functions are provided by the compiler and are prefixed with@. Thecomptime keyword on a parameter means that the parameter must be known at compile time.

@addWithOverflow§

@addWithOverflow(comptime T:type, a: T, b: T, result: *T)bool

Performsresult.* = a + b. If overflow or underflow occurs, stores the overflowed bits inresult and returnstrue. If no overflow or underflow occurs, returnsfalse.

@alignCast§

@alignCast(comptime alignment:u29, ptr:anytype)anytype

ptr can be*T,fn(),?*T,?fn(), or[]T. It returns the same type asptr except with the alignment adjusted to the new value.

Apointer alignment safety check is added to the generated code to make sure the pointer is aligned as promised.

@alignOf§

@alignOf(comptime T:type)comptime_int

This function returns the number of bytes that this type should be aligned to for the current target to match the C ABI. When the child type of a pointer has this alignment, the alignment can be omitted from the type.

const expect =@import("std").debug.assert;comptime {    assert(*u32 == *align(@alignOf(u32))u32);}

The result is a target-specific compile time constant. It is guaranteed to be less than or equal to@sizeOf(T).

See also:

@as§

@as(comptime T:type, expression) T

PerformsType Coercion. This cast is allowed when the conversion is unambiguous and safe, and is the preferred way to convert between types, whenever possible.

@asyncCall§

@asyncCall(frame_buffer: []align(@alignOf(@Frame(anyAsyncFunction)))u8, result_ptr, function_ptr, args:anytype)anyframe->T

@asyncCall performs anasync call on a function pointer, which may or may not be anasync function.

The providedframe_buffer must be large enough to fit the entire function frame. This size can be determined with@frameSize. To provide a too-small buffer invokes safety-checkedUndefined Behavior.

result_ptr is optional (null may be provided). If provided, the function call will write its result directly to the result pointer, which will be available to read afterawait completes. Any result location provided toawait will copy the result fromresult_ptr.

test.zig

const std =@import("std");const expect = std.testing.expect;test"async fn pointer in a struct field" {var data:i32 =1;const Foo =struct {        bar:fn (*i32)callconv(.Async)void,    };var foo = Foo{ .bar = func };var bytes: [64]u8align(@alignOf(@Frame(func))) =undefined;const f =@asyncCall(&bytes, {}, foo.bar, .{&data});try expect(data ==2);resume f;try expect(data ==4);}fnfunc(y: *i32)void {defer y.* +=2;    y.* +=1;suspend {}}
$ zig test test.zigTest [1/1] test "async fn pointer in a struct field"... All 1 tests passed.

@atomicLoad§

@atomicLoad(comptime T:type, ptr: *const T,comptime ordering: builtin.AtomicOrder) T

This builtin function atomically dereferences a pointer and returns the value.

T must be a pointer, abool, a float, an integer or an enum.

@atomicRmw§

@atomicRmw(comptime T:type, ptr: *T,comptime op: builtin.AtomicRmwOp, operand: T,comptime ordering: builtin.AtomicOrder) T

This builtin function atomically modifies memory and then returns the previous value.

T must be a pointer, abool, a float, an integer or an enum.

Supported operations:

  • .Xchg - stores the operand unmodified. Supports enums, integers and floats.
  • .Add - for integers, twos complement wraparound addition. Also supportsFloats.
  • .Sub - for integers, twos complement wraparound subtraction. Also supportsFloats.
  • .And - bitwise and
  • .Nand - bitwise nand
  • .Or - bitwise or
  • .Xor - bitwise xor
  • .Max - stores the operand if it is larger. Supports integers and floats.
  • .Min - stores the operand if it is smaller. Supports integers and floats.

@atomicStore§

@atomicStore(comptime T:type, ptr: *T, value: T,comptime ordering: builtin.AtomicOrder)void

This builtin function atomically stores a value.

T must be a pointer, abool, a float, an integer or an enum.

@bitCast§

@bitCast(comptime DestType:type, value:anytype) DestType

Converts a value of one type to another type.

Asserts that@sizeOf(@TypeOf(value)) ==@sizeOf(DestType).

Asserts that@typeInfo(DestType) != .Pointer. Use@ptrCast or@intToPtr if you need this.

Can be used for these things for example:

  • Convertf32 tou32 bits
  • Converti32 tou32 preserving twos complement

Works at compile-time ifvalue is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works.

@bitOffsetOf§

@bitOffsetOf(comptime T:type,comptime field_name: []constu8)comptime_int

Returns the bit offset of a field relative to its containing struct.

For nonpacked structs, this will always be divisible by8. For packed structs, non-byte-aligned fields will share a byte offset, but they will have different bit offsets.

See also:

@boolToInt§

@boolToInt(value:bool)u1

Convertstrue tou1(1) andfalse tou1(0).

If the value is known at compile-time, the return type iscomptime_int instead ofu1.

@bitSizeOf§

@bitSizeOf(comptime T:type)comptime_int

This function returns the number of bits it takes to storeT in memory if the type were a field in a packed struct/union. The result is a target-specific compile time constant.

This function measures the size at runtime. For types that are disallowed at runtime, such ascomptime_int andtype, the result is0.

See also:

@breakpoint§

@breakpoint()

This function inserts a platform-specific debug trap instruction which causes debuggers to break there.

This function is only valid within function scope.

@mulAdd§

@mulAdd(comptime T:type, a: T, b: T, c: T) T

Fused multiply add, similar to(a * b) + c, except only rounds once, and is thus more accurate.

Supports Floats and Vectors of floats.

@byteSwap§

@byteSwap(comptime T:type, operand: T) T

T must be an integer type with bit count evenly divisible by 8.

operand may be aninteger orvector.

Swaps the byte order of the integer. This converts a big endian integer to a little endian integer, and converts a little endian integer to a big endian integer.

Note that for the purposes of memory layout with respect to endianness, the integer type should be related to the number of bytes reported by@sizeOf bytes. This is demonstrated withu24.@sizeOf(u24) ==4, which means that au24 stored in memory takes 4 bytes, and those 4 bytes are what are swapped on a little vs big endian system. On the other hand, ifT is specified to beu24, then only 3 bytes are reversed.

@bitReverse§

@bitReverse(comptime T:type, integer: T) T

T accepts any integer type.

Reverses the bitpattern of an integer value, including the sign bit if applicable.

For example 0b10110110 (u8 =182,i8 = -74) becomes 0b01101101 (u8 =109,i8 =109).

@byteOffsetOf§

@byteOffsetOf(comptime T:type,comptime field_name: []constu8)comptime_int

Returns the byte offset of a field relative to its containing struct.

See also:

@call§

@call(options: std.builtin.CallOptions, function:anytype, args:anytype)anytype

Calls a function, in the same way that invoking an expression with parentheses does:

call.zig

const expect =@import("std").testing.expect;test"noinline function call" {try expect(@call(.{}, add, .{3,9}) ==12);}fnadd(a:i32, b:i32)i32 {return a + b;}
$ zig test call.zigTest [1/1] test "noinline function call"... All 1 tests passed.

@call allows more flexibility than normal function call syntax does. TheCallOptions struct is reproduced here:

pubconst CallOptions =struct {    modifier: Modifier = .auto,    stack: ?[]align(std.Target.stack_align)u8 =null,pubconst Modifier =enum {/// Equivalent to function call syntax.        auto,/// Equivalent to async keyword used with function call syntax.        async_kw,/// Prevents tail call optimization. This guarantees that the return/// address will point to the callsite, as opposed to the callsite's/// callsite. If the call is otherwise required to be tail-called/// or inlined, a compile error is emitted instead.        never_tail,/// Guarantees that the call will not be inlined. If the call is/// otherwise required to be inlined, a compile error is emitted instead.        never_inline,/// Asserts that the function call will not suspend. This allows a/// non-async function to call an async function.        no_async,/// Guarantees that the call will be generated with tail call optimization./// If this is not possible, a compile error is emitted instead.        always_tail,/// Guarantees that the call will inlined at the callsite./// If this is not possible, a compile error is emitted instead.        always_inline,/// Evaluates the call at compile-time. If the call cannot be completed at/// compile-time, a compile error is emitted instead.        compile_time,    };};

@cDefine§

@cDefine(comptime name: []u8, value)

This function can only occur inside@cImport.

This appends#define $name $value to the@cImport temporary buffer.

To define without a value, like this:

#define _GNU_SOURCE

Use the void value, like this:

@cDefine("_GNU_SOURCE", {})

See also:

@cImport§

@cImport(expression)type

This function parses C code and imports the functions, types, variables, and compatible macro definitions into a new empty struct type, and then returns that type.

expression is interpreted at compile time. The builtin functions@cInclude,@cDefine, and@cUndef work within this expression, appending to a temporary buffer which is then parsed as C code.

Usually you should only have one@cImport in your entire application, because it saves the compiler from invoking clang multiple times, and prevents inline functions from being duplicated.

Reasons for having multiple@cImport expressions would be:

  • To avoid a symbol collision, for example if foo.h and bar.h both#define CONNECTION_COUNT
  • To analyze the C code with different preprocessor defines

See also:

@cInclude§

@cInclude(comptime path: []u8)

This function can only occur inside@cImport.

This appends#include <$path>\n to thec_import temporary buffer.

See also:

@clz§

@clz(comptime T:type, integer: T)

This function counts the number of most-significant (leading in a big-Endian sense) zeroes ininteger.

Ifinteger is known atcomptime, the return type iscomptime_int. Otherwise, the return type is an unsigned integer with the minimum number of bits that can represent the bit count of the integer type.

Ifinteger is zero,@clz returns the bit width of integer typeT.

See also:

@cmpxchgStrong§

@cmpxchgStrong(comptime T:type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T

This function performs a strong atomic compare exchange operation. It's the equivalent of this code, except atomic:

fncmpxchgStrongButNotAtomic(comptime T:type, ptr: *T, expected_value: T, new_value: T) ?T {const old_value = ptr.*;if (old_value == expected_value) {        ptr.* = new_value;returnnull;    }else {return old_value;    }}

If you are using cmpxchg in a loop,@cmpxchgWeak is the better choice, because it can be implemented more efficiently in machine instructions.

T must be a pointer, abool, a float, an integer or an enum.

@typeInfo(@TypeOf(ptr)).Pointer.alignment must be>=@sizeOf(T).

See also:

@cmpxchgWeak§

@cmpxchgWeak(comptime T:type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T

This function performs a weak atomic compare exchange operation. It's the equivalent of this code, except atomic:

fncmpxchgWeakButNotAtomic(comptime T:type, ptr: *T, expected_value: T, new_value: T) ?T {const old_value = ptr.*;if (old_value == expected_valueand usuallyTrueButSometimesFalse()) {        ptr.* = new_value;returnnull;    }else {return old_value;    }}

If you are using cmpxchg in a loop, the sporadic failure will be no problem, andcmpxchgWeak is the better choice, because it can be implemented more efficiently in machine instructions. However if you need a stronger guarantee, use@cmpxchgStrong.

T must be a pointer, abool, a float, an integer or an enum.

@typeInfo(@TypeOf(ptr)).Pointer.alignment must be>=@sizeOf(T).

See also:

@compileError§

@compileError(comptime msg: []u8)

This function, when semantically analyzed, causes a compile error with the messagemsg.

There are several ways that code avoids being semantically checked, such as usingif orswitch with compile time constants, andcomptime functions.

@compileLog§

@compileLog(args: ...)

This function prints the arguments passed to it at compile-time.

To prevent accidentally leaving compile log statements in a codebase, a compilation error is added to the build, pointing to the compile log statement. This error prevents code from being generated, but does not otherwise interfere with analysis.

This function can be used to do "printf debugging" on compile-time executing code.

test.zig

const print =@import("std").debug.print;const num1 = blk: {var val1:i32 =99;@compileLog("comptime val1 = ", val1);    val1 = val1 +1;break :blk val1;};test"main" {@compileLog("comptime in main");    print("Runtime in main, num1 = {}.\n", .{num1});}
$ zig test test.zig| *"comptime in main"| *"comptime val1 = ", 99./docgen_tmp/test.zig:11:5:error:found compile log statement    @compileLog("comptime in main");^./docgen_tmp/test.zig:1:35:note:referenced hereconst print = @import("std").debug.print;^./docgen_tmp/test.zig:13:5:note:referenced here    print("Runtime in main, num1 = {}.\n", .{num1});^./docgen_tmp/test.zig:5:5:error:found compile log statement    @compileLog("comptime val1 = ", val1);^./docgen_tmp/test.zig:13:46:note:referenced here    print("Runtime in main, num1 = {}.\n", .{num1});^

will ouput:

If all@compileLog calls are removed or not encountered by analysis, the program compiles successfully and the generated executable prints:

test.zig

const print =@import("std").debug.print;const num1 = blk: {var val1:i32 =99;    val1 = val1 +1;break :blk val1;};test"main" {    print("Runtime in main, num1 = {}.\n", .{num1});}
$ zig test test.zigTest [1/1] test "main"... Runtime in main, num1 = 100.All 1 tests passed.

@ctz§

@ctz(comptime T:type, integer: T)

This function counts the number of least-significant (trailing in a big-Endian sense) zeroes ininteger.

Ifinteger is known atcomptime, the return type iscomptime_int. Otherwise, the return type is an unsigned integer with the minimum number of bits that can represent the bit count of the integer type.

Ifinteger is zero,@ctz returns the bit width of integer typeT.

See also:

@cUndef§

@cUndef(comptime name: []u8)

This function can only occur inside@cImport.

This appends#undef $name to the@cImport temporary buffer.

See also:

@divExact§

@divExact(numerator: T, denominator: T) T

Exact division. Caller guaranteesdenominator !=0 and@divTrunc(numerator, denominator) * denominator == numerator.

  • @divExact(6,3) ==2
  • @divExact(a, b) * b == a

For a function that returns a possible error code, use@import("std").math.divExact.

See also:

@divFloor§

@divFloor(numerator: T, denominator: T) T

Floored division. Rounds toward negative infinity. For unsigned integers it is the same asnumerator / denominator. Caller guaranteesdenominator !=0 and!(@typeInfo(T) == .Intand T.is_signedand numerator == std.math.minInt(T)and denominator == -1).

  • @divFloor(-5,3) == -2
  • (@divFloor(a, b) * b) +@mod(a, b) == a

For a function that returns a possible error code, use@import("std").math.divFloor.

See also:

@divTrunc§

@divTrunc(numerator: T, denominator: T) T

Truncated division. Rounds toward zero. For unsigned integers it is the same asnumerator / denominator. Caller guaranteesdenominator !=0 and!(@typeInfo(T) == .Intand T.is_signedand numerator == std.math.minInt(T)and denominator == -1).

  • @divTrunc(-5,3) == -1
  • (@divTrunc(a, b) * b) +@rem(a, b) == a

For a function that returns a possible error code, use@import("std").math.divTrunc.

See also:

@embedFile§

@embedFile(comptime path: []constu8) *const [X:0]u8

This function returns a compile time constant pointer to null-terminated, fixed-size array with length equal to the byte count of the file given bypath. The contents of the array are the contents of the file. This is equivalent to astring literal with the file contents.

path is absolute or relative to the current file, just like@import.

See also:

@enumToInt§

@enumToInt(enum_or_tagged_union:anytype)anytype

Converts an enumeration value into its integer tag type. When a tagged union is passed, the tag value is used as the enumeration value.

If there is only one possible enum value, the resut is acomptime_int known atcomptime.

See also:

@errorName§

@errorName(err:anyerror) []constu8

This function returns the string representation of an error. The string representation oferror.OutOfMem is"OutOfMem".

If there are no calls to@errorName in an entire application, or all calls have a compile-time known value forerr, then no error name table will be generated.

@errorReturnTrace§

@errorReturnTrace() ?*builtin.StackTrace

If the binary is built with error return tracing, and this function is invoked in a function that calls a function with an error or error union return type, returns a stack trace object. Otherwise returnsnull.

@errorToInt§

@errorToInt(err:anytype) std.meta.Int(.unsigned,@sizeOf(anyerror) *8)

Supports the following types:

Converts an error to the integer representation of an error.

It is generally recommended to avoid this cast, as the integer representation of an error is not stable across source code changes.

See also:

@errSetCast§

@errSetCast(comptime T: DestType, value:anytype) DestType

Converts an error value from one error set to another error set. Attempting to convert an error which is not in the destination error set results in safety-protectedUndefined Behavior.

@export§

@export(identifier,comptime options: std.builtin.ExportOptions)void

Creates a symbol in the output object file.

This function can be called from acomptime block to conditionally export symbols. Whenidentifier is a function with the C calling convention andoptions.linkage isStrong, this is equivalent to theexport keyword used on a function:

test.zig

comptime {@export(internalName, .{ .name ="foo", .linkage = .Strong });}fninternalName()callconv(.C)void {}
$ zig build-obj test.zig

This is equivalent to:

test.zig

exportfnfoo()void {}
$ zig build-obj test.zig

Note that even when usingexport,@"foo" syntax can be used to choose any string for the symbol name:

test.zig

exportfn@"A function name that is a complete sentence."()void {}
$ zig build-obj test.zig

When looking at the resulting object, you can see the symbol is used verbatim:

00000000000001f0 T A function name that is a complete sentence.

See also:

@extern§

@extern(T:type,comptime options: std.builtin.ExternOptions) *T

Creates a reference to an external symbol in the output object file.

See also:

@fence§

@fence(order: AtomicOrder)

Thefence function is used to introduce happens-before edges between operations.

AtomicOrder can be found with@import("std").builtin.AtomicOrder.

See also:

@field§

@field(lhs:anytype,comptime field_name: []constu8) (field)

Performs field access by a compile-time string. Works on both fields and declarations.

test.zig

const std =@import("std");const Point =struct {    x:u32,    y:u32,pubvar z:u32 =1;};test"field access by string" {const expect = std.testing.expect;var p = Point{ .x =0, .y =0 };@field(p,"x") =4;@field(p,"y") =@field(p,"x") +1;try expect(@field(p,"x") ==4);try expect(@field(p,"y") ==5);}test"decl access by string" {const expect = std.testing.expect;try expect(@field(Point,"z") ==1);@field(Point,"z") =2;try expect(@field(Point,"z") ==2);}
$ zig test test.zigTest [1/2] test "field access by string"... Test [2/2] test "decl access by string"... All 2 tests passed.

@fieldParentPtr§

@fieldParentPtr(comptime ParentType:type,comptime field_name: []constu8,    field_ptr: *T) *ParentType

Given a pointer to a field, returns the base pointer of a struct.

@floatCast§

@floatCast(comptime DestType:type, value:anytype) DestType

Convert from one float type to another. This cast is safe, but may cause the numeric value to lose precision.

@floatToInt§

@floatToInt(comptime DestType:type, float:anytype) DestType

Converts the integer part of a floating point number to the destination type.

If the integer part of the floating point number cannot fit in the destination type, it invokes safety-checkedUndefined Behavior.

See also:

@frame§

@frame() *@Frame(func)

This function returns a pointer to the frame for a given function. This type can becoerced toanyframe->T and toanyframe, whereT is the return type of the function in scope.

This function does not mark a suspension point, but it does cause the function in scope to become anasync function.

@Frame§

@Frame(func:anytype)type

This function returns the frame type of a function. This works forAsync Functions as well as any function without a specific calling convention.

This type is suitable to be used as the return type ofasync which allows one to, for example, heap-allocate an async function frame:

test.zig

const std =@import("std");test"heap allocated frame" {const frame =try std.heap.page_allocator.create(@Frame(func));    frame.* =async func();}fnfunc()void {suspend {}}
$ zig test test.zigTest [1/1] test "heap allocated frame"... All 1 tests passed.

@frameAddress§

@frameAddress()usize

This function returns the base pointer of the current stack frame.

The implications of this are target specific and not consistent across all platforms. The frame address may not be available in release mode due to aggressive optimizations.

This function is only valid within function scope.

@frameSize§

@frameSize()usize

This is the same as@sizeOf(@Frame(func)), wherefunc may be runtime-known.

This function is typically used in conjunction with@asyncCall.

@hasDecl§

@hasDecl(comptime Container:type,comptime name: []constu8)bool

Returns whether or not astruct,enum, orunion has a declaration matchingname.

test.zig

const std =@import("std");const expect = std.testing.expect;const Foo =struct {    nope:i32,pubvar blah ="xxx";const hi =1;};test"@hasDecl" {try expect(@hasDecl(Foo,"blah"));// Even though `hi` is private, @hasDecl returns true because this test is// in the same file scope as Foo. It would return false if Foo was declared// in a different file.try expect(@hasDecl(Foo,"hi"));// @hasDecl is for declarations; not fields.try expect(!@hasDecl(Foo,"nope"));try expect(!@hasDecl(Foo,"nope1234"));}
$ zig test test.zigTest [1/1] test "@hasDecl"... All 1 tests passed.

See also:

@hasField§

@hasField(comptime Container:type,comptime name: []constu8)bool

Returns whether the field name of a struct, union, or enum exists.

The result is a compile time constant.

It does not include functions, variables, or constants.

See also:

@import§

@import(comptime path: []u8)type

This function finds a zig file corresponding topath and adds it to the build, if it is not already added.

Zig source files are implicitly structs, with a name equal to the file's basename with the extension truncated.@import returns the struct type corresponding to the file.

Declarations which have thepub keyword may be referenced from a different source file than the one they are declared in.

path can be a relative path or it can be the name of a package. If it is a relative path, it is relative to the file that contains the@import function call.

The following packages are always available:

  • @import("std") - Zig Standard Library
  • @import("builtin") - Target-specific information The commandzig build-exe --show-builtin outputs the source to stdout for reference.

See also:

@intCast§

@intCast(comptime DestType:type, int:anytype) DestType

Converts an integer to another integer while keeping the same numerical value. Attempting to convert a number which is out of range of the destination type results in safety-protectedUndefined Behavior.

IfT iscomptime_int, then this is semantically equivalent toType Coercion.

@intToEnum§

@intToEnum(comptime DestType:type, int_value: std.meta.Tag(DestType)) DestType

Converts an integer into anenum value.

Attempting to convert an integer which represents no value in the chosen enum type invokes safety-checkedUndefined Behavior.

See also:

@intToError§

@intToError(value: std.meta.Int(.unsigned,@sizeOf(anyerror) *8))anyerror

Converts from the integer representation of an error intoThe Global Error Set type.

It is generally recommended to avoid this cast, as the integer representation of an error is not stable across source code changes.

Attempting to convert an integer that does not correspond to any error results in safety-protectedUndefined Behavior.

See also:

@intToFloat§

@intToFloat(comptime DestType:type, int:anytype) DestType

Converts an integer to the closest floating point representation. To convert the other way, use@floatToInt. This cast is always safe.

@intToPtr§

@intToPtr(comptime DestType:type, address:usize) DestType

Converts an integer to apointer. To convert the other way, use@ptrToInt.

If the destination pointer type does not allow address zero andaddress is zero, this invokes safety-checkedUndefined Behavior.

@memcpy§

@memcpy(noalias dest: [*]u8,noalias source: [*]constu8, byte_count:usize)

This function copies bytes from one region of memory to another.dest andsource are both pointers and must not overlap.

This function is a low level intrinsic with no safety mechanisms. Most code should not use this function, instead using something like this:

for (source[0..byte_count]) |b, i| dest[i] = b;

The optimizer is intelligent enough to turn the above snippet into a memcpy.

There is also a standard library function for this:

const mem =@import("std").mem;mem.copy(u8, dest[0..byte_count], source[0..byte_count]);

@memset§

@memset(dest: [*]u8, c:u8, byte_count:usize)

This function sets a region of memory toc.dest is a pointer.

This function is a low level intrinsic with no safety mechanisms. Most code should not use this function, instead using something like this:

for (dest[0..byte_count]) |*b| b.* = c;

The optimizer is intelligent enough to turn the above snippet into a memset.

There is also a standard library function for this:

const mem =@import("std").mem;mem.set(u8, dest, c);

@wasmMemorySize§

@wasmMemorySize(index:u32)u32

This function returns the size of the Wasm memory identified byindex as an unsigned value in units of Wasm pages. Note that each Wasm page is 64KB in size.

This function is a low level intrinsic with no safety mechanisms usually useful for allocator designers targeting Wasm. So unless you are writing a new allocator from scratch, you should use something like@import("std").heap.WasmPageAllocator.

See also:

@wasmMemoryGrow§

@wasmMemoryGrow(index:u32, delta:u32)i32

This function increases the size of the Wasm memory identified byindex bydelta in units of unsigned number of Wasm pages. Note that each Wasm page is 64KB in size. On success, returns previous memory size; on failure, if the allocation fails, returns -1.

This function is a low level intrinsic with no safety mechanisms usually useful for allocator designers targeting Wasm. So unless you are writing a new allocator from scratch, you should use something like@import("std").heap.WasmPageAllocator.

test.zig

const std =@import("std");const native_arch =@import("builtin").target.cpu.arch;const expect = std.testing.expect;test"@wasmMemoryGrow" {if (native_arch != .wasm32)returnerror.SkipZigTest;var prev =@wasmMemorySize(0);try expect(prev ==@wasmMemoryGrow(0,1));try expect(prev +1 ==@wasmMemorySize(0));}
$ zig test test.zigTest [1/1] test "@wasmMemoryGrow"... Test [2/1] test "@wasmMemoryGrow"... SKIP0 passed; 1 skipped; 0 failed.

See also:

@mod§

@mod(numerator: T, denominator: T) T

Modulus division. For unsigned integers this is the same asnumerator % denominator. Caller guaranteesdenominator >0.

  • @mod(-5,3) ==1
  • (@divFloor(a, b) * b) +@mod(a, b) == a

For a function that returns an error code, see@import("std").math.mod.

See also:

@mulWithOverflow§

@mulWithOverflow(comptime T:type, a: T, b: T, result: *T)bool

Performsresult.* = a * b. If overflow or underflow occurs, stores the overflowed bits inresult and returnstrue. If no overflow or underflow occurs, returnsfalse.

@panic§

@panic(message: []constu8)noreturn

Invokes the panic handler function. By default the panic handler function calls the publicpanic function exposed in the root source file, or if there is not one specified, thestd.builtin.default_panic function fromstd/builtin.zig.

Generally it is better to use@import("std").debug.panic. However,@panic can be useful for 2 scenarios:

  • From library code, calling the programmer's panic function if they exposed one in the root source file.
  • When mixing C and Zig code, calling the canonical panic implementation across multiple .o files.

See also:

@popCount§

@popCount(comptime T:type, integer: T)

Counts the number of bits set in an integer.

Ifinteger is known atcomptime, the return type iscomptime_int. Otherwise, the return type is an unsigned integer with the minimum number of bits that can represent the bit count of the integer type.

See also:

@ptrCast§

@ptrCast(comptime DestType:type, value:anytype) DestType

Converts a pointer of one type to a pointer of another type.

Optional Pointers are allowed. Casting an optional pointer which isnull to a non-optional pointer invokes safety-checkedUndefined Behavior.

@ptrToInt§

@ptrToInt(value:anytype)usize

Convertsvalue to ausize which is the address of the pointer.value can be one of these types:

  • *T
  • ?*T
  • fn()
  • ?fn()

To convert the other way, use@intToPtr

@rem§

@rem(numerator: T, denominator: T) T

Remainder division. For unsigned integers this is the same asnumerator % denominator. Caller guaranteesdenominator >0.

  • @rem(-5,3) == -2
  • (@divTrunc(a, b) * b) +@rem(a, b) == a

For a function that returns an error code, see@import("std").math.rem.

See also:

@returnAddress§

@returnAddress()usize

This function returns the address of the next machine code instruction that will be executed when the current function returns.

The implications of this are target specific and not consistent across all platforms.

This function is only valid within function scope. If the function gets inlined into a calling function, the returned address will apply to the calling function.

@setAlignStack§

@setAlignStack(comptime alignment:u29)

Ensures that a function will have a stack alignment of at leastalignment bytes.

@setCold§

@setCold(is_cold:bool)

Tells the optimizer that a function is rarely called.

@setEvalBranchQuota§

@setEvalBranchQuota(new_quota:u32)

Changes the maximum number of backwards branches that compile-time code execution can use before giving up and making a compile error.

If thenew_quota is smaller than the default quota (1000) or a previously explicitly set quota, it is ignored.

Example:

test.zig

test"foo" {comptime {var i =0;while (i <1001) : (i +=1) {}    }}
$ zig test test.zig./docgen_tmp/test.zig:4:9:error:evaluation exceeded 1000 backwards branches        while (i < 1001) : (i += 1) {}^./docgen_tmp/test.zig:1:12:note:referenced heretest "foo" {^

Now we use@setEvalBranchQuota:

test.zig

test"foo" {comptime {@setEvalBranchQuota(1001);var i =0;while (i <1001) : (i +=1) {}    }}
$ zig test test.zigTest [1/1] test "foo"... All 1 tests passed.

See also:

@setFloatMode§

@setFloatMode(mode:@import("std").builtin.FloatMode)

Sets the floating point mode of the current scope. Possible values are:

pubconst FloatMode =enum {    Strict,    Optimized,};
  • Strict (default) - Floating point operations follow strict IEEE compliance.
  • Optimized - Floating point operations may do all of the following:
    • Assume the arguments and result are not NaN. Optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.
    • Assume the arguments and result are not +/-Inf. Optimizations are required to retain defined behavior over +/-Inf, but the value of the result is undefined.
    • Treat the sign of a zero argument or result as insignificant.
    • Use the reciprocal of an argument rather than perform division.
    • Perform floating-point contraction (e.g. fusing a multiply followed by an addition into a fused multiply-and-add).
    • Perform algebraically equivalent transformations that may change results in floating point (e.g. reassociate).
    This is equivalent to-ffast-math in GCC.

The floating point mode is inherited by child scopes, and can be overridden in any scope. You can set the floating point mode in a struct or module scope by using a comptime block.

See also:

@setRuntimeSafety§

@setRuntimeSafety(safety_on:bool)void

Sets whether runtime safety checks are enabled for the scope that contains the function call.

test.zig

test"@setRuntimeSafety" {// The builtin applies to the scope that it is called in. So here, integer overflow// will not be caught in ReleaseFast and ReleaseSmall modes:// var x: u8 = 255;// x += 1; // undefined behavior in ReleaseFast/ReleaseSmall modes.    {// However this block has safety enabled, so safety checks happen here,// even in ReleaseFast and ReleaseSmall modes.@setRuntimeSafety(true);var x:u8 =255;        x +=1;        {// The value can be overridden at any scope. So here integer overflow// would not be caught in any build mode.@setRuntimeSafety(false);// var x: u8 = 255;// x += 1; // undefined behavior in all build modes.        }    }}
$ zig test test.zig -OReleaseFastTest [1/1] test "@setRuntimeSafety"... thread 16085 panic: integer overflowerror: the following test command crashed:docgen_tmp/zig-cache/o/5f772ba34b67337c067e5630990ef24e/test /home/andy/Downloads/zig/build-release/zig

Note: it isplanned to replace@setRuntimeSafety with@optimizeFor

@shlExact§

@shlExact(value: T, shift_amt: Log2T) T

Performs the left shift operation (<<). Caller guarantees that the shift will not shift any 1 bits out.

The type ofshift_amt is an unsigned integer withlog2(T.bit_count) bits. This is becauseshift_amt >= T.bit_count is undefined behavior.

See also:

@shlWithOverflow§

@shlWithOverflow(comptime T:type, a: T, shift_amt: Log2T, result: *T)bool

Performsresult.* = a << b. If overflow or underflow occurs, stores the overflowed bits inresult and returnstrue. If no overflow or underflow occurs, returnsfalse.

The type ofshift_amt is an unsigned integer withlog2(T.bit_count) bits. This is becauseshift_amt >= T.bit_count is undefined behavior.

See also:

@shrExact§

@shrExact(value: T, shift_amt: Log2T) T

Performs the right shift operation (>>). Caller guarantees that the shift will not shift any 1 bits out.

The type ofshift_amt is an unsigned integer withlog2(T.bit_count) bits. This is becauseshift_amt >= T.bit_count is undefined behavior.

See also:

@shuffle§

@shuffle(comptime E:type, a: std.meta.Vector(a_len, E), b: std.meta.Vector(b_len, E),comptime mask: std.meta.Vector(mask_len,i32)) std.meta.Vector(mask_len, E)

Constructs a newvector by selecting elements froma andb based onmask.

Each element inmask selects an element from eithera orb. Positive numbers select froma starting at 0. Negative values select fromb, starting at-1 and going down. It is recommended to use the~ operator from indexes fromb so that both indexes can start from0 (i.e.~@as(i32,0) is-1).

For each element ofmask, if it or the selected value froma orb isundefined, then the resulting element isundefined.

a_len andb_len may differ in length. Out-of-bounds element indexes inmask result in compile errors.

Ifa orb isundefined, it is equivalent to a vector of allundefined with the same length as the other vector. If both vectors areundefined,@shuffle returns a vector with all elementsundefined.

E must be aninteger,float,pointer, orbool. The mask may be any vector length, and its length determines the result length.

See also:

@sizeOf§

@sizeOf(comptime T:type)comptime_int

This function returns the number of bytes it takes to storeT in memory. The result is a target-specific compile time constant.

This size may contain padding bytes. If there were two consecutive T in memory, this would be the offset in bytes between element at index 0 and the element at index 1. Forinteger, consider whether you want to use@sizeOf(T) or@typeInfo(T).Int.bits.

This function measures the size at runtime. For types that are disallowed at runtime, such ascomptime_int andtype, the result is0.

See also:

@splat§

@splat(comptime len:u32, scalar:anytype) std.meta.Vector(len,@TypeOf(scalar))

Produces a vector of lengthlen where each element is the valuescalar:

test.zig

const std =@import("std");const expect = std.testing.expect;test"vector @splat" {const scalar:u32 =5;const result =@splat(4, scalar);comptimetry expect(@TypeOf(result) == std.meta.Vector(4,u32));try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{5,5,5,5 }));}
$ zig test test.zigTest [1/1] test "vector @splat"... All 1 tests passed.

scalar must be aninteger,bool,float, orpointer.

See also:

@reduce§

@reduce(comptime op: std.builtin.ReduceOp, value:anytype) std.meta.Child(value)

Transforms avector into a scalar value by performing a sequential horizontal reduction of its elements using the specified operatorop.

Not every operator is available for every vector element type:

  • .And,.Or,.Xor are available forbool vectors,
  • .Min,.Max,.Add,.Mul are available forfloating point vectors,
  • Every operator is available forinteger vectors.

Note that.Add and.Mul reductions on integral types are wrapping; when applied on floating point types the operation associativity is preserved, unless the float mode is set toOptimized.

test.zig

const std =@import("std");const expect = std.testing.expect;test"vector @reduce" {const value: std.meta.Vector(4,i32) = [_]i32{1, -1,1, -1 };const result = value >@splat(4,@as(i32,0));// result is { true, false, true, false };comptimetry expect(@TypeOf(result) == std.meta.Vector(4,bool));const is_all_true =@reduce(.And, result);comptimetry expect(@TypeOf(is_all_true) ==bool);try expect(is_all_true ==false);}
$ zig test test.zigTest [1/1] test "vector @reduce"... All 1 tests passed.

See also:

@src§

@src() std.builtin.SourceLocation

Returns aSourceLocation struct representing the function's name and location in the source code. This must be called in a function.

test.zig

const std =@import("std");const expect = std.testing.expect;test"@src" {try doTheTest();}fndoTheTest() !void {const src =@src();try expect(src.line ==9);try expect(src.column ==17);try expect(std.mem.endsWith(u8, src.fn_name,"doTheTest"));try expect(std.mem.endsWith(u8, src.file,"test.zig"));}
$ zig test test.zigTest [1/1] test "@src"... All 1 tests passed.

@sqrt§

@sqrt(value:anytype)@TypeOf(value)

Performs the square root of a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@sin§

@sin(value:anytype)@TypeOf(value)

Sine trigometric function on a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@cos§

@cos(value:anytype)@TypeOf(value)

Cosine trigometric function on a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@exp§

@exp(value:anytype)@TypeOf(value)

Base-e exponential function on a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@exp2§

@exp2(value:anytype)@TypeOf(value)

Base-2 exponential function on a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@log§

@log(value:anytype)@TypeOf(value)

Returns the natural logarithm of a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@log2§

@log2(value:anytype)@TypeOf(value)

Returns the logarithm to the base 2 of a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@log10§

@log10(value:anytype)@TypeOf(value)

Returns the logarithm to the base 10 of a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@fabs§

@fabs(value:anytype)@TypeOf(value)

Returns the absolute value of a floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@floor§

@floor(value:anytype)@TypeOf(value)

Returns the largest integral value not greater than the given floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@ceil§

@ceil(value:anytype)@TypeOf(value)

Returns the largest integral value not less than the given floating point number. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@trunc§

@trunc(value:anytype)@TypeOf(value)

Rounds the given floating point number to an integer, towards zero. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@round§

@round(value:anytype)@TypeOf(value)

Rounds the given floating point number to an integer, away from zero. Uses a dedicated hardware instruction when available.

SupportsFloats andVectors of floats, with the caveat thatsome float operations are not yet implemented for all float types.

@subWithOverflow§

@subWithOverflow(comptime T:type, a: T, b: T, result: *T)bool

Performsresult.* = a - b. If overflow or underflow occurs, stores the overflowed bits inresult and returnstrue. If no overflow or underflow occurs, returnsfalse.

@tagName§

@tagName(value:anytype) []constu8

Converts an enum value or union value to a slice of bytes representing the name.

If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checkedUndefined Behavior.

@This§

@This()type

Returns the innermost struct, enum, or union that this function call is inside. This can be useful for an anonymous struct that needs to refer to itself:

test.zig

const std =@import("std");const expect = std.testing.expect;test"@This()" {var items = [_]i32{1,2,3,4 };const list = List(i32){ .items = items[0..] };try expect(list.length() ==4);}fnList(comptime T:type)type {returnstruct {const Self =@This();        items: []T,fnlength(self: Self)usize {return self.items.len;        }    };}
$ zig test test.zigTest [1/1] test "@This()"... All 1 tests passed.

When@This() is used at file scope, it returns a reference to the struct that corresponds to the current file.

@truncate§

@truncate(comptime T:type, integer:anytype) T

This function truncates bits from an integer type, resulting in a smaller or same-sized integer type.

The following produces safety-checkedUndefined Behavior:

test.zig

test"integer cast panic" {var a:u16 =0xabcd;var b:u8 =@intCast(u8, a);}
$ zig test test.zigTest [1/1] test "integer cast panic"... thread 16275 panic: integer cast truncated bits/home/andy/Downloads/zig/docgen_tmp/test.zig:3:17:0x206715 in test "integer cast panic" (test)    var b: u8 = @intCast(u8, a);^/home/andy/Downloads/zig/lib/std/special/test_runner.zig:76:28:0x22cffa in std.special.main (test)        } else test_fn.func();^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x22555a in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2093de in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x208406 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x208212 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^error: the following test command crashed:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

However this is well defined and working code:

truncate.zig

const std =@import("std");const expect = std.testing.expect;test"integer truncation" {var a:u16 =0xabcd;var b:u8 =@truncate(u8, a);try expect(b ==0xcd);}
$ zig test truncate.zigTest [1/1] test "integer truncation"... All 1 tests passed.

This function always truncates the significant bits of the integer, regardless of endianness on the target platform.

@Type§

@Type(comptime info: std.builtin.TypeInfo)type

This function is the inverse of@typeInfo. It reifies type information into atype.

It is available for the following types:

For these types,@Type is not available:

@typeInfo§

@typeInfo(comptime T:type) std.builtin.TypeInfo

Provides type reflection.

Forstructs,unions,enums, anderror sets, the fields are guaranteed to be in the same order as declared. For declarations, the order is unspecified.

@typeName§

@typeName(T:type) [N]u8

This function returns the string representation of a type, as an array. It is equivalent to a string literal of the type name.

@TypeOf§

@TypeOf(...)type

@TypeOf is a special builtin function that takes any (nonzero) number of expressions as parameters and returns the type of the result, usingPeer Type Resolution.

The expressions are evaluated, however they are guaranteed to have noruntime side-effects:

test.zig

const std =@import("std");const expect = std.testing.expect;test"no runtime side effects" {var data:i32 =0;const T =@TypeOf(foo(i32, &data));comptimetry expect(T ==i32);try expect(data ==0);}fnfoo(comptime T:type, ptr: *T) T {    ptr.* +=1;return ptr.*;}
$ zig test test.zigTest [1/1] test "no runtime side effects"... All 1 tests passed.

@unionInit§

@unionInit(comptime Union:type,comptime active_field_name: []constu8, init_expr) Union

This is the same thing asunion initialization syntax, except that the field name is acomptime-known value rather than an identifier token.

@unionInit forwards itsresult location toinit_expr.

Build Mode§

Zig has four build modes:

To add standard build options to abuild.zig file:

const Builder =@import("std").build.Builder;pubfnbuild(b: *Builder)void {const exe = b.addExecutable("example","example.zig");    exe.setBuildMode(b.standardReleaseOptions());    b.default_step.dependOn(&exe.step);}

This causes these options to be available:

  -Drelease-safe=[bool] optimizations on and safety on  -Drelease-fast=[bool] optimizations on and safety off  -Drelease-small=[bool] size optimizations on and safety off

Debug§

$ zig build-exe example.zig
  • Fast compilation speed
  • Safety checks enabled
  • Slow runtime performance
  • Large binary size
  • No reproducible build requirement

ReleaseFast§

$ zig build-exe example.zig -O ReleaseFast
  • Fast runtime performance
  • Safety checks disabled
  • Slow compilation speed
  • Large binary size
  • Reproducible build

ReleaseSafe§

$ zig build-exe example.zig -O ReleaseSafe
  • Medium runtime performance
  • Safety checks enabled
  • Slow compilation speed
  • Large binary size
  • Reproducible build

ReleaseSmall§

$ zig build-exe example.zig -O ReleaseSmall
  • Medium runtime performance
  • Safety checks disabled
  • Slow compilation speed
  • Small binary size
  • Reproducible build

See also:

Single Threaded Builds§

Zig has a compile option--single-threaded which has the following effects:

  • AllThread Local Variables are treated as regularContainer Level Variables.
  • The overhead ofAsync Functions becomes equivalent to function call overhead.
  • The@import("builtin").single_threaded becomestrue and therefore various userland APIs which read this variable become more efficient. For examplestd.Mutex becomes an empty data structure and all of its functions become no-ops.

Undefined Behavior§

Zig has many instances of undefined behavior. If undefined behavior is detected at compile-time, Zig emits a compile error and refuses to continue. Most undefined behavior that cannot be detected at compile-time can be detected at runtime. In these cases, Zig has safety checks. Safety checks can be disabled on a per-block basis with@setRuntimeSafety. TheReleaseFast andReleaseSmall build modes disable all safety checks (except where overridden by@setRuntimeSafety) in order to facilitate optimizations.

When a safety check fails, Zig crashes with a stack trace, like this:

test.zig

test"safety check" {unreachable;}
$ zig test test.zigTest [1/1] test "safety check"... thread 16425 panic: reached unreachable code/home/andy/Downloads/zig/docgen_tmp/test.zig:2:5:0x2066ea in test "safety check" (test)    unreachable;^/home/andy/Downloads/zig/lib/std/special/test_runner.zig:76:28:0x22cfca in std.special.main (test)        } else test_fn.func();^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x22552a in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2093ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2083d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x2081e2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^error: the following test command crashed:docgen_tmp/zig-cache/o/333a8741816702dafd0ff99af380ebb1/test /home/andy/Downloads/zig/build-release/zig

Reaching Unreachable Code§

At compile-time:

test.zig

comptime {    assert(false);}fnassert(ok:bool)void {if (!ok)unreachable;// assertion failure}
$ zig test test.zig./docgen_tmp/test.zig:5:14:error:reached unreachable code    if (!ok) unreachable; // assertion failure^./docgen_tmp/test.zig:2:11:note:called from here    assert(false);^./docgen_tmp/test.zig:1:10:note:called from herecomptime {^./docgen_tmp/test.zig:2:11:note:referenced here    assert(false);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {    std.debug.assert(false);}
$ zig build-exe test.zig$ ./testthread 16491 panic: reached unreachable code/home/andy/Downloads/zig/lib/std/debug.zig:226:14:0x2042cb in std.debug.assert (test)    if (!ok) unreachable; // assertion failure^/home/andy/Downloads/zig/docgen_tmp/test.zig:4:21:0x229eea in main (test)    std.debug.assert(false);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x2222cc in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20615e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205186 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204f92 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Index out of Bounds§

At compile-time:

test.zig

comptime {const array: [5]u8 ="hello".*;const garbage = array[5];}
$ zig test test.zig./docgen_tmp/test.zig:3:26:error:index 5 outside array of size 5    const garbage = array[5];^

At runtime:

test.zig

pubfnmain()void {var x = foo("hello");}fnfoo(x: []constu8)u8 {return x[5];}
$ zig build-exe test.zig$ ./testthread 16553 panic: index out of bounds/home/andy/Downloads/zig/docgen_tmp/test.zig:6:13:0x231179 in foo (test)    return x[5];^/home/andy/Downloads/zig/docgen_tmp/test.zig:2:16:0x229f06 in main (test)    var x = foo("hello");^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x2222dc in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20616e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205196 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fa2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Cast Negative Number to Unsigned Integer§

At compile-time:

test.zig

comptime {const value:i32 = -1;const unsigned =@intCast(u32, value);}
$ zig test test.zig./docgen_tmp/test.zig:3:22:error:attempt to cast negative value to unsigned integer    const unsigned = @intCast(u32, value);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var value:i32 = -1;var unsigned =@intCast(u32, value);    std.debug.print("value: {}\n", .{unsigned});}
$ zig build-exe test.zig$ ./testthread 16614 panic: attempt to cast negative value to unsigned integer/home/andy/Downloads/zig/docgen_tmp/test.zig:5:20:0x229f77 in main (test)    var unsigned = @intCast(u32, value);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22231c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fe2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

To obtain the maximum value of an unsigned integer, usestd.math.maxInt.

Cast Truncates Data§

At compile-time:

test.zig

comptime {const spartan_count:u16 =300;const byte =@intCast(u8, spartan_count);}
$ zig test test.zig./docgen_tmp/test.zig:3:18:error:cast from 'u16' to 'u8' truncates bits    const byte = @intCast(u8, spartan_count);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var spartan_count:u16 =300;const byte =@intCast(u8, spartan_count);    std.debug.print("value: {}\n", .{byte});}
$ zig build-exe test.zig$ ./testthread 16677 panic: integer cast truncated bits/home/andy/Downloads/zig/docgen_tmp/test.zig:5:18:0x229f7c in main (test)    const byte = @intCast(u8, spartan_count);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22231c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fe2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

To truncate bits, use@truncate.

Integer Overflow§

Default Operations§

The following operators can cause integer overflow:

Example with addition at compile-time:

test.zig

comptime {var byte:u8 =255;    byte +=1;}
$ zig test test.zig./docgen_tmp/test.zig:3:10:error:operation caused overflow    byte += 1;^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var byte:u8 =255;    byte +=1;    std.debug.print("value: {}\n", .{byte});}
$ zig build-exe test.zig$ ./testthread 16739 panic: integer overflow/home/andy/Downloads/zig/docgen_tmp/test.zig:5:10:0x229f5f in main (test)    byte += 1;^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22231c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fe2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Standard Library Math Functions§

These functions provided by the standard library return possible errors.

  • @import("std").math.add
  • @import("std").math.sub
  • @import("std").math.mul
  • @import("std").math.divTrunc
  • @import("std").math.divFloor
  • @import("std").math.divExact
  • @import("std").math.shl

Example of catching an overflow for addition:

test.zig

const math =@import("std").math;const print =@import("std").debug.print;pubfnmain() !void {var byte:u8 =255;    byte =if (math.add(u8, byte,1)) |result| resultelse |err| {        print("unable to add one: {s}\n", .{@errorName(err)});return err;    };    print("result: {}\n", .{byte});}
$ zig build-exe test.zig$ ./testunable to add one: Overflowerror: Overflow/home/andy/Downloads/zig/lib/std/math.zig:449:5:0x2315ab in std.math.add (test)    return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer;^/home/andy/Downloads/zig/docgen_tmp/test.zig:8:9:0x22a244 in main (test)        return err;^

Builtin Overflow Functions§

These builtins return abool of whether or not overflow occurred, as well as returning the overflowed bits:

Example of@addWithOverflow:

test.zig

const print =@import("std").debug.print;pubfnmain()void {var byte:u8 =255;var result:u8 =undefined;if (@addWithOverflow(u8, byte,10, &result)) {        print("overflowed result: {}\n", .{result});    }else {        print("result: {}\n", .{result});    }}
$ zig build-exe test.zig$ ./testoverflowed result: 9

Wrapping Operations§

These operations have guaranteed wraparound semantics.

  • +% (wraparound addition)
  • -% (wraparound subtraction)
  • -% (wraparound negation)
  • *% (wraparound multiplication)

test.zig

const std =@import("std");const expect = std.testing.expect;const minInt = std.math.minInt;const maxInt = std.math.maxInt;test"wraparound addition and subtraction" {const x:i32 = maxInt(i32);const min_val = x +%1;try expect(min_val == minInt(i32));const max_val = min_val -%1;try expect(max_val == maxInt(i32));}
$ zig test test.zigTest [1/1] test "wraparound addition and subtraction"... All 1 tests passed.

Exact Left Shift Overflow§

At compile-time:

test.zig

comptime {const x =@shlExact(@as(u8,0b01010101),2);}
$ zig test test.zig./docgen_tmp/test.zig:2:15:error:operation caused overflow    const x = @shlExact(@as(u8, 0b01010101), 2);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var x:u8 =0b01010101;var y =@shlExact(x,2);    std.debug.print("value: {}\n", .{y});}
$ zig build-exe test.zig$ ./testthread 16945 panic: left shift overflowed bits/home/andy/Downloads/zig/docgen_tmp/test.zig:5:13:0x229f9b in main (test)    var y = @shlExact(x, 2);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22233c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ce in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051f6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205002 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Exact Right Shift Overflow§

At compile-time:

test.zig

comptime {const x =@shrExact(@as(u8,0b10101010),2);}
$ zig test test.zig./docgen_tmp/test.zig:2:15:error:exact shift shifted out 1 bits    const x = @shrExact(@as(u8, 0b10101010), 2);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var x:u8 =0b10101010;var y =@shrExact(x,2);    std.debug.print("value: {}\n", .{y});}
$ zig build-exe test.zig$ ./testthread 17005 panic: right shift overflowed bits/home/andy/Downloads/zig/docgen_tmp/test.zig:5:13:0x229f9b in main (test)    var y = @shrExact(x, 2);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22233c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ce in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051f6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205002 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Division by Zero§

At compile-time:

test.zig

comptime {const a:i32 =1;const b:i32 =0;const c = a / b;}
$ zig test test.zig./docgen_tmp/test.zig:4:17:error:division by zero    const c = a / b;^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var a:u32 =1;var b:u32 =0;var c = a / b;    std.debug.print("value: {}\n", .{c});}
$ zig build-exe test.zig$ ./testthread 17071 panic: division by zero/home/andy/Downloads/zig/docgen_tmp/test.zig:6:15:0x229f69 in main (test)    var c = a / b;^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22231c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fe2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Remainder Division by Zero§

At compile-time:

test.zig

comptime {const a:i32 =10;const b:i32 =0;const c = a % b;}
$ zig test test.zig./docgen_tmp/test.zig:4:17:error:division by zero    const c = a % b;^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var a:u32 =10;var b:u32 =0;var c = a % b;    std.debug.print("value: {}\n", .{c});}
$ zig build-exe test.zig$ ./testthread 17131 panic: remainder division by zero or negative value/home/andy/Downloads/zig/docgen_tmp/test.zig:6:15:0x229f8b in main (test)    var c = a % b;^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22231c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fe2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Exact Division Remainder§

At compile-time:

test.zig

comptime {const a:u32 =10;const b:u32 =3;const c =@divExact(a, b);}
$ zig test test.zig./docgen_tmp/test.zig:4:15:error:exact division had a remainder    const c = @divExact(a, b);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var a:u32 =10;var b:u32 =3;var c =@divExact(a, b);    std.debug.print("value: {}\n", .{c});}
$ zig build-exe test.zig$ ./testthread 17193 panic: exact division produced remainder/home/andy/Downloads/zig/docgen_tmp/test.zig:6:13:0x229fad in main (test)    var c = @divExact(a, b);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22231c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ae in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051d6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204fe2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Attempt to Unwrap Null§

At compile-time:

test.zig

comptime {const optional_number: ?i32 =null;const number = optional_number.?;}
$ zig test test.zig./docgen_tmp/test.zig:3:35:error:unable to unwrap null    const number = optional_number.?;^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var optional_number: ?i32 =null;var number = optional_number.?;    std.debug.print("value: {}\n", .{number});}
$ zig build-exe test.zig$ ./testthread 17271 panic: attempt to use null value/home/andy/Downloads/zig/docgen_tmp/test.zig:5:33:0x229f6c in main (test)    var number = optional_number.?;^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22232c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061be in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051e6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204ff2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

One way to avoid this crash is to test for null instead of assuming non-null, with theif expression:

test.zig

const print =@import("std").debug.print;pubfnmain()void {const optional_number: ?i32 =null;if (optional_number) |number| {        print("got number: {}\n", .{number});    }else {        print("it's null\n", .{});    }}
$ zig build-exe test.zig$ ./testit's null

See also:

Attempt to Unwrap Error§

At compile-time:

test.zig

comptime {const number = getNumberOrFail()catchunreachable;}fngetNumberOrFail() !i32 {returnerror.UnableToReturnNumber;}
$ zig test test.zig./docgen_tmp/test.zig:2:38:error:caught unexpected error 'UnableToReturnNumber'    const number = getNumberOrFail() catch unreachable;^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {const number = getNumberOrFail()catchunreachable;    std.debug.print("value: {}\n", .{number});}fngetNumberOrFail() !i32 {returnerror.UnableToReturnNumber;}
$ zig build-exe test.zig$ ./testthread 17368 panic: attempt to unwrap error: UnableToReturnNumber/home/andy/Downloads/zig/docgen_tmp/test.zig:9:5:0x231254 in getNumberOrFail (test)    return error.UnableToReturnNumber;^???:?:?:0x20c682 in ??? (???)/home/andy/Downloads/zig/docgen_tmp/test.zig:4:38:0x229fbb in main (test)    const number = getNumberOrFail() catch unreachable;^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22233c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061ce in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2051f6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205002 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

One way to avoid this crash is to test for an error instead of assuming a successful result, with theif expression:

test.zig

const print =@import("std").debug.print;pubfnmain()void {const result = getNumberOrFail();if (result) |number| {        print("got number: {}\n", .{number});    }else |err| {        print("got error: {s}\n", .{@errorName(err)});    }}fngetNumberOrFail() !i32 {returnerror.UnableToReturnNumber;}
$ zig build-exe test.zig$ ./testgot error: UnableToReturnNumber

See also:

Invalid Error Code§

At compile-time:

test.zig

comptime {const err =error.AnError;const number =@errorToInt(err) +10;const invalid_err =@intToError(number);}
$ zig test test.zig./docgen_tmp/test.zig:4:25:error:integer value 11 represents no error    const invalid_err = @intToError(number);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var err =error.AnError;var number =@errorToInt(err) +500;var invalid_err =@intToError(number);    std.debug.print("value: {}\n", .{number});}
$ zig build-exe test.zig$ ./testthread 17469 panic: invalid error code/home/andy/Downloads/zig/docgen_tmp/test.zig:6:23:0x229fc3 in main (test)    var invalid_err = @intToError(number);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22234c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x2061de in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205206 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205012 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Invalid Enum Cast§

At compile-time:

test.zig

const Foo =enum {    a,    b,    c,};comptime {const a:u2 =3;const b =@intToEnum(Foo, a);}
$ zig test test.zig./docgen_tmp/test.zig:8:15:error:enum 'Foo' has no tag matching integer value 3    const b = @intToEnum(Foo, a);^./docgen_tmp/test.zig:1:13:note:'Foo' declared hereconst Foo = enum {^

At runtime:

test.zig

const std =@import("std");const Foo =enum {    a,    b,    c,};pubfnmain()void {var a:u2 =3;var b =@intToEnum(Foo, a);    std.debug.print("value: {s}\n", .{@tagName(b)});}
$ zig build-exe test.zig$ ./testthread 17530 panic: invalid enum value/home/andy/Downloads/zig/docgen_tmp/test.zig:11:13:0x229ff9 in main (test)    var b = @intToEnum(Foo, a);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x2223ac in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20623e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205266 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205072 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Invalid Error Set Cast§

At compile-time:

test.zig

const Set1 =error{    A,    B,};const Set2 =error{    A,    C,};comptime {    _ =@errSetCast(Set2, Set1.B);}
$ zig test test.zig./docgen_tmp/test.zig:10:9:error:error.B not a member of error set 'Set2'    _ = @errSetCast(Set2, Set1.B);^

At runtime:

test.zig

const std =@import("std");const Set1 =error{    A,    B,};const Set2 =error{    A,    C,};pubfnmain()void {    foo(Set1.B);}fnfoo(set1: Set1)void {const x =@errSetCast(Set2, set1);    std.debug.print("value: {}\n", .{x});}
$ zig build-exe test.zig$ ./testthread 17592 panic: invalid error code/home/andy/Downloads/zig/docgen_tmp/test.zig:15:15:0x231256 in foo (test)    const x = @errSetCast(Set2, set1);^/home/andy/Downloads/zig/docgen_tmp/test.zig:12:8:0x229fbd in main (test)    foo(Set1.B);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22239c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20622e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205256 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205062 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Incorrect Pointer Alignment§

At compile-time:

test.zig

comptime {const ptr =@intToPtr(*align(1)i32,0x1);const aligned =@alignCast(4, ptr);}
$ zig test test.zig./docgen_tmp/test.zig:3:35:error:pointer address 0x1 is not aligned to 4 bytes    const aligned = @alignCast(4, ptr);^./docgen_tmp/test.zig:3:21:note:referenced here    const aligned = @alignCast(4, ptr);^

At runtime:

test.zig

const mem =@import("std").mem;pubfnmain() !void {var arrayalign(4) = [_]u32{0x11111111,0x11111111 };const bytes = mem.sliceAsBytes(array[0..]);if (foo(bytes) !=0x11111111)returnerror.Wrong;}fnfoo(bytes: []u8)u32 {const slice4 = bytes[1..5];const int_slice = mem.bytesAsSlice(u32,@alignCast(4, slice4));return int_slice[0];}
$ zig build-exe test.zig$ ./testthread 17652 panic: incorrect alignment/home/andy/Downloads/zig/docgen_tmp/test.zig:9:59:0x2314dc in foo (test)    const int_slice = mem.bytesAsSlice(u32, @alignCast(4, slice4));^/home/andy/Downloads/zig/docgen_tmp/test.zig:5:12:0x22a135 in main (test)    if (foo(bytes) != 0x11111111) return error.Wrong;^/home/andy/Downloads/zig/lib/std/start.zig:471:37:0x2223ba in std.start.callMain (test)            const result = root.main() catch |err| {^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20621e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205246 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x205052 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Wrong Union Field Access§

At compile-time:

test.zig

comptime {var f = Foo{ .int =42 };    f.float =12.34;}const Foo =union {    float:f32,    int:u32,};
$ zig test test.zig./docgen_tmp/test.zig:3:6:error:accessing union field 'float' while field 'int' is set    f.float = 12.34;^

At runtime:

test.zig

const std =@import("std");const Foo =union {    float:f32,    int:u32,};pubfnmain()void {var f = Foo{ .int =42 };    bar(&f);}fnbar(f: *Foo)void {    f.float =12.34;    std.debug.print("value: {}\n", .{f.float});}
$ zig build-exe test.zig$ ./testthread 17714 panic: access of inactive union field/home/andy/Downloads/zig/docgen_tmp/test.zig:14:6:0x23e9da in bar (test)    f.float = 12.34;^/home/andy/Downloads/zig/docgen_tmp/test.zig:10:8:0x23773c in main (test)    bar(&f);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x22fb0c in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x21399e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x2129c6 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x2127d2 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

This safety is not available forextern orpacked unions.

To change the active field of a union, assign the entire union, like this:

test.zig

const std =@import("std");const Foo =union {    float:f32,    int:u32,};pubfnmain()void {var f = Foo{ .int =42 };    bar(&f);}fnbar(f: *Foo)void {    f.* = Foo{ .float =12.34 };    std.debug.print("value: {}\n", .{f.float});}
$ zig build-exe test.zig$ ./testvalue: 1.23400001e+01

To change the active field of a union when a meaningful value for the field is not known, useundefined, like this:

test.zig

const std =@import("std");const Foo =union {    float:f32,    int:u32,};pubfnmain()void {var f = Foo{ .int =42 };    f = Foo{ .float =undefined };    bar(&f);    std.debug.print("value: {}\n", .{f.float});}fnbar(f: *Foo)void {    f.float =12.34;}
$ zig build-exe test.zig$ ./testvalue: 1.23400001e+01

See also:

Out of Bounds Float to Integer Cast§

TODO

Pointer Cast Invalid Null§

This happens when casting a pointer with the address 0 to a pointer which may not have the address 0. For example,C Pointers,Optional Pointers, andallowzero pointers allow address zero, but normalPointers do not.

At compile-time:

test.zig

comptime {const opt_ptr: ?*i32 =null;const ptr =@ptrCast(*i32, opt_ptr);}
$ zig test test.zig./docgen_tmp/test.zig:3:17:error:null pointer casted to type '*i32'    const ptr = @ptrCast(*i32, opt_ptr);^

At runtime:

test.zig

pubfnmain()void {var opt_ptr: ?*i32 =null;var ptr =@ptrCast(*i32, opt_ptr);}
$ zig build-exe test.zig$ ./testthread 17846 panic: cast causes pointer to be null/home/andy/Downloads/zig/docgen_tmp/test.zig:3:15:0x229f10 in main (test)    var ptr = @ptrCast(*i32, opt_ptr);^/home/andy/Downloads/zig/lib/std/start.zig:461:22:0x2222cc in std.start.callMain (test)            root.main();^/home/andy/Downloads/zig/lib/std/start.zig:413:12:0x20615e in std.start.callMainWithArgs (test)    return @call(.{ .modifier = .always_inline }, callMain, .{});^/home/andy/Downloads/zig/lib/std/start.zig:332:17:0x205186 in std.start.posixCallMainAndExit (test)    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));^/home/andy/Downloads/zig/lib/std/start.zig:245:5:0x204f92 in std.start._start (test)    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});^(process terminated by signal)

Memory§

The Zig language performs no memory management on behalf of the programmer. This is why Zig has no runtime, and why Zig code works seamlessly in so many environments, including real-time software, operating system kernels, embedded devices, and low latency servers. As a consequence, Zig programmers must always be able to answer the question:

Where are the bytes?

Like Zig, the C programming language has manual memory management. However, unlike Zig, C has a default allocator -malloc,realloc, andfree. When linking against libc, Zig exposes this allocator withstd.heap.c_allocator. However, by convention, there is no default allocator in Zig. Instead, functions which need to allocate accept an*Allocator parameter. Likewise, data structures such asstd.ArrayList accept an*Allocator parameter in their initialization functions:

allocator.zig

const std =@import("std");const Allocator = std.mem.Allocator;const expect = std.testing.expect;test"using an allocator" {var buffer: [100]u8 =undefined;const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator;const result =try concat(allocator,"foo","bar");try expect(std.mem.eql(u8,"foobar", result));}fnconcat(allocator: *Allocator, a: []constu8, b: []constu8) ![]u8 {const result =try allocator.alloc(u8, a.len + b.len);    std.mem.copy(u8, result, a);    std.mem.copy(u8, result[a.len..], b);return result;}
$ zig test allocator.zigTest [1/1] test "using an allocator"... All 1 tests passed.

In the above example, 100 bytes of stack memory are used to initialize aFixedBufferAllocator, which is then passed to a function. As a convenience there is a globalFixedBufferAllocator available for quick tests atstd.testing.allocator, which will also do perform basic leak detection.

Zig has a general purpose allocator available to be imported withstd.heap.GeneralPurposeAllocator. However, it is still recommended to follow theChoosing an Allocator guide.

Choosing an Allocator§

What allocator to use depends on a number of factors. Here is a flow chart to help you decide:

  1. Are you making a library? In this case, best to accept an*Allocator as a parameter and allow your library's users to decide what allocator to use.
  2. Are you linking libc? In this case,std.heap.c_allocator is likely the right choice, at least for your main allocator.
  3. Is the maximum number of bytes that you will need bounded by a number known atcomptime? In this case, usestd.heap.FixedBufferAllocator orstd.heap.ThreadSafeFixedBufferAllocator depending on whether you need thread-safety or not.
  4. Is your program a command line application which runs from start to end without any fundamental cyclical pattern (such as a video game main loop, or a web server request handler), such that it would make sense to free everything at once at the end? In this case, it is recommended to follow this pattern:

    cli_allocation.zig

    const std =@import("std");pubfnmain() !void {var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);defer arena.deinit();const allocator = &arena.allocator;const ptr =try allocator.create(i32);    std.debug.print("ptr={*}\n", .{ptr});}
    $ zig build-exe cli_allocation.zig$ ./cli_allocationptr=i32@7f00d058c018
    When using this kind of allocator, there is no need to free anything manually. Everything gets freed at once with the call toarena.deinit().
  5. Are the allocations part of a cyclical pattern such as a video game main loop, or a web server request handler? If the allocations can all be freed at once, at the end of the cycle, for example once the video game frame has been fully rendered, or the web server request has been served, thenstd.heap.ArenaAllocator is a great candidate. As demonstrated in the previous bullet point, this allows you to free entire arenas at once. Note also that if an upper bound of memory can be established, thenstd.heap.FixedBufferAllocator can be used as a further optimization.
  6. Are you writing a test, and you want to make sureerror.OutOfMemory is handled correctly? In this case, usestd.testing.FailingAllocator.
  7. Are you writing a test? In this case, usestd.testing.allocator.
  8. Finally, if none of the above apply, you need a general purpose allocator. Zig's general purpose allocator is available as a function that takes acomptimestruct of configuration options and returns a type. Generally, you will set up onestd.heap.GeneralPurposeAllocator in your main function, and then pass it or sub-allocators around to various parts of your application.
  9. You can also considerImplementing an Allocator.

Where are the bytes?§

String literals such as"foo" are in the global constant data section. This is why it is an error to pass a string literal to a mutable slice, like this:

test.zig

fnfoo(s: []u8)void {}test"string literal to mutable slice" {    foo("hello");}
$ zig test test.zig./docgen_tmp/test.zig:4:9:error:expected type '[]u8', found '*const [5:0]u8'    foo("hello");^

However if you make the slice constant, then it works:

strlit.zig

fnfoo(s: []constu8)void {}test"string literal to constant slice" {    foo("hello");}
$ zig test strlit.zigTest [1/1] test "string literal to constant slice"... All 1 tests passed.

Just like string literals,const declarations, when the value is known atcomptime, are stored in the global constant data section. AlsoCompile Time Variables are stored in the global constant data section.

var declarations inside functions are stored in the function's stack frame. Once a function returns, anyPointers to variables in the function's stack frame become invalid references, and dereferencing them becomes uncheckedUndefined Behavior.

var declarations at the top level or instruct declarations are stored in the global data section.

The location of memory allocated withallocator.alloc orallocator.create is determined by the allocator's implementation.

TODO: thread local variables

Implementing an Allocator§

Zig programmers can implement their own allocators by fulfilling the Allocator interface. In order to do this one must read carefully the documentation comments in std/mem.zig and then supply areallocFn and ashrinkFn.

There are many example allocators to look at for inspiration. Look at std/heap.zig andstd.heap.GeneralPurposeAllocator.

Heap Allocation Failure§

Many programming languages choose to handle the possibility of heap allocation failure by unconditionally crashing. By convention, Zig programmers do not consider this to be a satisfactory solution. Instead,error.OutOfMemory represents heap allocation failure, and Zig libraries return this error code whenever heap allocation failure prevented an operation from completing successfully.

Some have argued that because some operating systems such as Linux have memory overcommit enabled by default, it is pointless to handle heap allocation failure. There are many problems with this reasoning:

  • Only some operating systems have an overcommit feature.
    • Linux has it enabled by default, but it is configurable.
    • Windows does not overcommit.
    • Embedded systems do not have overcommit.
    • Hobby operating systems may or may not have overcommit.
  • For real-time systems, not only is there no overcommit, but typically the maximum amount of memory per application is determined ahead of time.
  • When writing a library, one of the main goals is code reuse. By making code handle allocation failure correctly, a library becomes eligible to be reused in more contexts.
  • Although some software has grown to depend on overcommit being enabled, its existence is the source of countless user experience disasters. When a system with overcommit enabled, such as Linux on default settings, comes close to memory exhaustion, the system locks up and becomes unusable. At this point, the OOM Killer selects an application to kill based on heuristics. This non-deterministic decision often results in an important process being killed, and often fails to return the system back to working order.

Recursion§

Recursion is a fundamental tool in modeling software. However it has an often-overlooked problem: unbounded memory allocation.

Recursion is an area of active experimentation in Zig and so the documentation here is not final. You can read asummary of recursion status in the 0.3.0 release notes.

The short summary is that currently recursion works normally as you would expect. Although Zig code is not yet protected from stack overflow, it is planned that a future version of Zig will provide such protection, with some degree of cooperation from Zig code required.

Lifetime and Ownership§

It is the Zig programmer's responsibility to ensure that apointer is not accessed when the memory pointed to is no longer available. Note that aslice is a form of pointer, in that it references other memory.

In order to prevent bugs, there are some helpful conventions to follow when dealing with pointers. In general, when a function returns a pointer, the documentation for the function should explain who "owns" the pointer. This concept helps the programmer decide when it is appropriate, if ever, to free the pointer.

For example, the function's documentation may say "caller owns the returned memory", in which case the code that calls the function must have a plan for when to free that memory. Probably in this situation, the function will accept an*Allocator parameter.

Sometimes the lifetime of a pointer may be more complicated. For example, thestd.ArrayList(T).items slice has a lifetime that remains valid until the next time the list is resized, such as by appending new elements.

The API documentation for functions and data structures should take great care to explain the ownership and lifetime semantics of pointers. Ownership determines whose responsibility it is to free the memory referenced by the pointer, and lifetime determines the point at which the memory becomes inaccessible (lestUndefined Behavior occur).

Compile Variables§

Compile variables are accessible by importing the"builtin" package, which the compiler makes available to every Zig source file. It contains compile-time constants such as the current target, endianness, and release mode.

const builtin =@import("builtin");const separator =if (builtin.os.tag == builtin.Os.windows)'\\'else'/';

Example of what is imported with@import("builtin"):

const std =@import("std");/// Zig version. When writing code that supports multiple versions of Zig, prefer/// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.pubconst zig_version = std.SemanticVersion.parse("0.8.1")catchunreachable;/// Temporary until self-hosted is feature complete.pubconst zig_is_stage2 =true;/// Temporary until self-hosted supports the `cpu.arch` value.pubconst stage2_arch: std.Target.Cpu.Arch = .x86_64;pubconst output_mode = std.builtin.OutputMode.Obj;pubconst link_mode = std.builtin.LinkMode.Static;pubconst is_test =false;pubconst single_threaded =false;pubconst abi = std.Target.Abi.gnu;pubconst cpu: std.Target.Cpu = .{    .arch = .x86_64,    .model = &std.Target.x86.cpu.skylake,    .features = std.Target.x86.featureSet(&[_]std.Target.x86.Feature{        .@"64bit",        .adx,        .aes,        .avx,        .avx2,        .bmi,        .bmi2,        .clflushopt,        .cmov,        .cx16,        .cx8,        .ermsb,        .f16c,        .false_deps_popcnt,        .fast_15bytenop,        .fast_gather,        .fast_scalar_fsqrt,        .fast_shld_rotate,        .fast_variable_shuffle,        .fast_vector_fsqrt,        .fma,        .fsgsbase,        .fxsr,        .idivq_to_divl,        .invpcid,        .lzcnt,        .macrofusion,        .mmx,        .movbe,        .nopl,        .pclmul,        .popcnt,        .prfchw,        .rdrnd,        .rdseed,        .sahf,        .sgx,        .slow_3ops_lea,        .sse,        .sse2,        .sse3,        .sse4_1,        .sse4_2,        .ssse3,        .vzeroupper,        .x87,        .xsave,        .xsavec,        .xsaveopt,        .xsaves,    }),};pubconst os = std.Target.Os{    .tag = .linux,    .version_range = .{ .linux = .{        .range = .{            .min = .{                .major =5,                .minor =4,                .patch =122,            },            .max = .{                .major =5,                .minor =4,                .patch =122,            },        },        .glibc = .{            .major =2,            .minor =31,            .patch =0,        },    }},};pubconst target = std.Target{    .cpu = cpu,    .os = os,    .abi = abi,};pubconst object_format = std.Target.ObjectFormat.elf;pubconst mode = std.builtin.Mode.Debug;pubconst link_libc =false;pubconst link_libcpp =false;pubconst have_error_return_tracing =true;pubconst valgrind_support =true;pubconst position_independent_code =false;pubconst position_independent_executable =false;pubconst strip_debug_info =false;pubconst code_model = std.builtin.CodeModel.default;

See also:

Root Source File§

TODO: explain how root source file finds other files

TODO: pub fn main

TODO: pub fn panic

TODO: if linking with libc you can use export fn main

TODO: order independent top level declarations

TODO: lazy analysis

TODO: using comptime { _ = @import() }

Zig Test§

zig test is a tool that can be used to quickly build and run Zig code to make sure behavior meets expectations.@import("builtin").is_test is available for code to detect whether the current build is a test build.

detect_test.zig

const std =@import("std");const builtin =@import("builtin");const expect = std.testing.expect;test"builtin.is_test" {try expect(builtin.is_test);}
$ zig test detect_test.zigTest [1/1] test "builtin.is_test"... All 1 tests passed.

Zig has lazy top level declaration analysis, which means that if a function is not called, or otherwise used, it is not analyzed. This means that there may be an undiscovered compile error in a function because it is never called.

unused_fn.zig

fnunused()i32 {return"wrong return type";}test"unused function" { }
$ zig test unused_fn.zigTest [1/1] test "unused function"... All 1 tests passed.

Note that, while inDebug andReleaseSafe modes,unreachable emits a call to@panic, inReleaseFast andReleaseSmall modes, it is really undefined behavior. The implementation ofstd.debug.assert is as simple as:

pubfnassert(ok:bool)void {if (!ok)unreachable;}

This means that when testing in ReleaseFast or ReleaseSmall mode,assert is not sufficient to check the result of a computation:

const std =@import("std");const assert = std.debug.assert;test"assert in release fast mode" {    assert(false);}

When compiling this test inReleaseFast mode, it invokes uncheckedUndefined Behavior. Since that could do anything, this documentation cannot show you the output.

Better practice for checking the output when testing is to usestd.testing.expect:

test.zig

const std =@import("std");const expect = std.testing.expect;test"expect in release fast mode" {try expect(false);}
$ zig test test.zig -O ReleaseFastTest [1/1] test "expect in release fast mode"... Test [2/1] test "expect in release fast mode"... FAIL (TestUnexpectedResult)0 passed; 0 skipped; 1 failed.error: the following test command failed with exit code 1:docgen_tmp/zig-cache/o/5f772ba34b67337c067e5630990ef24e/test /home/andy/Downloads/zig/build-release/zig

See the rest of thestd.testing namespace for more available functions.

zig test has a few command line parameters which affect the compilation. Seezig --help for a full list. The most interesting one is--test-filter [text]. This makes the test build only include tests whose name contains the supplied filter text. Again, thanks to lazy analysis, this can allow you to narrow a build to only a few functions in isolation.

Zig Build System§

The Zig Build System provides a cross-platform, dependency-free way to declare the logic required to build a project. With this system, the logic to build a project is written in a build.zig file, using the Zig Build System API to declare and configure build artifacts and other tasks.

Some examples of tasks the build system can help with:

  • Creating build artifacts by executing the Zig compiler. This includes building Zig source code as well as C and C++ source code.
  • Capturing user-configured options and using those options to configure the build.
  • Surfacing build configuration ascomptime values by providing a file that can beimported by Zig code.
  • Caching build artifacts to avoid unnecessarily repeating steps.
  • Executing build artifacts or system-installed tools.
  • Running tests and verifying the output of executing a build artifact matches the expected value.
  • Runningzig fmt on a codebase or a subset of it.
  • Custom tasks.

To use the build system, runzig build --help to see a command-line usage help menu. This will include project-specific options that were declared in the build.zig script.

Building an Executable§

Thisbuild.zig file is automatically generated byzig init-exe.

const Builder =@import("std").build.Builder;pubfnbuild(b: *Builder)void {// Standard target options allows the person running `zig build` to choose// what target to build for. Here we do not override the defaults, which// means any target is allowed, and the default is native. Other options// for restricting supported target set are available.const target = b.standardTargetOptions(.{});// Standard release options allow the person running `zig build` to select// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.const mode = b.standardReleaseOptions();const exe = b.addExecutable("example","src/main.zig");    exe.setTarget(target);    exe.setBuildMode(mode);    exe.install();const run_cmd = exe.run();    run_cmd.step.dependOn(b.getInstallStep());if (b.args) |args| {        run_cmd.addArgs(args);    }const run_step = b.step("run","Run the app");    run_step.dependOn(&run_cmd.step);}

Building a Library§

Thisbuild.zig file is automatically generated byzig init-lib.

const Builder =@import("std").build.Builder;pubfnbuild(b: *Builder)void {const mode = b.standardReleaseOptions();const lib = b.addStaticLibrary("example","src/main.zig");    lib.setBuildMode(mode);    lib.install();var main_tests = b.addTest("src/main.zig");    main_tests.setBuildMode(mode);const test_step = b.step("test","Run library tests");    test_step.dependOn(&main_tests.step);}

Compiling C Source Code§

lib.addCSourceFile("src/lib.c", &[_][]constu8{"-Wall","-Wextra","-Werror",});

C§

Although Zig is independent of C, and, unlike most other languages, does not depend on libc, Zig acknowledges the importance of interacting with existing C code.

There are a few ways that Zig facilitates C interop.

C Type Primitives§

These have guaranteed C ABI compatibility and can be used like any other type.

  • c_short
  • c_ushort
  • c_int
  • c_uint
  • c_long
  • c_ulong
  • c_longlong
  • c_ulonglong
  • c_longdouble
  • c_void

See also:

Import from C Header File§

The@cImport builtin function can be used to directly import symbols from .h files:

test.zig

const c =@cImport({// See https://github.com/ziglang/zig/issues/515@cDefine("_NO_CRT_STDIO_INLINE","1");@cInclude("stdio.h");});pubfnmain()void {    _ = c.printf("hello\n");}
$ zig build-exe test.zig -lc$ ./testhello

The@cImport function takes an expression as a parameter. This expression is evaluated at compile-time and is used to control preprocessor directives and include multiple .h files:

const builtin =@import("builtin");const c =@cImport({@cDefine("NDEBUG", builtin.mode == .ReleaseFast);if (something) {@cDefine("_GNU_SOURCE", {});    }@cInclude("stdlib.h");if (something) {@cUndef("_GNU_SOURCE");    }@cInclude("soundio.h");});

See also:

C Pointers§

This type is to be avoided whenever possible. The only valid reason for using a C pointer is in auto-generated code from translating C code.

When importing C header files, it is ambiguous whether pointers should be translated as single-item pointers (*T) or many-item pointers ([*]T). C pointers are a compromise so that Zig code can utilize translated header files directly.

[*c]T - C pointer.

  • Supports all the syntax of the other two pointer types.
  • Coerces to other pointer types, as well asOptional Pointers. When a C pointer is coerced to a non-optional pointer, safety-checkedUndefined Behavior occurs if the address is 0.
  • Allows address 0. On non-freestanding targets, dereferencing address 0 is safety-checkedUndefined Behavior. Optional C pointers introduce another bit to keep track of null, just like?usize. Note that creating an optional C pointer is unnecessary as one can use normalOptional Pointers.
  • SupportsType Coercion to and from integers.
  • Supports comparison with integers.
  • Does not support Zig-only pointer attributes such as alignment. Use normalPointers please!

When a C pointer is pointing to a single struct (not an array), dereference the C pointer to access to the struct's fields or member data. That syntax looks like this:

ptr_to_struct.*.struct_member

This is comparable to doing-> in C.

When a C pointer is pointing to an array of structs, the syntax reverts to this:

ptr_to_struct_array[index].struct_member

Exporting a C Library§

One of the primary use cases for Zig is exporting a library with the C ABI for other programming languages to call into. Theexport keyword in front of functions, variables, and types causes them to be part of the library API:

mathtest.zig

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

To make a static library:

$ zig build-lib mathtest.zig

To make a shared library:

$ zig build-lib mathtest.zig -dynamic

Here is an example with theZig Build System:

test.c

// This header is generated by zig from mathtest.zig#include "mathtest.h"#include <stdio.h>int main(int argc, char **argv) {    int32_t result = add(42, 1337);    printf("%d\n", result);    return 0;}

build.zig

const Builder =@import("std").build.Builder;pubfnbuild(b: *Builder)void {const lib = b.addSharedLibrary("mathtest","mathtest.zig", b.version(1,0,0));const exe = b.addExecutable("test",null);    exe.addCSourceFile("test.c", &[_][]constu8{"-std=c99"});    exe.linkLibrary(lib);    exe.linkSystemLibrary("c");    b.default_step.dependOn(&exe.step);const run_cmd = exe.run();const test_step = b.step("test","Test the program");    test_step.dependOn(&run_cmd.step);}

terminal

$ zig build test1379

See also:

Mixing Object Files§

You can mix Zig object files with any other object files that respect the C ABI. Example:

base64.zig

const base64 =@import("std").base64;exportfndecode_base_64(    dest_ptr: [*]u8,    dest_len:usize,    source_ptr: [*]constu8,    source_len:usize,)usize {const src = source_ptr[0..source_len];const dest = dest_ptr[0..dest_len];const base64_decoder = base64.standard.Decoder;const decoded_size = base64_decoder.calcSizeForSlice(src)catchunreachable;    base64_decoder.decode(dest[0..decoded_size], src)catchunreachable;return decoded_size;}

test.c

// This header is generated by zig from base64.zig#include "base64.h"#include <string.h>#include <stdio.h>int main(int argc, char **argv) {    const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";    char buf[200];    size_t len = decode_base_64(buf, 200, encoded, strlen(encoded));    buf[len] = 0;    puts(buf);    return 0;}

build.zig

const Builder =@import("std").build.Builder;pubfnbuild(b: *Builder)void {const obj = b.addObject("base64","base64.zig");const exe = b.addExecutable("test",null);    exe.addCSourceFile("test.c", &[_][]constu8{"-std=c99"});    exe.addObject(obj);    exe.linkSystemLibrary("c");    exe.install();}

terminal

$ zig build$ ./zig-out/bin/testall your base are belong to us

See also:

WebAssembly§

Zig supports building for WebAssembly out of the box.

Freestanding§

For host environments like the web browser and nodejs, build as a dynamic library using the freestanding OS target. Here's an example of running Zig code compiled to WebAssembly with nodejs.

math.zig

externfnprint(i32)void;exportfnadd(a:i32, b:i32)void {    print(a + b);}
$ zig build-lib math.zig -target wasm32-freestanding -dynamic

test.js

const fs = require('fs');const source = fs.readFileSync("./math.wasm");const typedArray = new Uint8Array(source);WebAssembly.instantiate(typedArray, {  env: {    print: (result) => { console.log(`The result is ${result}`); }  }}).then(result => {  const add = result.instance.exports.add;  add(1, 2);});
$ node test.jsThe result is 3

WASI§

Zig's support for WebAssembly System Interface (WASI) is under active development. Example of using the standard library and reading command line arguments:

args.zig

const std =@import("std");pubfnmain() !void {var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};const gpa = &general_purpose_allocator.allocator;const args =try std.process.argsAlloc(gpa);defer std.process.argsFree(gpa, args);for (args) |arg, i| {        std.debug.print("{}: {s}\n", .{ i, arg });    }}
$ zig build-exe args.zig -target wasm32-wasi
$ wasmtime args.wasm 123 hello0: args.wasm1: 1232: hello

A more interesting example would be extracting the list of preopens from the runtime. This is now supported in the standard library viastd.fs.wasi.PreopenList:

preopens.zig

const std =@import("std");const PreopenList = std.fs.wasi.PreopenList;pubfnmain() !void {var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};const gpa = &general_purpose_allocator.allocator;var preopens = PreopenList.init(gpa);defer preopens.deinit();try preopens.populate();for (preopens.asSlice()) |preopen, i| {        std.debug.print("{}: {}\n", .{ i, preopen });    }}
$ zig build-exe preopens.zig -target wasm32-wasi
$ wasmtime --dir=. preopens.wasm0: Preopen{ .fd = 3, .type = PreopenType{ .Dir = '.' } }

Targets§

Zig supports generating code for all targets that LLVM supports. Here is what it looks like to executezig targets on a Linux x86_64 computer:

$ zig targetsArchitectures:  arm    v8_4a    v8_3a    v8_2a    v8_1a    v8    v8r    v8m_baseline    v8m_mainline    v7    v7em    v7m    v7s    v7k    v7ve    v6    v6m    v6k    v6t2    v5    v5te    v4t  armeb    v8_4a    v8_3a    v8_2a    v8_1a    v8    v8r    v8m_baseline    v8m_mainline    v7    v7em    v7m    v7s    v7k    v7ve    v6    v6m    v6k    v6t2    v5    v5te    v4t  aarch64    v8_4a    v8_3a    v8_2a    v8_1a    v8    v8r    v8m_baseline    v8m_mainline  aarch64_be    v8_4a    v8_3a    v8_2a    v8_1a    v8    v8r    v8m_baseline    v8m_mainline  avr  bpfel  bpfeb  hexagon  mips  mipsel  mips64  mips64el  msp430  powerpc  powerpc64  powerpc64le  r600  amdgcn  riscv32  riscv64  sparc  sparcv9  sparcel  s390x  thumb    v8_4a    v8_3a    v8_2a    v8_1a    v8    v8r    v8m_baseline    v8m_mainline    v7    v7em    v7m    v7s    v7k    v7ve    v6    v6m    v6k    v6t2    v5    v5te    v4t  thumbeb    v8_4a    v8_3a    v8_2a    v8_1a    v8    v8r    v8m_baseline    v8m_mainline    v7    v7em    v7m    v7s    v7k    v7ve    v6    v6m    v6k    v6t2    v5    v5te    v4t  i386  x86_64 (native)  xcore  nvptx  nvptx64  lanai  wasm32  wasm64Operating Systems:  freestanding  ananas  cloudabi  dragonfly  freebsd  fuchsia  ios  kfreebsd  linux (native)  lv2  macos  netbsd  openbsd  solaris  windows  haiku  minix  rtems  nacl  cnk  aix  cuda  nvcl  amdhsa  ps4  elfiamcu  tvos  wasi  watchos  mesa3d  contiki  amdpal  zen  uefiC ABIs:  none  gnu (native)  gnuabin32  gnuabi64  gnueabi  gnueabihf  gnux32  code16  eabi  eabihf  android  musl  musleabi  musleabihf  msvc  itanium  cygnus  coreclr  simulatorAvailable libcs:  aarch64_be-linux-gnu  aarch64_be-linux-musl  aarch64-linux-gnu  aarch64-linux-musleabi  armeb-linux-gnueabi  armeb-linux-gnueabihf  armeb-linux-musleabi  armeb-linux-musleabihf  arm-linux-gnueabi  arm-linux-gnueabihf  arm-linux-musleabi  arm-linux-musleabihf  i386-linux-gnu  i386-linux-musl  mips64el-linux-gnuabi64  mips64el-linux-gnuabin32  mips64el-linux-musl  mips64-linux-gnuabi64  mips64-linux-gnuabin32  mips64-linux-musl  mipsel-linux-gnu  mipsel-linux-musl  mips-linux-gnu  mips-linux-musl  nios2-linux-gnu  powerpc64le-linux-gnu  powerpc64le-linux-musl  powerpc64-linux-gnu  powerpc64-linux-musl  powerpc-linux-gnu  powerpc-linux-musl  riscv32-linux-musl  riscv64-linux-gnu  riscv64-linux-musl  s390x-linux-gnu  s390x-linux-musl  sparc-linux-gnu  sparcv9-linux-gnu  wasm32-freestanding-musl  wasm32-wasi-musl  x86_64-linux-gnu  x86_64-linux-gnux32  x86_64-linux-musl

The Zig Standard Library (@import("std")) has architecture, environment, and operating system abstractions, and thus takes additional work to support more platforms. Not all standard library code requires operating system abstractions, however, so things such as generic data structures work on all above platforms.

The current list of targets supported by the Zig Standard Library is:

  • Linux x86_64
  • Windows x86_64
  • macOS x86_64

Style Guide§

These coding conventions are not enforced by the compiler, but they are shipped inthis documentation along with the compiler in order to provide a point ofreference, should anyone wish to point to an authority on agreed upon Zigcoding style.

Whitespace§

  • 4 space indentation
  • Open braces on same line, unless you need to wrap.
  • If a list of things is longer than 2, put each item on its own line and exercise the ability to put an extra comma at the end.
  • Line length: aim for 100; use common sense.

Names§

Roughly speaking:camelCaseFunctionName,TitleCaseTypeName,snake_case_variable_name. More precisely:

  • Ifx is atype thenx should beTitleCase, unless it is astruct with 0 fields and is never meant to be instantiated, in which case it is considered to be a "namespace" and usessnake_case.
  • Ifx is callable, andx's return type istype, thenx should beTitleCase.
  • Ifx is otherwise callable, thenx should becamelCase.
  • Otherwise,x should besnake_case.

Acronyms, initialisms, proper nouns, or any other word that has capitalization rules in written English are subject to naming conventions just like any other word. Even acronyms that are only 2 letters long are subject to these conventions.

File names fall into two categories: types and namespaces. If the file (implicitly a struct) has top level fields, it should be named like any other struct with fields usingTitleCase. Otherwise, it should usesnake_case. Directory names should besnake_case.

These are general rules of thumb; if it makes sense to do something different, do what makes sense. For example, if there is an established convention such asENOENT, follow the established convention.

Examples§

const namespace_name =@import("dir_name/file_name.zig");const TypeName =@import("dir_name/TypeName.zig");var global_var:i32 =undefined;const const_name =42;const primitive_type_alias =f32;const string_alias = []u8;const StructName =struct {    field:i32,};const StructAlias = StructName;fnfunctionName(param_name: TypeName)void {var functionPointer = functionName;    functionPointer();    functionPointer = otherFunction;    functionPointer();}const functionAlias = functionName;fnListTemplateFunction(comptime ChildType:type,comptime fixed_size:usize)type {return List(ChildType, fixed_size);}fnShortList(comptime T:type,comptime n:usize)type {returnstruct {        field_name: [n]T,fnmethodName()void {}    };}// The word XML loses its casing when used in Zig identifiers.const xml_document =\\<?xml version="1.0" encoding="UTF-8"?>\\<document>\\</document>;const XmlParser =struct {    field:i32,};// The initials BE (Big Endian) are just another word in Zig identifier names.fnreadU32Be()u32 {}

See the Zig Standard Library for more examples.

Doc Comment Guidance§

  • Omit any information that is redundant based on the name of the thing being documented.
  • Duplicating information onto multiple similar functions is encouraged because it helps IDEs and other tools provide better help text.
  • Use the wordassume to indicate invariants that causeUndefined Behavior when violated.
  • Use the wordassert to indicate invariants that causesafety-checkedUndefined Behavior when violated.

Source Encoding§

Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.

Throughout all zig source code (including in comments), some code points are never allowed:

  • Ascii control characters, except for U+000a (LF), U+000d (CR), and U+0009 (HT): U+0000 - U+0008, U+000b - U+000c, U+000e - U+0001f, U+007f.
  • Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).

LF (byte value 0x0a, code point U+000a,'\n') is the line terminator in Zig source code. This byte value terminates every line of zig source code except the last line of the file. It is recommended that non-empty source files end with an empty line, which means the last byte would be 0x0a (LF).

Each LF may be immediately preceded by a single CR (byte value 0x0d, code point U+000d,'\r') to form a Windows style line ending, but this is discouraged. A CR in any other context is not allowed.

HT hard tabs (byte value 0x09, code point U+0009,'\t') are interchangeable with SP spaces (byte value 0x20, code point U+0020,' ') as a token separator, but use of hard tabs is discouraged. SeeGrammar.

Note that runningzig fmt on a source file will implement all recommendations mentioned here. Note also that the stage1 compiler doesnot yet support CR or HT control characters.

Note that a tool reading Zig source code can make assumptions if the source code is assumed to be correct Zig code. For example, when identifying the ends of lines, a tool can use a naive search such as/\n/, or anadvanced search such as/\r\n?|[\n\u0085\u2028\u2029]/, and in either case line endings will be correctly identified. For another example, when identifying the whitespace before the first token on a line, a tool can either use a naive search such as/[ \t]/, or anadvanced search such as/\s/, and in either case whitespace will be correctly identified.

Keyword Reference§

Keyword Description
align
align can be used to specify the alignment of a pointer. It can also be used after a variable or function declaration to specify the alignment of pointers to that variable or function.
allowzero
The pointer attributeallowzero allows a pointer to have address zero.
and
The boolean operatorand.
anyframe
anyframe can be used as a type for variables which hold pointers to function frames.
anytype
Function parameters and struct fields can be declared withanytype in place of the type. The type will be inferred where the function is called or the struct is instantiated.
asm
asm begins an inline assembly expression. This allows for directly controlling the machine code generated on compilation.
async
async can be used before a function call to get a pointer to the function's frame when it suspends.
await
await can be used to suspend the current function until the frame provided after theawait completes.await copies the value returned from the target function's frame to the caller.
break
break can be used with a block label to return a value from the block. It can also be used to exit a loop before iteration completes naturally.
catch
catch can be used to evaluate an expression if the expression before it evaluates to an error. The expression after thecatch can optionally capture the error value.
comptime
comptime before a declaration can be used to label variables or function parameters as known at compile time. It can also be used to guarantee an expression is run at compile time.
const
const declares a variable that can not be modified. Used as a pointer attribute, it denotes the value referenced by the pointer cannot be modified.
continue
continue can be used in a loop to jump back to the beginning of the loop.
defer
defer will execute an expression when control flow leaves the current block.
else
else can be used to provide an alternate branch forif,switch,while, andfor expressions.
  • If used after an if expression, the else branch will be executed if the test value returns false, null, or an error.
  • If used within a switch expression, the else branch will be executed if the test value matches no other cases.
  • If used after a loop expression, the else branch will be executed if the loop finishes without breaking.
  • See alsoif,switch,while,for
enum
enum defines an enum type.
errdefer
errdefer will execute an expression when control flow leaves the current block if the function returns an error.
error
error defines an error type.
export
export makes a function or variable externally visible in the generated object file. Exported functions default to the C calling convention.
extern
extern can be used to declare a function or variable that will be resolved at link time, when linking statically or at runtime, when linking dynamically.
false
The boolean valuefalse.
fn
fn declares a function.
for
Afor expression can be used to iterate over the elements of a slice, array, or tuple.
if
Anif expression can test boolean expressions, optional values, or error unions. For optional values or error unions, the if expression can capture the unwrapped value.
  • See alsoif
inline
inline can be used to label a loop expression such that it will be unrolled at compile time. It can also be used to force a function to be inlined at all call sites.
noalias
Thenoalias keyword.
  • TODO add documentation for noalias
nosuspend
Thenosuspend keyword can be used in front of a block, statement or expression, to mark a scope where no suspension points are reached. In particular, inside anosuspend scope:
  • Using thesuspend keyword results in a compile error.
  • Usingawait on a function frame which hasn't completed yet results in safety-checkedUndefined Behavior.
  • Calling an async function may result in safety-checkedUndefined Behavior, because it's equivalent toawait async some_async_fn(), which contains anawait.
Code inside anosuspend scope does not cause the enclosing function to become anasync function.
null
The optional valuenull.
or
The boolean operatoror.
orelse
orelse can be used to evaluate an expression if the expression before it evaluates to null.
packed
Thepacked keyword before a struct definition changes the struct's in-memory layout to the guaranteedpacked layout.
pub
Thepub in front of a top level declaration makes the declaration available to reference from a different file than the one it is declared in.
resume
resume will continue execution of a function frame after the point the function was suspended.
return
return exits a function with a value.
linksection
Thelinksection keyword.
  • TODO add documentation for linksection
struct
struct defines a struct.
suspend
suspend will cause control flow to return to the call site or resumer of the function.suspend can also be used before a block within a function, to allow the function access to its frame before control flow returns to the call site.
switch
Aswitch expression can be used to test values of a common type.switch cases can capture field values of aTagged union.
test
Thetest keyword can be used to denote a top-level block of code used to make sure behavior meets expectations.
threadlocal
threadlocal can be used to specify a variable as thread-local.
true
The boolean valuetrue.
try
try evaluates an error union expression. If it is an error, it returns from the current function with the same error. Otherwise, the expression results in the unwrapped value.
undefined
undefined can be used to leave a value uninitialized.
union
union defines a union.
unreachable
unreachable can be used to assert that control flow will never happen upon a particular location. Depending on the build mode,unreachable may emit a panic.
  • Emits a panic inDebug andReleaseSafe mode, or when usingzig test.
  • Does not emit a panic inReleaseFast mode, unlesszig test is being used.
  • See alsounreachable
usingnamespace
usingnamespace is a top-level declaration that imports all the public declarations of the operand, which must be a struct, union, or enum, into the current scope.
var
var declares a variable that may be modified.
volatile
volatile can be used to denote loads or stores of a pointer have side effects. It can also modify an inline assembly expression to denote it has side effects.
while
Awhile expression can be used to repeatedly test a boolean, optional, or error union expression, and cease looping when that expression evaluates to false, null, or an error, respectively.

Grammar§

Root <- skip container_doc_comment? ContainerMembers eof# *** Top level ***ContainerMembers <- ContainerDeclarations (ContainerField COMMA)* (ContainerField / ContainerDeclarations)ContainerDeclarations    <- TestDecl ContainerDeclarations     / TopLevelComptime ContainerDeclarations     / doc_comment? KEYWORD_pub? TopLevelDecl ContainerDeclarations     /TestDecl <- doc_comment? KEYWORD_test STRINGLITERALSINGLE? BlockTopLevelComptime <- doc_comment? KEYWORD_comptime BlockExprTopLevelDecl    <- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / (KEYWORD_inline / KEYWORD_noinline))? FnProto (SEMICOLON / Block)     / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl     / KEYWORD_usingnamespace Expr SEMICOLONFnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? CallConv? EXCLAMATIONMARK? TypeExprVarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? LinkSection? (EQUAL Expr)? SEMICOLONContainerField <- doc_comment? KEYWORD_comptime? IDENTIFIER (COLON (KEYWORD_anytype / TypeExpr) ByteAlign?)? (EQUAL Expr)?# *** Block Level ***Statement    <- KEYWORD_comptime? VarDecl     / KEYWORD_comptime BlockExprStatement     / KEYWORD_nosuspend BlockExprStatement     / KEYWORD_suspend BlockExprStatement     / KEYWORD_defer BlockExprStatement     / KEYWORD_errdefer Payload? BlockExprStatement     / IfStatement     / LabeledStatement     / SwitchExpr     / AssignExpr SEMICOLONIfStatement    <- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )?     / IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )LabeledStatement <- BlockLabel? (Block / LoopStatement)LoopStatement <- KEYWORD_inline? (ForStatement / WhileStatement)ForStatement    <- ForPrefix BlockExpr ( KEYWORD_else Statement )?     / ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement )WhileStatement    <- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )?     / WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )BlockExprStatement    <- BlockExpr     / AssignExpr SEMICOLONBlockExpr <- BlockLabel? Block# *** Expression Level ***AssignExpr <- Expr (AssignOp Expr)?Expr <- BoolOrExprBoolOrExpr <- BoolAndExpr (KEYWORD_or BoolAndExpr)*BoolAndExpr <- CompareExpr (KEYWORD_and CompareExpr)*CompareExpr <- BitwiseExpr (CompareOp BitwiseExpr)?BitwiseExpr <- BitShiftExpr (BitwiseOp BitShiftExpr)*BitShiftExpr <- AdditionExpr (BitShiftOp AdditionExpr)*AdditionExpr <- MultiplyExpr (AdditionOp MultiplyExpr)*MultiplyExpr <- PrefixExpr (MultiplyOp PrefixExpr)*PrefixExpr <- PrefixOp* PrimaryExprPrimaryExpr    <- AsmExpr     / IfExpr     / KEYWORD_break BreakLabel? Expr?     / KEYWORD_comptime Expr     / KEYWORD_nosuspend Expr     / KEYWORD_continue BreakLabel?     / KEYWORD_resume Expr     / KEYWORD_return Expr?     / BlockLabel? LoopExpr     / Block     / CurlySuffixExprIfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)?Block <- LBRACE Statement* RBRACELoopExpr <- KEYWORD_inline? (ForExpr / WhileExpr)ForExpr <- ForPrefix Expr (KEYWORD_else Expr)?WhileExpr <- WhilePrefix Expr (KEYWORD_else Payload? Expr)?CurlySuffixExpr <- TypeExpr InitList?InitList    <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE     / LBRACE Expr (COMMA Expr)* COMMA? RBRACE     / LBRACE RBRACETypeExpr <- PrefixTypeOp* ErrorUnionExprErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK TypeExpr)?SuffixExpr    <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments     / PrimaryTypeExpr (SuffixOp / FnCallArguments)*PrimaryTypeExpr    <- BUILTINIDENTIFIER FnCallArguments     / CHAR_LITERAL     / ContainerDecl     / DOT IDENTIFIER     / DOT InitList     / ErrorSetDecl     / FLOAT     / FnProto     / GroupedExpr     / LabeledTypeExpr     / IDENTIFIER     / IfTypeExpr     / INTEGER     / KEYWORD_comptime TypeExpr     / KEYWORD_error DOT IDENTIFIER     / KEYWORD_false     / KEYWORD_null     / KEYWORD_anyframe     / KEYWORD_true     / KEYWORD_undefined     / KEYWORD_unreachable     / STRINGLITERAL     / SwitchExprContainerDecl <- (KEYWORD_extern / KEYWORD_packed)? ContainerDeclAutoErrorSetDecl <- KEYWORD_error LBRACE IdentifierList RBRACEGroupedExpr <- LPAREN Expr RPARENIfTypeExpr <- IfPrefix TypeExpr (KEYWORD_else Payload? TypeExpr)?LabeledTypeExpr    <- BlockLabel Block     / BlockLabel? LoopTypeExprLoopTypeExpr <- KEYWORD_inline? (ForTypeExpr / WhileTypeExpr)ForTypeExpr <- ForPrefix TypeExpr (KEYWORD_else TypeExpr)?WhileTypeExpr <- WhilePrefix TypeExpr (KEYWORD_else Payload? TypeExpr)?SwitchExpr <- KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE# *** Assembly ***AsmExpr <- KEYWORD_asm KEYWORD_volatile? LPAREN Expr AsmOutput? RPARENAsmOutput <- COLON AsmOutputList AsmInput?AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPARENAsmInput <- COLON AsmInputList AsmClobbers?AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPARENAsmClobbers <- COLON StringList# *** Helper grammar ***BreakLabel <- COLON IDENTIFIERBlockLabel <- IDENTIFIER COLONFieldInit <- DOT IDENTIFIER EQUAL ExprWhileContinueExpr <- COLON LPAREN AssignExpr RPARENLinkSection <- KEYWORD_linksection LPAREN Expr RPAREN# Fn specificCallConv <- KEYWORD_callconv LPAREN Expr RPARENParamDecl    <- doc_comment? (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType     / DOT3ParamType    <- KEYWORD_anytype     / TypeExpr# Control flow prefixesIfPrefix <- KEYWORD_if LPAREN Expr RPAREN PtrPayload?WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr?ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload# PayloadsPayload <- PIPE IDENTIFIER PIPEPtrPayload <- PIPE ASTERISK? IDENTIFIER PIPEPtrIndexPayload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE# Switch specificSwitchProng <- SwitchCase EQUALRARROW PtrPayload? AssignExprSwitchCase    <- SwitchItem (COMMA SwitchItem)* COMMA?     / KEYWORD_elseSwitchItem <- Expr (DOT3 Expr)?# OperatorsAssignOp    <- ASTERISKEQUAL     / SLASHEQUAL     / PERCENTEQUAL     / PLUSEQUAL     / MINUSEQUAL     / LARROW2EQUAL     / RARROW2EQUAL     / AMPERSANDEQUAL     / CARETEQUAL     / PIPEEQUAL     / ASTERISKPERCENTEQUAL     / PLUSPERCENTEQUAL     / MINUSPERCENTEQUAL     / EQUALCompareOp    <- EQUALEQUAL     / EXCLAMATIONMARKEQUAL     / LARROW     / RARROW     / LARROWEQUAL     / RARROWEQUALBitwiseOp    <- AMPERSAND     / CARET     / PIPE     / KEYWORD_orelse     / KEYWORD_catch Payload?BitShiftOp    <- LARROW2     / RARROW2AdditionOp    <- PLUS     / MINUS     / PLUS2     / PLUSPERCENT     / MINUSPERCENTMultiplyOp    <- PIPE2     / ASTERISK     / SLASH     / PERCENT     / ASTERISK2     / ASTERISKPERCENTPrefixOp    <- EXCLAMATIONMARK     / MINUS     / TILDE     / MINUSPERCENT     / AMPERSAND     / KEYWORD_try     / KEYWORD_awaitPrefixTypeOp    <- QUESTIONMARK     / KEYWORD_anyframe MINUSRARROW     / SliceTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*     / PtrTypeStart (KEYWORD_align LPAREN Expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*     / ArrayTypeStartSuffixOp    <- LBRACKET Expr (DOT2 (Expr? (COLON Expr)?)?)? RBRACKET     / DOT IDENTIFIER     / DOTASTERISK     / DOTQUESTIONMARKFnCallArguments <- LPAREN ExprList RPAREN# Ptr specificSliceTypeStart <- LBRACKET (COLON Expr)? RBRACKETPtrTypeStart    <- ASTERISK     / ASTERISK2     / LBRACKET ASTERISK (LETTERC / COLON Expr)? RBRACKETArrayTypeStart <- LBRACKET Expr (COLON Expr)? RBRACKET# ContainerDecl specificContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACEContainerDeclType    <- KEYWORD_struct     / KEYWORD_opaque     / KEYWORD_enum (LPAREN Expr RPAREN)?     / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?# AlignmentByteAlign <- KEYWORD_align LPAREN Expr RPAREN# ListsIdentifierList <- (doc_comment? IDENTIFIER COMMA)* (doc_comment? IDENTIFIER)?SwitchProngList <- (SwitchProng COMMA)* SwitchProng?AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem?AsmInputList <- (AsmInputItem COMMA)* AsmInputItem?StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL?ParamDeclList <- (ParamDecl COMMA)* ParamDecl?ExprList <- (Expr COMMA)* Expr?# *** Tokens ***eof <- !.bin <- [01]bin_ <- '_'? binoct <- [0-7]oct_ <- '_'? octhex <- [0-9a-fA-F]hex_ <- '_'? hexdec <- [0-9]dec_ <- '_'? decbin_int <- bin bin_*oct_int <- oct oct_*dec_int <- dec dec_*hex_int <- hex hex_*ox80_oxBF <- [\200-\277]oxF4 <- '\364'ox80_ox8F <- [\200-\217]oxF1_oxF3 <- [\361-\363]oxF0 <- '\360'ox90_0xBF <- [\220-\277]oxEE_oxEF <- [\356-\357]oxED <- '\355'ox80_ox9F <- [\200-\237]oxE1_oxEC <- [\341-\354]oxE0 <- '\340'oxA0_oxBF <- [\240-\277]oxC2_oxDF <- [\302-\337]# From https://lemire.me/blog/2018/05/09/how-quickly-can-you-check-that-a-string-is-valid-unicode-utf-8/# First Byte      Second Byte     Third Byte      Fourth Byte# [0x00,0x7F]# [0xC2,0xDF]     [0x80,0xBF]#    0xE0         [0xA0,0xBF]     [0x80,0xBF]# [0xE1,0xEC]     [0x80,0xBF]     [0x80,0xBF]#    0xED         [0x80,0x9F]     [0x80,0xBF]# [0xEE,0xEF]     [0x80,0xBF]     [0x80,0xBF]#    0xF0         [0x90,0xBF]     [0x80,0xBF]     [0x80,0xBF]# [0xF1,0xF3]     [0x80,0xBF]     [0x80,0xBF]     [0x80,0xBF]#    0xF4         [0x80,0x8F]     [0x80,0xBF]     [0x80,0xBF]mb_utf8_literal <-       oxF4      ox80_ox8F ox80_oxBF ox80_oxBF     / oxF1_oxF3 ox80_oxBF ox80_oxBF ox80_oxBF     / oxF0      ox90_0xBF ox80_oxBF ox80_oxBF     / oxEE_oxEF ox80_oxBF ox80_oxBF     / oxED      ox80_ox9F ox80_oxBF     / oxE1_oxEC ox80_oxBF ox80_oxBF     / oxE0      oxA0_oxBF ox80_oxBF     / oxC2_oxDF ox80_oxBFascii_char_not_nl_slash_squote <- [\000-\011\013-\046-\050-\133\135-\177]char_escape    <- "\\x" hex hex     / "\\u{" hex+ "}"     / "\\" [nr\\t'"]char_char    <- mb_utf8_literal     / char_escape     / ascii_char_not_nl_slash_squotestring_char    <- char_escape     / [^\\"\n]container_doc_comment <- ('//!' [^\n]* [ \n]*)+doc_comment <- ('///' [^\n]* [ \n]*)+line_comment <- '//' ![!/][^\n]* / '////' [^\n]*line_string <- ("\\\\" [^\n]* [ \n]*)+skip <- ([ \n] / line_comment)*CHAR_LITERAL <- "'" char_char "'" skipFLOAT    <- "0x" hex_int "." hex_int ([pP] [-+]? dec_int)? skip     /      dec_int "." dec_int ([eE] [-+]? dec_int)? skip     / "0x" hex_int "."? [pP] [-+]? dec_int skip     /      dec_int "."? [eE] [-+]? dec_int skipINTEGER    <- "0b" bin_int skip     / "0o" oct_int skip     / "0x" hex_int skip     /      dec_int   skipSTRINGLITERALSINGLE <- "\"" string_char* "\"" skipSTRINGLITERAL    <- STRINGLITERALSINGLE     / (line_string                 skip)+IDENTIFIER    <- !keyword [A-Za-z_] [A-Za-z0-9_]* skip     / "@\"" string_char* "\""                            skipBUILTINIDENTIFIER <- "@"[A-Za-z_][A-Za-z0-9_]* skipAMPERSAND            <- '&'      ![=]      skipAMPERSANDEQUAL       <- '&='               skipASTERISK             <- '*'      ![*%=]    skipASTERISK2            <- '**'               skipASTERISKEQUAL        <- '*='               skipASTERISKPERCENT      <- '*%'     ![=]      skipASTERISKPERCENTEQUAL <- '*%='              skipCARET                <- '^'      ![=]      skipCARETEQUAL           <- '^='               skipCOLON                <- ':'                skipCOMMA                <- ','                skipDOT                  <- '.'      ![*.?]    skipDOT2                 <- '..'     ![.]      skipDOT3                 <- '...'              skipDOTASTERISK          <- '.*'               skipDOTQUESTIONMARK      <- '.?'               skipEQUAL                <- '='      ![>=]     skipEQUALEQUAL           <- '=='               skipEQUALRARROW          <- '=>'               skipEXCLAMATIONMARK      <- '!'      ![=]      skipEXCLAMATIONMARKEQUAL <- '!='               skipLARROW               <- '<'      ![<=]     skipLARROW2              <- '<<'     ![=]      skipLARROW2EQUAL         <- '<<='              skipLARROWEQUAL          <- '<='               skipLBRACE               <- '{'                skipLBRACKET             <- '['                skipLPAREN               <- '('                skipMINUS                <- '-'      ![%=>]    skipMINUSEQUAL           <- '-='               skipMINUSPERCENT         <- '-%'     ![=]      skipMINUSPERCENTEQUAL    <- '-%='              skipMINUSRARROW          <- '->'               skipPERCENT              <- '%'      ![=]      skipPERCENTEQUAL         <- '%='               skipPIPE                 <- '|'      ![|=]     skipPIPE2                <- '||'               skipPIPEEQUAL            <- '|='               skipPLUS                 <- '+'      ![%+=]    skipPLUS2                <- '++'               skipPLUSEQUAL            <- '+='               skipPLUSPERCENT          <- '+%'     ![=]      skipPLUSPERCENTEQUAL     <- '+%='              skipLETTERC              <- 'c'                skipQUESTIONMARK         <- '?'                skipRARROW               <- '>'      ![>=]     skipRARROW2              <- '>>'     ![=]      skipRARROW2EQUAL         <- '>>='              skipRARROWEQUAL          <- '>='               skipRBRACE               <- '}'                skipRBRACKET             <- ']'                skipRPAREN               <- ')'                skipSEMICOLON            <- ';'                skipSLASH                <- '/'      ![=]      skipSLASHEQUAL           <- '/='               skipTILDE                <- '~'                skipend_of_word <- ![a-zA-Z0-9_] skipKEYWORD_align       <- 'align'       end_of_wordKEYWORD_allowzero   <- 'allowzero'   end_of_wordKEYWORD_and         <- 'and'         end_of_wordKEYWORD_anyframe    <- 'anyframe'    end_of_wordKEYWORD_anytype     <- 'anytype'     end_of_wordKEYWORD_asm         <- 'asm'         end_of_wordKEYWORD_async       <- 'async'       end_of_wordKEYWORD_await       <- 'await'       end_of_wordKEYWORD_break       <- 'break'       end_of_wordKEYWORD_callconv    <- 'callconv'    end_of_wordKEYWORD_catch       <- 'catch'       end_of_wordKEYWORD_comptime    <- 'comptime'    end_of_wordKEYWORD_const       <- 'const'       end_of_wordKEYWORD_continue    <- 'continue'    end_of_wordKEYWORD_defer       <- 'defer'       end_of_wordKEYWORD_else        <- 'else'        end_of_wordKEYWORD_enum        <- 'enum'        end_of_wordKEYWORD_errdefer    <- 'errdefer'    end_of_wordKEYWORD_error       <- 'error'       end_of_wordKEYWORD_export      <- 'export'      end_of_wordKEYWORD_extern      <- 'extern'      end_of_wordKEYWORD_false       <- 'false'       end_of_wordKEYWORD_fn          <- 'fn'          end_of_wordKEYWORD_for         <- 'for'         end_of_wordKEYWORD_if          <- 'if'          end_of_wordKEYWORD_inline      <- 'inline'      end_of_wordKEYWORD_noalias     <- 'noalias'     end_of_wordKEYWORD_nosuspend   <- 'nosuspend'   end_of_wordKEYWORD_noinline    <- 'noinline'    end_of_wordKEYWORD_null        <- 'null'        end_of_wordKEYWORD_opaque      <- 'opaque'      end_of_wordKEYWORD_or          <- 'or'          end_of_wordKEYWORD_orelse      <- 'orelse'      end_of_wordKEYWORD_packed      <- 'packed'      end_of_wordKEYWORD_pub         <- 'pub'         end_of_wordKEYWORD_resume      <- 'resume'      end_of_wordKEYWORD_return      <- 'return'      end_of_wordKEYWORD_linksection <- 'linksection' end_of_wordKEYWORD_struct      <- 'struct'      end_of_wordKEYWORD_suspend     <- 'suspend'     end_of_wordKEYWORD_switch      <- 'switch'      end_of_wordKEYWORD_test        <- 'test'        end_of_wordKEYWORD_threadlocal <- 'threadlocal' end_of_wordKEYWORD_true        <- 'true'        end_of_wordKEYWORD_try         <- 'try'         end_of_wordKEYWORD_undefined   <- 'undefined'   end_of_wordKEYWORD_union       <- 'union'       end_of_wordKEYWORD_unreachable <- 'unreachable' end_of_wordKEYWORD_usingnamespace <- 'usingnamespace' end_of_wordKEYWORD_var         <- 'var'         end_of_wordKEYWORD_volatile    <- 'volatile'    end_of_wordKEYWORD_while       <- 'while'       end_of_wordkeyword <- KEYWORD_align / KEYWORD_allowzero / KEYWORD_and / KEYWORD_anyframe         / KEYWORD_anytype / KEYWORD_asm / KEYWORD_async / KEYWORD_await         / KEYWORD_break / KEYWORD_callconv / KEYWORD_catch / KEYWORD_comptime         / KEYWORD_const / KEYWORD_continue / KEYWORD_defer / KEYWORD_else         / KEYWORD_enum / KEYWORD_errdefer / KEYWORD_error / KEYWORD_export         / KEYWORD_extern / KEYWORD_false / KEYWORD_fn / KEYWORD_for / KEYWORD_if         / KEYWORD_inline / KEYWORD_noalias / KEYWORD_nosuspend / KEYWORD_noinline         / KEYWORD_null / KEYWORD_opaque / KEYWORD_or / KEYWORD_orelse / KEYWORD_packed         / KEYWORD_pub / KEYWORD_resume / KEYWORD_return / KEYWORD_linksection         / KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch         / KEYWORD_test / KEYWORD_threadlocal / KEYWORD_true / KEYWORD_try         / KEYWORD_undefined / KEYWORD_union / KEYWORD_unreachable         / KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while

Zen§

  • Communicate intent precisely.
  • Edge cases matter.
  • Favor reading code over writing code.
  • Only one obvious way to do things.
  • Runtime crashes are better than bugs.
  • Compile errors are better than runtime crashes.
  • Incremental improvements.
  • Avoid local maximums.
  • Reduce the amount one must remember.
  • Focus on code rather than style.
  • Resource allocation may fail; resource deallocation must succeed.
  • Memory is a resource.
  • Together we serve the users.

[8]ページ先頭

©2009-2026 Movatter.jp