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

Index

Introduction

Zig is an open-source programming language designed forrobustness,optimality, andclarity.

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.

Hello World

hello.zig

const std =@import("std");pubfnmain() !void {// If this program is run without stdout attached, exit with an error.var stdout_file =try std.io.getStdOut();// If this program encounters pipe failure when printing to stdout, exit// with an error.try stdout_file.write("Hello, world!\n");}
$ zig build-exe hello.zig$ ./helloHello, world!

Usually you don't want to write to stdout. You want to write to stderr. And you don't care if it fails. It's more like awarning message that you want to emit. For that you can use a simpler API:

hello.zig

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

Note that we also left off the! from the return type. In Zig, if your main function cannot fail, you must use thevoid return type.

See also:

Comments

comments.zig

const assert =@import("std").debug.assert;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.//assert(false);const x =true;// another comment    assert(x);}
$ zig test comments.zigTest 1/1 comments...OKAll 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 have a doc comment in an unexpected place, such as in the middle of an expression, or just before a non-doc comment.

Values

values.zig

const std =@import("std");const warn = std.debug.warn;const os = std.os;const assert = std.debug.assert;pubfnmain()void {// integersconst one_plus_one:i32 =1 +1;    warn("1 + 1 = {}\n", one_plus_one);// floatsconst seven_div_three:f32 =7.0 /3.0;    warn("7.0 / 3.0 = {}\n", seven_div_three);// boolean    warn("{}\n{}\n{}\n",trueandfalse,trueorfalse,        !true);// optionalvar optional_value: ?[]constu8 =null;    assert(optional_value ==null);    warn("\noptional 1\ntype: {}\nvalue: {}\n",@typeName(@typeOf(optional_value)), optional_value);    optional_value ="hi";    assert(optional_value !=null);    warn("\noptional 2\ntype: {}\nvalue: {}\n",@typeName(@typeOf(optional_value)), optional_value);// error unionvar number_or_error:error!i32 =error.ArgNotFound;    warn("\nerror union 1\ntype: {}\nvalue: {}\n",@typeName(@typeOf(number_or_error)), number_or_error);     number_or_error =1234;    warn("\nerror union 2\ntype: {}\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: error!i32value: error.ArgNotFounderror union 2type: error!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
error(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.

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

test.zig

const assert =@import("std").debug.assert;const mem =@import("std").mem;test"string literals" {// In Zig a string literal is an array of bytes.const normal_bytes ="hello";    assert(@typeOf(normal_bytes) == [5]u8);    assert(normal_bytes.len ==5);    assert(normal_bytes[1] =='e');    assert('e' =='\x65');    assert(mem.eql(u8,"hello","h\x65llo"));// A C string literal is a null terminated pointer.const null_terminated_bytes =c"hello";    assert(@typeOf(null_terminated_bytes) == [*]constu8);    assert(null_terminated_bytes[5] ==0);}
$ zig test test.zigTest 1/1 string literals...OKAll tests passed.

See also:

Escape Sequences

Escape Sequence Name
\nNewline
\rCarriage Return
\tTab
\\Backslash
\'Single Quote
\"Double Quote
\xNNhexadecimal 8-bit character code (2 digits)
\uNNNNhexadecimal 16-bit Unicode character code UTF-8 encoded (4 digits)
\UNNNNNNhexadecimal 24-bit Unicode character code UTF-8 encoded (6 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;\\};

For a multiline C string literal, prependc to each\\:

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

In this example the variablec_string_literal has type[*]constu8 and has a terminating null byte.

See also:

Assignment

Use theconst keyword to assign a value to an identifier:

test.zig

const x =1234;fnfoo()void {// It works at global 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/home/andy/dev/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 assert =@import("std").debug.assert;test"var" {var y:i32 =5678;    y +=1;    assert(y ==5679);}
$ zig test test.zigTest 1/1 var...OKAll tests passed.

Variables must be initialized:

test.zig

test"initialization" {var x:i32;    x =1;}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:2:5:error: variables must be initialized    var x: i32;^/home/andy/dev/zig/docgen_tmp/test.zig:4:5:error: use of undeclared identifier 'x'    x = 1;^

undefined

Useundefined to leave variables uninitialized:

test.zig

const assert =@import("std").debug.assert;test"init with undefined" {var x:i32 =undefined;    x =1;    assert(x ==1);}
$ zig test test.zigTest 1/1 init with undefined...OKAll tests passed.

undefined can beimplicitly cast 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.

Integers

Integer Literals

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

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 both integer overflow and division 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.

See also:

Floats

Zig has the following floating point types:

Float Literals

Float literals have typecomptime_float which is guaranteed to hold at least all possible values that the largest other floating point type can hold. Float literalsimplicitly cast to any other type.

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;

Floating Point Operations

By default floating point operations useStrict mode, but you can switch toOptimized mode on a per-block basis:

foo.zig

const builtin =@import("builtin");const big =f64(1 <<40);exportfnfoo_strict(x:f64)f64 {return x + big - big;}exportfnfoo_optimized(x:f64)f64 {@setFloatMode(builtin.FloatMode.Optimized);return x + big - big;}
$ zig build-obj foo.zig --release-fast

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

float_mode.zig

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

See also:

Operators

Table of Operators

Syntax Relevant Types Description Example
a + ba += b
Addition.
2 +5 ==7
a +% ba +%= b
Wrapping Addition.
u32(@maxValue(u32)) +%1 ==0
a - ba -= b
Subtraction.
2 -5 == -3
a -% ba -%= b
Wrapping Subtraction.
u32(0) -%1 ==@maxValue(u32)
-a
Negation.
-1 ==0 -1
-%a
Wrapping Negation.
  • Guaranteed to have twos-complement wrapping behavior.
-%i32(@minValue(i32)) ==@minValue(i32)
a * ba *= b
Multiplication.
2 *5 ==10
a *% ba *%= b
Wrapping Multiplication.
u8(200) *%2 ==144
a / ba /= b
Divison.
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 &amp;0b101 ==0b001
a | ba |= b
Bitwise OR.
0b010 |0b100 ==0b110
a ^ ba ^= b
Bitwise XOR.
0b011 ^0b101 ==0b110
~a
Bitwise NOT.
~u8(0b0101111) ==0b1010000
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:error!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;x.* ==1234
&amp;a
All types Address of.
const x:u32 =1234;const ptr = &x;x.* ==1234
a || b
Merging Error Sets
const A =error{One};const B =error{Two};(A || B) ==error{One, Two}

Precedence

x() x[] x.ya!b!x -x -%x ~x &x ?xx{} x.* x.?! * / % ** *% ||+ - ++ +% -%<< >>&^|== != < > <= >=andororelsecatch= *= /= %= += -= <<= >>= &= ^= |=

Arrays

arrays.zig

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 an array literalconst same_message ="hello";comptime {    assert(mem.eql(u8, message, same_message));    assert(@typeOf(message) ==@typeOf(same_message));}test"iterate over an array" {var sum:usize =0;for (message) |byte| {        sum += byte;    }    assert(sum ==usize('h') +usize('e') +usize('l') *2 +usize('o'));}// modifiable arrayvar some_integers: [100]i32 =undefined;test"modify an array" {for (some_integers) |*item, i| {        item.* =@intCast(i32, i);    }    assert(some_integers[10] ==10);    assert(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 initalization" {    assert(fancy_array[4].x ==4);    assert(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" {    assert(more_points[4].x ==3);    assert(more_points[4].y ==6);    assert(more_points.len ==10);}
$ zig test arrays.zigTest 1/4 iterate over an array...OKTest 2/4 modify an array...OKTest 3/4 compile-time array initalization...OKTest 4/4 array initialization with function calls...OKAll tests passed.

See also:

Pointers

test.zig

const assert =@import("std").debug.assert;test"address of syntax" {// Get the address of a variable:const x:i32 =1234;const x_ptr = &x;// Deference a pointer:    assert(x_ptr.* ==1234);// When you get the address of a const variable, you get a const pointer.    assert(@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;    assert(@typeOf(y_ptr) == *i32);    y_ptr.* +=1;    assert(y_ptr.* ==5679);}test"pointer array access" {// Taking an address of an individual element gives a// pointer to a single item. 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];    assert(@typeOf(ptr) == *u8);    assert(array[2] ==3);    ptr.* +=1;    assert(array[2] ==4);}test"pointer slicing" {// In Zig, we prefer slices over pointers to null-terminated arrays.// You can turn an array into a slice using slice syntax:var array = []u8{1,2,3,4,5,6,7,8,9,10 };const slice = array[2..4];    assert(slice.len ==2);// Slices have bounds checking and are therefore protected// against this kind of undefined behavior. This is one reason// we prefer slices to pointers.    assert(array[3] ==4);    slice[1] +=1;    assert(array[3] ==5);}comptime {// Pointers work at compile-time too, as long as you don't use// @ptrCast.var x:i32 =1;const ptr = &x;    ptr.* +=1;    x +=1;    assert(ptr.* ==3);}test"@ptrToInt and @intToPtr" {// To convert an integer address into a pointer, use @intToPtr:const ptr =@intToPtr(*i32,0xdeadbeef);// To convert a pointer to an integer, use @ptrToInt:const addr =@ptrToInt(ptr);    assert(@typeOf(addr) ==usize);    assert(addr ==0xdeadbeef);}comptime {// Zig is able to do this at compile-time, as long as// ptr is never dereferenced.const ptr =@intToPtr(*i32,0xdeadbeef);const addr =@ptrToInt(ptr);    assert(@typeOf(addr) ==usize);    assert(addr ==0xdeadbeef);}test"volatile" {// In Zig, 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), use `volatile`:const mmio_ptr =@intToPtr(*volatileu8,0x12345678);// Now loads and stores with mmio_ptr are guaranteed to all happen// and in the same order as in source code.    assert(@typeOf(mmio_ptr) == *volatileu8);}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;    assert(ptr.?.* ==1);// Optional pointers are the same size as normal pointers, because pointer// value 0 is used as the null value.    assert(@sizeOf(?*i32) ==@sizeOf(*i32));}test"pointer casting" {// 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.const bytesalign(@alignOf(u32)) = []u8{0x12,0x12,0x12,0x12 };const u32_ptr =@ptrCast(*constu32, &bytes);    assert(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 =@bytesToSlice(u32, bytes[0..])[0];    assert(u32_value ==0x12121212);// And even another way, the most straightforward way to do it:    assert(@bitCast(u32, bytes) ==0x12121212);}test"pointer child type" {// pointer types have a `child` field which tells you the type they point to.    assert((*u32).Child ==u32);}
$ zig test test.zigTest 1/8 address of syntax...OKTest 2/8 pointer array access...OKTest 3/8 pointer slicing...OKTest 4/8 @ptrToInt and @intToPtr...OKTest 5/8 volatile...OKTest 6/8 optional pointers...OKTest 7/8 pointer casting...OKTest 8/8 pointer child type...OKAll 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 assert =@import("std").debug.assert;const builtin =@import("builtin");test"variable alignment" {var x:i32 =1234;const align_of_i32 =@alignOf(@typeOf(x));    assert(@typeOf(&x) == *i32);    assert(*i32 == *align(align_of_i32)i32);if (builtin.arch == builtin.Arch.x86_64) {        assert((*i32).alignment ==4);    }}
$ zig test test.zigTest 1/1 variable alignment...OKAll tests passed.

In the same way that a*i32 can beimplicitly cast 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 assert =@import("std").debug.assert;var foo:u8align(4) =100;test"global variable alignment" {    assert(@typeOf(&foo).alignment ==4);    assert(@typeOf(&foo) == *align(4)u8);const slice = (*[1]u8)(&foo)[0..];    assert(@typeOf(slice) == []align(4)u8);}fnderp()align(@sizeOf(usize) *2)i32 {return1234; }fnnoop1()align(1)void {}fnnoop4()align(4)void {}test"function alignment" {    assert(derp() ==1234);    assert(@typeOf(noop1) ==fn()align(1)void);    assert(@typeOf(noop4) ==fn()align(4)void);    noop1();    noop4();}
$ zig test test.zigTest 1/2 global variable alignment...OKTest 2/2 function alignment...OKAll 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 assert =@import("std").debug.assert;test"pointer alignment safety" {var arrayalign(4) = []u32{0x11111111,0x11111111 };const bytes =@sliceToBytes(array[0..]);    assert(foo(bytes) ==0x11111111);}fnfoo(bytes: []u8)u32 {const slice4 = bytes[1..5];const int_slice =@bytesToSlice(u32,@alignCast(4, slice4));return int_slice[0];}
$ zig test test.zigTest 1/1 pointer alignment safety...incorrect alignment/home/andy/dev/zig/docgen_tmp/test.zig:10:56:0x2052bf in ??? (test)    const int_slice = @bytesToSlice(u32, @alignCast(4, slice4));^/home/andy/dev/zig/docgen_tmp/test.zig:6:15:0x2050a7 in ??? (test)    assert(foo(bytes) == 0x11111111);^/home/andy/dev/zig/build/lib/zig/std/special/test_runner.zig:13:25:0x222aca in ??? (test)        if (test_fn.func()) |_| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:96:22:0x22287b in ??? (test)            root.main() catch |err| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x2227f5 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x222658 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222510 in ??? (test)    @noInlineCall(posixCallMainAndExit);^Tests failed. Use the following command to reproduce the failure:/home/andy/dev/zig/docgen_tmp/test

Type Based Alias Analysis

Zig uses Type Based Alias Analysis (also known as Strict Aliasing) to perform some optimizations. This means that pointers of different types must not alias the same memory, with the exception ofu8. Pointers tou8 can alias any memory.

As an example, this code produces undefined behavior:

@ptrCast(*u32,f32(12.34)).*

Instead, use@bitCast:

@bitCast(u32,f32(12.34))

As an added benefit, the@bitCast version works at compile-time.

See also:

Slices

test.zig

const assert =@import("std").debug.assert;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.const slice = array[0..array.len];    assert(&slice[0] == &array[0]);    assert(slice.len == array.len);// Using the address-of operator on a slice gives a pointer to a single// item, while using the `ptr` field gives an unknown length pointer.    assert(@typeOf(slice.ptr) == [*]i32);    assert(@typeOf(&slice[0]) == *i32);    assert(@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.zigTest 1/1 basic slices...index out of bounds/home/andy/dev/zig/docgen_tmp/test.zig:21:10:0x205156 in ??? (test)    slice[10] += 1;^/home/andy/dev/zig/build/lib/zig/std/special/test_runner.zig:13:25:0x222a8a in ??? (test)        if (test_fn.func()) |_| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:96:22:0x22283b in ??? (test)            root.main() catch |err| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x2227b5 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x222618 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x2224d0 in ??? (test)    @noInlineCall(posixCallMainAndExit);^Tests failed. Use the following command to reproduce the failure:/home/andy/dev/zig/docgen_tmp/test

This is one reason we prefer slices to pointers.

slices.zig

const assert =@import("std").debug.assert;const mem =@import("std").mem;const fmt =@import("std").fmt;test"using slices for strings" {// Zig has no concept of strings. String literals are arrays of u8, and// in general the string type is []u8 (slice of u8).// Here we implicitly cast [5]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,"{} {}", 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.    assert(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;    assert(slice[2] ==3);// The slice is mutable because we sliced a mutable pointer.    assert(@typeOf(slice) == []u8);// You can also slice a slice:const slice2 = slice[2..3];    assert(slice2.len ==1);    assert(slice2[0] ==3);}test"slice widening" {// Zig supports slice widening and slice narrowing. Cast a slice of u8// to a slice of anything else, and Zig will perform the length conversion.const arrayalign(@alignOf(u32)) = []u8{0x12,0x12,0x12,0x12,0x13,0x13,0x13,0x13 };const slice =@bytesToSlice(u32, array[0..]);    assert(slice.len ==2);    assert(slice[0] ==0x12121212);    assert(slice[1] ==0x13131313);}
$ zig test slices.zigTest 1/3 using slices for strings...OKTest 2/3 slice pointer...OKTest 3/3 slice widening...OKAll tests passed.

See also:

struct

structs.zig

// Declare a struct.// Zig gives no guarantees about the order of fields and whether or// not there will be padding.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: *const Vec3, other: *const Vec3)f32 {return self.x * other.x + self.y * other.y + self.z * other.z;    }};const assert =@import("std").debug.assert;test"dot product" {const v1 = Vec3.init(1.0,0.0,0.0);const v2 = Vec3.init(0.0,1.0,0.0);    assert(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:    assert(Vec3.dot(v1, v2) ==0.0);}// Structs can have global declarations.// Structs can have 0 fields.const Empty =struct {pubconst PI =3.14;};test"struct namespaced variable" {    assert(Empty.PI ==3.14);    assert(@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);    assert(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:    assert(LinkedList(i32) == LinkedList(i32));var list = LinkedList(i32) {        .first =null,        .last =null,        .len =0,    };    assert(list.len ==0);// Since types are first class values you can instantiate the type// by assigning it to a variable:const ListOfInts = LinkedList(i32);    assert(ListOfInts == LinkedList(i32));var node = ListOfInts.Node {        .prev =null,        .next =null,        .data =1234,    };var list2 = LinkedList(i32) {        .first = &node,        .last = &node,        .len =1,    };    assert(list2.first.?.data ==1234);}
$ zig test structs.zigTest 1/4 dot product...OKTest 2/4 struct namespaced variable...OKTest 3/4 field parent pointer...OKTest 4/4 linked list...OKAll tests passed.

packed struct

packed structs have guaranteed in-memory layout.

TODO bit fields

TODO alignment

TODO endianness

TODO @bitOffsetOf and @byteOffsetOf

TODO mention how volatile loads and stores of bit packed fields could be more efficient when done by hand instead of with packed struct

struct Naming

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

struct_name.zig

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

See also:

enum

enums.zig

const assert =@import("std").debug.assert;const mem =@import("std").mem;// Declare an enum.const Type =enum {    Ok,    NotOk,};// 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" {    assert(@enumToInt(Value.Zero) ==0);    assert(@enumToInt(Value.One) ==1);    assert(@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" {    assert(@enumToInt(Value2.Hundred) ==100);    assert(@enumToInt(Value2.Thousand) ==1000);    assert(@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;    assert(!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",    };    assert(mem.eql(u8, what_is_it,"this is a number"));}// @TagType can be used to access the integer tag type of an enum.const Small =enum {    One,    Two,    Three,    Four,};test"@TagType" {    assert(@TagType(Small) ==u2);}// @memberCount tells how many fields an enum has:test"@memberCount" {    assert(@memberCount(Small) ==4);}// @memberName tells the name of a field in an enum:test"@memberName" {    assert(mem.eql(u8,@memberName(Small,1),"Two"));}// @tagName gives a []const u8 representation of an enum value:test"@tagName" {    assert(mem.eql(u8,@tagName(Small.Three),"Three"));}
$ zig test enums.zigTest 1/8 enum ordinal value...OKTest 2/8 set enum ordinal value...OKTest 3/8 enum method...OKTest 4/8 enum variant switch...OKTest 5/8 @TagType...OKTest 6/8 @memberCount...OKTest 7/8 @memberName...OKTest 8/8 @tagName...OKAll tests passed.

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/home/andy/dev/zig/docgen_tmp/test.zig:2:22:error: parameter of type 'Foo' not allowed in function with calling convention 'ccc'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

packed enum

By default, the size of enums is not guaranteed.

packedenum causes the size of the enum to be the same as the size of the integer tag type of the enum:

test.zig

const std =@import("std");test"packed enum" {const Number =packedenum(u8) {        One,        Two,        Three,    };    std.debug.assert(@sizeOf(Number) ==@sizeOf(u8));}
$ zig test test.zigTest 1/1 packed enum...OKAll tests passed.

See also:

union

union.zig

const assert =@import("std").debug.assert;const mem =@import("std").mem;// A union has only 1 active field at a time.const Payload =union {    Int:i64,    Float:f64,    Bool:bool,};test"simple union" {var payload = Payload {.Int =1234};// payload.Float = 12.34; // ERROR! field not active    assert(payload.Int ==1234);// You can activate another field by assigning the entire union.    payload = Payload {.Float =12.34};    assert(payload.Float ==12.34);}// Unions can be given an enum tag type:const ComplexTypeTag =enum { Ok, NotOk };const ComplexType =union(ComplexTypeTag) {    Ok:u8,    NotOk:void,};// Declare a specific instance of the union variant.test"declare union value" {const c = ComplexType { .Ok =0 };    assert(ComplexTypeTag(c) == ComplexTypeTag.Ok);}// @TagType can be used to access the enum tag type of a union.test"@TagType" {    assert(@TagType(ComplexType) == ComplexTypeTag);}// Unions can be made to infer the enum tag type.const Foo =union(enum) {    String: []constu8,    Number:u64,// void can be omitted when inferring enum tag type.    None,};test"union variant switch" {const p = Foo { .Number =54 };const what_is_it =switch (p) {// Capture by reference        Foo.String => |*x| blk: {break :blk"this is a string";        },// Capture by value        Foo.Number => |x| blk: {            assert(x ==54);break :blk"this is a number";        },        Foo.None => blk: {break :blk"this is a none";        },    };    assert(mem.eql(u8, what_is_it,"this is a number"));}// Unions can have methods just like structs and enums:const Variant =union(enum) {    Int:i32,    Bool:bool,fntruthy(self: *const Variant)bool {returnswitch (self.*) {            Variant.Int => |x_int| x_int !=0,            Variant.Bool => |x_bool| x_bool,        };    }};test"union method" {var v1 = Variant { .Int =1 };var v2 = Variant { .Bool =false };    assert(v1.truthy());    assert(!v2.truthy());}const Small =union {    A:i32,    B:bool,    C:u8,};// @memberCount tells how many fields a union has:test"@memberCount" {    assert(@memberCount(Small) ==3);}// @memberName tells the name of a field in an enum:test"@memberName" {    assert(mem.eql(u8,@memberName(Small,1),"B"));}// @tagName gives a []const u8 representation of an enum value,// but only if the union has an enum tag type.const Small2 =union(enum) {    A:i32,    B:bool,    C:u8,};test"@tagName" {    assert(mem.eql(u8,@tagName(Small2.C),"C"));}
$ zig test union.zigTest 1/8 simple union...OKTest 2/8 declare union value...OKTest 3/8 @TagType...OKTest 4/8 union variant switch...OKTest 5/8 union method...OKTest 6/8 @memberCount...OKTest 7/8 @memberName...OKTest 8/8 @tagName...OKAll tests passed.

Unions with an enum tag are generated as a struct with a tag field and union field. Zig sorts the order of the tag and union field by the largest alignment.

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/home/andy/dev/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 assert = std.debug.assert;test"labeled break from labeled block expression" {var y:i32 =123;const x = blk: {        y +=1;break :blk y;    };    assert(x ==124);    assert(y ==124);}
$ zig test test.zigTest 1/1 labeled break from labeled block expression...OKAll tests passed.

Here,blk can be any name.

See also:

switch

switch.zig

const assert =@import("std").debug.assert;const builtin =@import("builtin");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,    };    assert(b ==1);}test"switch enum" {const Item =union(enum) {        A:u32,        C:struct { x:u8, y:u8 },        D,    };var a = Item { .A =3 };// 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.        Item.A => |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,    };    assert(b ==3);}// Switch expressions can be used outside a function:const os_msg =switch (builtin.os) {    builtin.Os.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 (builtin.os) {        builtin.Os.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/3 switch simple...OKTest 2/3 switch enum...OKTest 3/3 switch inside function...OKAll tests passed.

See also:

while

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

while.zig

const assert =@import("std").debug.assert;test"while basic" {var i:usize =0;while (i <10) {        i +=1;    }    assert(i ==10);}
$ zig test while.zigTest 1/1 while basic...OKAll tests passed.

Usebreak to exit a while loop early.

while.zig

const assert =@import("std").debug.assert;test"while break" {var i:usize =0;while (true) {if (i ==10)break;        i +=1;    }    assert(i ==10);}
$ zig test while.zigTest 1/1 while break...OKAll tests passed.

Usecontinue to jump back to the beginning of the loop.

while.zig

const assert =@import("std").debug.assert;test"while continue" {var i:usize =0;while (true) {        i +=1;if (i <10)continue;break;    }    assert(i ==10);}
$ zig test while.zigTest 1/1 while continue...OKAll tests passed.

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

while.zig

const assert =@import("std").debug.assert;test"while loop continue expression" {var i:usize =0;while (i <10) : (i +=1) {}    assert(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;        assert(my_ij <2000);    }}
$ zig test while.zigTest 1/2 while loop continue expression...OKTest 2/2 while loop continue expression, more complicated...OKAll 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 assert =@import("std").debug.assert;test"while else" {    assert(rangeHasNumber(0,10,5));    assert(!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 while else...OKAll 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 nested break...OKTest 2/2 nested continue...OKAll 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 assert =@import("std").debug.assert;test"while null capture" {var sum1:u32 =0;    numbers_left =3;while (eventuallyNullSequence()) |value| {        sum1 += value;    }    assert(sum1 ==3);var sum2:u32 =0;    numbers_left =3;while (eventuallyNullSequence()) |value| {        sum2 += value;    }else {        assert(sum1 ==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 while null capture...OKAll 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 assert =@import("std").debug.assert;test"while error union capture" {var sum1:u32 =0;    numbers_left =3;while (eventuallyErrorSequence()) |value| {        sum1 += value;    }else |err| {        assert(err ==error.ReachedZero);    }}var numbers_left:u32 =undefined;fneventuallyErrorSequence()error!u32 {returnif (numbers_left ==0)error.ReachedZeroelse blk: {        numbers_left -=1;break :blk numbers_left;    };}
$ zig test while.zigTest 1/1 while error union capture...OKAll 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 assert =@import("std").debug.assert;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);    }    assert(sum ==9);}fntypeNameLength(comptime T:type)usize {return@typeName(T).len;}
$ zig test test.zigTest 1/1 inline while loop...OKAll tests passed.

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

See also:

for

for.zig

const assert =@import("std").debug.assert;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;    }    assert(sum ==16);// To iterate over a portion of a slice, reslice.for (items[0..1]) |value| {        sum += value;    }    assert(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| {        assert(@typeOf(i) ==usize);        sum2 +=@intCast(i32, i);    }    assert(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;    }    assert(items[0] ==4);    assert(items[1] ==5);    assert(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.var sum:i32 =0;const result =for (items) |value| {if (value ==null) {break9;        }else {            sum += value.?;        }    }else blk: {        assert(sum ==7);break :blk sum;    };}
$ zig test for.zigTest 1/3 for basics...OKTest 2/3 for reference...OKTest 3/3 for else...OKAll 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 assert = std.debug.assert;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;        }    }    assert(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;        }    }    assert(count ==8);}
$ zig test test.zigTest 1/2 nested break...OKTest 2/2 nested continue...OKAll 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 assert =@import("std").debug.assert;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);    }    assert(sum ==9);}fntypeNameLength(comptime T:type)usize {return@typeName(T).len;}
$ zig test test.zigTest 1/1 inline for loop...OKAll tests passed.

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

See also:

if

if.zig

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

See also:

defer

defer.zig

const std =@import("std");const assert = std.debug.assert;const warn = std.debug.warn;// defer will execute an expression at the end of the current scope.fndeferExample()usize {var a:usize =1;    {defer a =2;        a =1;    }    assert(a ==2);    a =5;return a;}test"defer basics" {    assert(deferExample() ==5);}// If multiple defer statements are specified, they will be executed in// the reverse order they were run.fndeferUnwindExample()void {    warn("\n");defer {        warn("1 ");    }defer {        warn("2 ");    }if (false) {// defers are not run if they are never executed.defer {            warn("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 {    warn("\nstart of function\n");// This will always be executed on exitdefer {        warn("end of function\n");    }errdefer {        warn("encountered an error!\n");    }if (is_error) {returnerror.DeferError;    }}test"errdefer unwinding" {    _ = deferErrorExample(false);    _ = deferErrorExample(true);}
$ zig test defer.zigTest 1/3 defer basics...OKTest 2/3 defer unwinding...2 1 OKTest 3/3 errdefer unwinding...start of functionend of functionstart of functionencountered an error!end of functionOKAll 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 basic math...OKAll tests passed.

In fact, this is how assert is implemented:

test.zig

fnassert(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 this will fail...reached unreachable code/home/andy/dev/zig/docgen_tmp/test.zig:2:14:0x2051c9 in ??? (test)    if (!ok) unreachable; // assertion failure^/home/andy/dev/zig/docgen_tmp/test.zig:7:11:0x20504b in ??? (test)    assert(false);^/home/andy/dev/zig/build/lib/zig/std/special/test_runner.zig:13:25:0x22297a in ??? (test)        if (test_fn.func()) |_| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:96:22:0x22272b in ??? (test)            root.main() catch |err| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x2226a5 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x222508 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x2223c0 in ??? (test)    @noInlineCall(posixCallMainAndExit);^Tests failed. Use the following command to reproduce the failure:/home/andy/dev/zig/docgen_tmp/test

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/home/andy/dev/zig/docgen_tmp/test.zig:10:16:error: unreachable code        assert(@typeOf(unreachable) == noreturn);^

See also:

noreturn

noreturn is the type of:

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 noreturn...OKAll tests passed.

Another use case fornoreturn is theexit function:

test.zig

pubextern"kernel32"stdcallccfnExitProcess(exit_code:c_uint)noreturn;test"foo" {const value = bar()catch ExitProcess(1);    assert(value ==1234);}fnbar()error!u32 {return1234;}const assert =@import("std").debug.assert;
$ zig test test.zigCreated /home/andy/dev/zig/docgen_tmp/test but skipping execution because it is non-native.

Functions

functions.zig

const assert =@import("std").debug.assert;// Functions are declared like thisfnadd(a:i8, b:i8)i8 {if (a ==0) {// You can still return manually if needed.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 stdcallcc specifier changes the calling convention of the function.extern"kernel32"stdcallccfnExitProcess(exit_code:u32)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) {}}// nakedcc makes a function not have any function prologue or epilogue.// This can be useful when integrating with assembly.nakedccfn_start()noreturn {    abort();}// 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" {    assert(do_op(add,5,6) ==11);    assert(do_op(sub2,5,6) == -1);}
$ zig test functions.zigTest 1/1 function...OKAll 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

In Zig, structs, unions, and enums with payloads can be passed directly to a function:

test.zig

const Point =struct {    x:i32,    y:i32,};fnfoo(point: Point)i32 {return point.x + point.y;}const assert =@import("std").debug.assert;test"pass aggregate type by non-copy value to function" {    assert(foo(Point{ .x =1, .y =2 }) ==3);}
$ zig test test.zigTest 1/1 pass aggregate type by non-copy value to function...OKAll tests passed.

In this case, the value may be passed by reference, or by value, whichever way Zig decides will be faster.

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

Function Reflection

test.zig

const assert =@import("std").debug.assert;test"fn reflection" {    assert(@typeOf(assert).ReturnType ==void);    assert(@typeOf(assert).is_var_args ==false);}
$ zig test test.zigTest 1/1 fn reflection...OKAll 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 canimplicitly cast an error from a subset to its superset:

test.zig

const std =@import("std");const FileOpenError =error {    AccessDenied,    OutOfMemory,    FileNotFound,};const AllocationError =error {    OutOfMemory,};test"implicit cast subset to superset" {const err = foo(AllocationError.OutOfMemory);    std.debug.assert(err == FileOpenError.OutOfMemory);}fnfoo(err: AllocationError) FileOpenError {return err;}
$ zig test test.zigTest 1/1 implicit cast subset to superset...OKAll tests passed.

But you cannot implicitly cast an error from a superset to a subset:

test.zig

const FileOpenError =error {    AccessDenied,    OutOfMemory,    FileNotFound,};const AllocationError =error {    OutOfMemory,};test"implicit cast superset to subset" {    foo(FileOpenError.OutOfMemory)catch {};}fnfoo(err: FileOpenError) AllocationError {return err;}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:16:12:error: expected type 'AllocationError', found 'FileOpenError'    return err;^/home/andy/dev/zig/docgen_tmp/test.zig:2:5:note: 'error.AccessDenied' not a member of destination error set    AccessDenied,^/home/andy/dev/zig/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

error 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 can implicitly cast any error set to the global one, and you can explicitly cast an error of 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

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 =>@maxValue(u8),    };}test"parse u64" {const result =try parseU64("1234",10);@import("std").debug.assert(result ==1234);}
$ zig test test.zigTest 1/1 parse u64...OKAll 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 typesimplicitly cast toerror!u64.

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

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:

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 assert =@import("std").debug.assert;test"error union" {var foo:error!i32 =undefined;// Implicitly cast from child type of an error union:    foo =1234;// Implicitly cast from an error set:    foo =error.SomeError;// Use compile-time reflection to access the payload type of an error union:comptime assert(@typeOf(foo).Payload ==i32);// Use compile-time reflection to access the error set type of an error union:comptime assert(@typeOf(foo).ErrorSet ==error);}
$ zig test test.zigTest 1/1 error union...OKAll 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 merge error sets...OKAll 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 inferred error set...OKAll 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/dev/zig/docgen_tmp/test.zig:39:5:0x222bc9 in ??? (test)    return error.FileNotFound;^/home/andy/dev/zig/docgen_tmp/test.zig:23:5:0x222aad in ??? (test)    try bang1();^/home/andy/dev/zig/docgen_tmp/test.zig:43:5:0x222a79 in ??? (test)    return error.PermissionDenied;^/home/andy/dev/zig/docgen_tmp/test.zig:31:5:0x222b9d in ??? (test)    try bang2();^/home/andy/dev/zig/docgen_tmp/test.zig:17:31:0x222a49 in ??? (test)        error.FileNotFound => try hello(),^/home/andy/dev/zig/docgen_tmp/test.zig:7:9:0x22293a in ??? (test)        try bar();^/home/andy/dev/zig/docgen_tmp/test.zig:2:5:0x2227e2 in ??? (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$ ./testPermissionDenied/home/andy/dev/zig/docgen_tmp/test.zig:38:5:0x222734 in ??? (test)    @panic("PermissionDenied");^/home/andy/dev/zig/docgen_tmp/test.zig:30:10:0x222779 in ??? (test)    bang2();^/home/andy/dev/zig/docgen_tmp/test.zig:17:14:0x22271b in ??? (test)        hello();^/home/andy/dev/zig/docgen_tmp/test.zig:7:12:0x2226e6 in ??? (test)        bar();^/home/andy/dev/zig/docgen_tmp/test.zig:2:8:0x2226ce in ??? (test)    foo(12);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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:

Implementation Details

To analyze performance cost, there are two cases:

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 assert =@import("std").debug.assert;test"optional type" {// Declare an optional and implicitly cast from null:var foo: ?i32 =null;// Implicitly cast from child type of an optional    foo =1234;// Use compile-time reflection to access the child type of the optional:comptime assert(@typeOf(foo).Child ==i32);}
$ zig test test.zigTest 1/1 optional type...OKAll 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;

Casting

Atype cast converts a value of one type to another. Zig hasImplicit Casts 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.

Implicit Casts

An implicit cast occurs when one type is expected, but different type is provided:

test.zig

test"implicit cast - variable declaration" {var a:u8 =1;var b:u16 = a;}test"implicit cast - function call" {var a:u8 =1;    foo(a);}fnfoo(b:u16)void {}test"implicit cast - invoke a type as a function" {var a:u8 =1;var b =u16(a);}
$ zig test test.zigTest 1/3 implicit cast - variable declaration...OKTest 2/3 implicit cast - function call...OKTest 3/3 implicit cast - invoke a type as a function...OKAll tests passed.

Implicit casts are only allowed when it is completely unambiguous how to get from one type to another, and the transformation is guaranteed to be safe.

Implicit Cast: 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:

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

test.zig

test"implicit cast - const qualification" {var a:i32 =1;var b: *i32 = &a;    foo(b);}fnfoo(a: *consti32)void {}
$ zig test test.zigTest 1/1 implicit cast - const qualification...OKAll tests passed.

In addition, pointers implicitly cast to const optional pointers:

test.zig

const std =@import("std");const assert = std.debug.assert;const mem = std.mem;test"cast *[1][*]const u8 to [*]const ?[*]const u8" {const window_name = [1][*]constu8{c"window name"};const x: [*]const ?[*]constu8 = &window_name;    assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?),"window name"));}
$ zig test test.zigTest 1/1 cast *[1][*]const u8 to [*]const ?[*]const u8...OKAll tests passed.

Implicit Cast: Integer and Float Widening

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

test.zig

const std =@import("std");const assert = std.debug.assert;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;    assert(f == a);}test"implicit unsigned integer to signed integer" {var a:u8 =250;var b:i16 = a;    assert(b ==250);}test"float widening" {var a:f16 =12.34;var b:f32 = a;var c:f64 = b;var d:f128 = c;    assert(d == a);}
$ zig test test.zigTest 1/3 integer widening...OKTest 2/3 implicit unsigned integer to signed integer...OKTest 3/3 float widening...OKAll tests passed.

Implicit Cast: Arrays

TODO: [N]T to []const T

TODO: *const [N]T to []const T

TODO: [N]T to *const []const T

TODO: [N]T to ?[]const T

TODO: *[N]T to []T

TODO: *[N]T to [*]T

TODO: *[N]T to ?[*]T

TODO: *T to *[1]T

TODO: [N]T to E![]const T

Implicit Cast: Optionals

TODO: T to ?T

TODO: T to E!?T

TODO: null to ?T

Implicit Cast: T to E!T

TODO

Implicit Cast: E to E!T

TODO

Implicit Cast: comptime_int to *const integer

TODO

Implicit Cast: comptime_float to *const float

TODO

Implicit Cast: compile-time known numbers

TODO

Implicit Cast: union to enum

TODO

Implicit Cast: enum to union

TODO

Implicit Cast: T to *T when @sizeOf(T) == 0

TODO

Implicit Cast: undefined

TODO

Implicit Cast: T to *const T

TODO

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.

Peer Type Resolution

Peer Type Resolution occurs in these places:

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

test.zig

const std =@import("std");const assert = std.debug.assert;const mem = std.mem;test"peer resolve int widening" {var a:i8 =12;var b:i16 =34;var c = a + b;    assert(c ==46);    assert(@typeOf(c) ==i16);}test"peer resolve arrays of different size to const slice" {    assert(mem.eql(u8, boolToStr(true),"true"));    assert(mem.eql(u8, boolToStr(false),"false"));comptime assert(mem.eql(u8, boolToStr(true),"true"));comptime assert(mem.eql(u8, boolToStr(false),"false"));}fnboolToStr(b:bool) []constu8 {returnif (b)"true"else"false";}test"peer resolve array and const slice" {    testPeerResolveArrayConstSlice(true);comptime testPeerResolveArrayConstSlice(true);}fntestPeerResolveArrayConstSlice(b:bool)void {const value1 =if (b)"aoeu"else ([]constu8)("zz");const value2 =if (b) ([]constu8)("zz")else"aoeu";    assert(mem.eql(u8, value1,"aoeu"));    assert(mem.eql(u8, value2,"zz"));}test"peer type resolution: ?T and T" {    assert(peerTypeTAndOptionalT(true,false).? ==0);    assert(peerTypeTAndOptionalT(false,false).? ==3);comptime {        assert(peerTypeTAndOptionalT(true,false).? ==0);        assert(peerTypeTAndOptionalT(false,false).? ==3);    }}fnpeerTypeTAndOptionalT(c:bool, b:bool) ?usize {if (c) {returnif (b)nullelseusize(0);    }returnusize(3);}test"peer type resolution: [0]u8 and []const u8" {    assert(peerTypeEmptyArrayAndSlice(true,"hi").len ==0);    assert(peerTypeEmptyArrayAndSlice(false,"hi").len ==1);comptime {        assert(peerTypeEmptyArrayAndSlice(true,"hi").len ==0);        assert(peerTypeEmptyArrayAndSlice(false,"hi").len ==1);    }}fnpeerTypeEmptyArrayAndSlice(a:bool, slice: []constu8) []constu8 {if (a) {return []constu8{};    }return slice[0..1];}test"peer type resolution: [0]u8, []const u8, and error![]u8" {    {var data ="hi";const slice = data[0..];        assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len ==0);        assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len ==1);    }comptime {var data ="hi";const slice = data[0..];        assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len ==0);        assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len ==1);    }}fnpeerTypeEmptyArrayAndSliceAndError(a:bool, slice: []u8)error![]u8 {if (a) {return []u8{};    }return slice[0..1];}
$ zig test test.zigTest 1/6 peer resolve int widening...OKTest 2/6 peer resolve arrays of different size to const slice...OKTest 3/6 peer resolve array and const slice...OKTest 4/6 peer type resolution: ?T and T...OKTest 5/6 peer type resolution: [0]u8 and []const u8...OKTest 6/6 peer type resolution: [0]u8, []const u8, and error![]u8...OKAll tests passed.

void

void represents a type that has no value. Code that makes use of void values is not included in the final generated code:

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

When this turns into LLVM IR, there is no code generated in the body ofentry, even in debug 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 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 assert = std.debug.assert;test"turn HashMap into a set with void" {var map = std.HashMap(i32,void, hash_i32, eql_i32).init(std.debug.global_allocator);defer map.deinit();    _ =try map.put(1, {});    _ =try map.put(2, {});    assert(map.contains(2));    assert(!map.contains(3));    _ = map.remove(2);    assert(!map.contains(2));}fnhash_i32(x:i32)u32 {return@bitCast(u32, x);}fneql_i32(a:i32, b:i32)bool {return a == b;}
$ zig test test.zigTest 1/1 turn HashMap into a set with void...OKAll tests passed.

Note that this is different than 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, which is defined like this:pubconstc_void =@OpaqueType();.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/home/andy/dev/zig/docgen_tmp/test.zig:2:8:error: expression value is ignored    foo();^

However, if the expression has typevoid:

test.zig

test"ignoring expression value" {    foo();}fnfoo()void {}
$ zig test test.zigTest 1/1 ignoring expression value...OKAll tests passed.

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:

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/home/andy/dev/zig/docgen_tmp/test.zig:1:29:error: unable to evaluate constant expressionfn max(comptime T: type, a: T, b: T) T {^

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/home/andy/dev/zig/docgen_tmp/test.zig:2:18:error: operator not allowed for type 'bool'    return if (a > b) a else b;^/home/andy/dev/zig/docgen_tmp/test.zig:5:12:note: called from here    _ = max(bool, true, false);^

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" {@import("std").debug.assert(max(bool,false,true) ==true);}
$ zig test test.zigTest 1/1 try to compare bools...OKAll 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 assert =@import("std").debug.assert;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" {    assert(performFn('t',1) ==6);    assert(performFn('o',0) ==1);    assert(performFn('w',99) ==99);}
$ zig test comptime_vars.zigTest 1/1 perform fn...OKAll 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:// assert(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:// assert(performFn('o', 0) == 1);fnperformFn(start_value:i32)i32 {var result:i32 = start_value;    result = one(result);return result;}
// From the line:// assert(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/home/andy/dev/zig/docgen_tmp/test.zig:5:9:error: unable to evaluate constant expression        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:

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 assert =@import("std").debug.assert;fnfibonacci(index:u32)u32 {if (index <2)return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {// test fibonacci at run-time    assert(fibonacci(7) ==13);// test fibonacci at compile-timecomptime {        assert(fibonacci(7) ==13);    }}
$ zig test test.zigTest 1/1 fibonacci...OKAll tests passed.

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

test.zig

const assert =@import("std").debug.assert;fnfibonacci(index:u32)u32 {//if (index < 2) return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {comptime {        assert(fibonacci(7) ==13);    }}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:5:28:error: operation caused overflow    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:10:25:note: called from here        assert(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 assert =@import("std").debug.assert;fnfibonacci(index:i32)i32 {//if (index < 2) return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {comptime {        assert(fibonacci(7) ==13);    }}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:5:21:error: evaluation exceeded 1000 backwards branches    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^/home/andy/dev/zig/docgen_tmp/test.zig:5:21:note: called from here    return fibonacci(index - 1) + fibonacci(index - 2);^

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 theassert line?

test.zig

const assert =@import("std").debug.assert;fnfibonacci(index:i32)i32 {if (index <2)return index;return fibonacci(index -1) + fibonacci(index -2);}test"fibonacci" {comptime {        assert(fibonacci(7) ==99999);    }}
$ zig test test.zig/home/andy/dev/zig/build/lib/zig/std/debug/index.zig:118:13:error: encountered @panic at compile-time            @panic("assertion failure");^/home/andy/dev/zig/docgen_tmp/test.zig:10:15:note: called from here        assert(fibonacci(7) == 99999);^

What happened is Zig started interpreting theassert function with the parameterok set tofalse. When the interpreter hitunreachable it emitted a compile error, because reaching unreachable code is undefined behavior, and undefined behavior causes a compile error if it is detected at compile-time.

In the global scope (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" {@import("std").debug.assert(sum_of_first_25_primes ==1060);}
$ zig test test.zigTest 1/1 variable values...OKAll 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 warn =@import("std").debug.warn;const a_number:i32 =1234;const a_string ="foobar";pubfnmain()void {    warn("here is a string: '{}' 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: *OutStream,comptime format: []constu8, args: ...)error!void {const State =enum {        Start,        OpenBrace,        CloseBrace,    };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.OpenBrace;                },'}' => {if (start_index < i)try self.write(format[start_index..i]);                    state = State.CloseBrace;                },else => {},            },            State.OpenBrace =>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.CloseBrace =>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: *OutStream, 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: *OutStream, value:var) !void {const T =@typeOf(value);if (@isInteger(T)) {return self.printInt(T, value);    }elseif (@isFloat(T)) {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 warn =@import("std").debug.warn;const a_number:i32 =1234;const a_string ="foobar";test"printf too many arguments" {    warn("here is a string: '{}' here is a number: {}\n",        a_string, a_number, a_number);}
$ zig test test.zig/home/andy/dev/zig/build/lib/zig/std/fmt/index.zig:95:13:error: Unused arguments            @compileError("Unused arguments");^/home/andy/dev/zig/build/lib/zig/std/io.zig:227:34:note: called from here            return std.fmt.format(self, Error, self.writeFn, format, args);^/home/andy/dev/zig/build/lib/zig/std/debug/index.zig:46:17:note: called from here    stderr.print(fmt, args) catch return;^/home/andy/dev/zig/docgen_tmp/test.zig:7:9:note: called from here    warn("here is a string: '{}' here is a number: {}\n",^

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 is implicitly castable to a[]constu8:

printf.zig

const warn =@import("std").debug.warn;const a_number:i32 =1234;const a_string ="foobar";const fmt ="here is a string: '{}' here is a number: {}\n";pubfnmain()void {    warn(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

TODO: example of inline assembly

TODO: example of module level assembly

TODO: example of using inline assembly return value

TODO: example of using inline assembly assigning values to variables

Atomics

TODO: @fence()

TODO: @atomic rmw

TODO: builtin atomic memory ordering enum

Coroutines

A coroutine is a generalization of a function.

When you call a function, it creates a stack frame, and then the function runs until it reaches a return statement, and then the stack frame is destroyed. At the callsite, the next line of code does not run until the function returns.

A coroutine is like a function, but it can be suspended and resumed any number of times, and then it must be explicitly destroyed. When a coroutine suspends, it returns to the resumer.

Minimal Coroutine Example

Declare a coroutine with theasync keyword. The expression in angle brackets must evaluate to a struct which has these fields:

You may notice that this corresponds to thestd.mem.Allocator interface. This makes it convenient to integrate with existing allocators. Note, however, that the language feature does not depend on the standard library, and any struct which has these fields is allowed.

Omitting the angle bracket expression when defining an async function makes the function generic. Zig will infer the allocator type when the async function is called.

Call a coroutine with theasync keyword. Here, the expression in angle brackets is a pointer to the allocator struct that the coroutine expects.

The result of an async function call is apromise->T type, whereT is the return type of the async function. Once a promise has been created, it must be consumed, either withcancel orawait:

Async functions start executing when created, so in the following example, the entire async function completes before it is canceled:

test.zig

const std =@import("std");const assert = std.debug.assert;var x:i32 =1;test"create a coroutine and cancel it" {const p =tryasync<std.debug.global_allocator> simpleAsyncFn();comptime assert(@typeOf(p) ==promise->void);cancel p;    assert(x ==2);}async<*std.mem.Allocator>fnsimpleAsyncFn()void {    x +=1;}
$ zig test test.zigTest 1/1 create a coroutine and cancel it...OKAll tests passed.

Suspend and Resume

At any point, an async function may suspend itself. This causes control flow to return to the caller or resumer. The following code demonstrates where control flow goes:

test.zig

const std =@import("std");const assert = std.debug.assert;test"coroutine suspend, resume, cancel" {    seq('a');const p =tryasync<std.debug.global_allocator> testAsyncSeq();    seq('c');resume p;    seq('f');cancel p;    seq('g');    assert(std.mem.eql(u8, points,"abcdefg"));}asyncfntestAsyncSeq()void {defer seq('e');    seq('b');suspend;    seq('d');}var points = []u8{0} **"abcdefg".len;var index:usize =0;fnseq(c:u8)void {    points[index] = c;    index +=1;}
$ zig test test.zigTest 1/1 coroutine suspend, resume, cancel...OKAll tests passed.

When an async function suspends itself, it must be sure that it will be resumed or canceled somehow, for example by registering its promise handle in an event loop. Use a suspend capture block to gain access to the promise:

test.zig

const std =@import("std");const assert = std.debug.assert;test"coroutine suspend with block" {const p =tryasync<std.debug.global_allocator> testSuspendBlock();    std.debug.assert(!result);resume a_promise;    std.debug.assert(result);cancel p;}var a_promise:promise =undefined;var result =false;asyncfntestSuspendBlock()void {suspend {comptime assert(@typeOf(@handle()) ==promise->void);        a_promise =@handle();    }    result =true;}
$ zig test test.zigTest 1/1 coroutine suspend with block...OKAll tests passed.

Every suspend point in an async function represents a point at which the coroutine could be destroyed. If that happens,defer expressions that are in scope are run, as well aserrdefer expressions.

Await counts as a suspend point.

Resuming from Suspend Blocks

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

However, the coroutine 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 assert = std.debug.assert;test"resume from suspend" {var buf: [500]u8 =undefined;var a = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;var my_result:i32 =1;const p =tryasync<a> testResumeFromSuspend(&my_result);cancel p;    std.debug.assert(my_result ==2);}asyncfntestResumeFromSuspend(my_result: *i32)void {suspend {resume@handle();    }    my_result.* +=1;suspend;    my_result.* +=1;}
$ zig test test.zigTest 1/1 resume from suspend...OKAll tests passed.

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

Await

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

await is valid only in anasync function, and it takes as an operand a promise handle. If the async function associated with the promise handle has already returned, thenawait destroys the target async function, and gives the return value. Otherwise,await suspends the current async function, registering its promise handle with the target coroutine. It becomes the target coroutine's responsibility to have ensured that it will be resumed or destroyed. When the target coroutine reaches its return statement, it gives the return value to the awaiter, destroys itself, and then resumes the awaiter.

A promise handle must be consumed exactly once after it is created, either bycancel orawait.

await counts as a suspend point, and therefore at everyawait, a coroutine can be potentially destroyed, which would rundefer anderrdefer expressions.

test.zig

const std =@import("std");const assert = std.debug.assert;var a_promise:promise =undefined;var final_result:i32 =0;test"coroutine await" {    seq('a');const p =async<std.debug.global_allocator> amain()catchunreachable;    seq('f');resume a_promise;    seq('i');    assert(final_result ==1234);    assert(std.mem.eql(u8, seq_points,"abcdefghi"));}asyncfnamain()void {    seq('b');const p =async another()catchunreachable;    seq('e');    final_result =await p;    seq('h');}asyncfnanother()i32 {    seq('c');suspend {        seq('d');        a_promise =@handle();    }    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 coroutine await...OKAll 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.

Open Issues

There are a few issues with coroutines that are considered unresolved. Best be aware of them, as the situation is likely to change before 1.0.0:

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.

@ArgType

@ArgType(comptime T:type,comptime n:usize)type

This builtin function takes a function type and returns the type of the parameter at indexn.

T must be a function type.

Note: This function is deprecated. Use@typeInfo instead.

@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 type, abool, or an integer whose bit count meets these requirements:

TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe we can remove this restriction

@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 type, abool, or an integer whose bit count meets these requirements:

TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe we can remove this restriction

@bitCast

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

Converts a value of one type to another type.

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

Asserts that@typeId(DestType) !=@import("builtin").TypeId.Pointer. Use@ptrCast or@intToPtr if you need this.

Can be used for these things for example:

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:

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

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

@alignCast

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

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 assert =@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:

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

@bytesToSlice

@bytesToSlice(comptime Element:type, bytes: []u8) []Element

Converts a slice of bytes or array of bytes into a slice ofElement. The resulting slice has the samepointer properties as the parameter.

Attempting to convert a number of bytes with a length that does not evenly divide into a slice of elements results in safety-protectedUndefined Behavior.

@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) (namespace)

This function parses C code and imports the functions, types, variables, and compatible macro definitions into the result namespace.

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:

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:

@cUndef

@cUndef(comptime name: []u8)

This function can only occur inside@cImport.

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

See also:

@clz

@clz(x: T) U

This function counts the number of leading zeroes inx which is an integer typeT.

The return typeU is an unsigned integer with the minimum number of bits that can represent the valueT.bit_count.

Ifx is zero,@clz returnsT.bit_count.

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.

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

@typeOf(ptr).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.

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

@typeOf(ptr).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 warn =@import("std").debug.warn;const num1 = blk: {var val1:i32 =99;@compileLog("comptime val1 = ", val1);     val1 = val1 +1;break :blk val1;};test"main" {@compileLog("comptime in main");     warn("Runtime in main, num1 = {}.\n", num1);}
$ zig test test.zig| "comptime in main"| "comptime val1 = ", 99/home/andy/dev/zig/docgen_tmp/test.zig:11:5:error: found compile log statement    @compileLog("comptime in main");^/home/andy/dev/zig/docgen_tmp/test.zig:5:5:error: found compile log statement    @compileLog("comptime val1 = ", val1);^

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 warn =@import("std").debug.warn;const num1 = blk: {var val1:i32 =99;    val1 = val1 +1;break :blk val1;};test"main" {    warn("Runtime in main, num1 = {}.\n", num1);}
$ zig test test.zigTest 1/1 main...Runtime in main, num1 = 100.OKAll tests passed.

@ctz

@ctz(x: T) U

This function counts the number of trailing zeroes inx which is an integer typeT.

The return typeU is an unsigned integer with the minimum number of bits that can represent the valueT.bit_count.

Ifx is zero,@ctz returnsT.bit_count.

See also:

@divExact

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

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

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!(@typeId(T) == builtin.TypeId.Intand T.is_signedand numerator ==@minValue(T)and denominator == -1).

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!(@typeId(T) == builtin.TypeId.Intand T.is_signedand numerator ==@minValue(T)and denominator == -1).

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

See also:

@embedFile

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

This function returns a compile time constant 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.

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

See also:

@enumToInt

@enumToInt(enum_value:var)var

Converts an enumeration value into its integer tag type.

If the enum has only 1 possible value, the resut is acomptime_int known atcomptime.

See also:

@errSetCast

@errSetCast(comptime T: DestType, value:var) 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.

@errorName

@errorName(err:error) []u8

This function returns the string representation of an error. If an error declaration is:

error OutOfMem

Then the string representation 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 returns `null`.

@errorToInt

@errorToInt(err:var)@IntType(false,@sizeOf(error) *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:

@export

@export(comptime name: []constu8, target:var, linkage: builtin.GlobalLinkage) []constu8

Creates a symbol in the output object file.

@fence

@fence(order: AtomicOrder)

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

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

See also:

@field

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

Preforms field access equivalent tolhs.field_name, except instead of the field"field_name", it accesses the field named by the string value offield_name.

@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:var) 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:var) 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:

@frameAddress

@frameAddress()

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.

@handle

@handle()

This function returns apromise->T type, whereT is the return type of the async function in scope.

This function is only valid within an async function scope.

@import

@import(comptime path: []u8) (namespace)

This function finds a zig file corresponding topath and imports all the public top level declarations into the resulting namespace.

path can be a relative or absolute 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:

See also:

@inlineCall

@inlineCall(function: X, args: ...) Y

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

test.zig

const assert =@import("std").debug.assert;test"inline function call" {    assert(@inlineCall(add,3,9) ==12);}fnadd(a:i32, b:i32)i32 {return a + b; }
$ zig test test.zigTest 1/1 inline function call...OKAll tests passed.

Unlike a normal function call, however,@inlineCall guarantees that the call will be inlined. If the call cannot be inlined, a compile error is emitted.

See also:

@intCast

@intCast(comptime DestType:type, int:var) 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.

@intToEnum

@intToEnum(comptime DestType:type, int_value:@TagType(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:@IntType(false,@sizeOf(error) *8))error

Converts from the integer representation of an error into the 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:var) 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, int:usize) DestType

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

@IntType

@IntType(comptime is_signed:bool,comptime bit_count:u32)type

This function returns an integer type with the given signness and bit count.

@maxValue

@maxValue(comptime T:type)comptime_int

This function returns the maximum value of the integer typeT.

The result is a compile time constant.

@memberCount

@memberCount(comptime T:type)comptime_int

This function returns the number of members in a struct, enum, or union type.

The result is a compile time constant.

It does not include functions, variables, or constants.

@memberName

@memberName(comptime T:type,comptime index:usize) [N]u8

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

The result is a compile time constant.

It does not include functions, variables, or constants.

@memberType

@memberType(comptime T:type,comptime index:usize)type

Returns the field type of a struct or union.

@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);

@minValue

@minValue(comptime T:type)comptime_int

This function returns the minimum value of the integer type T.

The result is a compile time constant.

@mod

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

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

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.

@newStackCall

@newStackCall(new_stack: []u8, function:var, args: ...)var

This calls a function, in the same way that invoking an expression with parentheses does. However, instead of using the same stack as the caller, the function uses the stack provided in thenew_stack parameter.

test.zig

const std =@import("std");const assert = std.debug.assert;var new_stack_bytes: [1024]u8 =undefined;test"calling a function with a new stack" {const arg =1234;const a =@newStackCall(new_stack_bytes[0..512], targetFunction, arg);const b =@newStackCall(new_stack_bytes[512..], targetFunction, arg);    _ = targetFunction(arg);    assert(arg ==1234);    assert(a < b);}fntargetFunction(x:i32)usize {    assert(x ==1234);var local_variable:i32 =42;const ptr = &local_variable;    ptr.* +=1;    assert(local_variable ==43);return@ptrToInt(ptr);}
$ zig test test.zigTest 1/1 calling a function with a new stack...OKAll tests passed.

@noInlineCall

@noInlineCall(function:var, args: ...)var

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

test.zig

const assert =@import("std").debug.assert;test"noinline function call" {    assert(@noInlineCall(add,3,9) ==12);}fnadd(a:i32, b:i32)i32 {return a + b;}
$ zig test test.zigTest 1/1 noinline function call...OKAll tests passed.

Unlike a normal function call, however,@noInlineCall guarantees that the call will not be inlined. If the call must be inlined, a compile error is emitted.

See also:

@OpaqueType

@OpaqueType()type

Creates a new type with an unknown (but non-zero) size and alignment.

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

test.zig

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

@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, invokes the one provided instd/special/panic.zig.

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

See also:

@popCount

@popCount(integer:var)var

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:var) DestType

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

@ptrToInt

@ptrToInt(value:var)usize

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

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.

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

See also:

@returnAddress

@returnAddress()

This function returns a pointer to the return address of the current stack frame.

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

This function is only valid within function scope.

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

@setRuntimeSafety

@setRuntimeSafety(safety_on:bool)

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

@setEvalBranchQuota

@setEvalBranchQuota(new_quota:usize)

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/home/andy/dev/zig/docgen_tmp/test.zig:4:9:error: evaluation exceeded 1000 backwards branches        while (i < 1001) : (i += 1) {}^

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 foo...OKAll tests passed.

See also:

@setFloatMode

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

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

pubconst FloatMode =enum {    Strict,    Optimized,};

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:

@setGlobalLinkage

@setGlobalLinkage(global_variable_name,comptime linkage: GlobalLinkage)

GlobalLinkage can be found with@import("builtin").GlobalLinkage.

See also:

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

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

@sliceToBytes

@sliceToBytes(value:var) []u8

Converts a slice or array to a slice ofu8. The resulting slice has the samepointer properties as the parameter.

@sqrt

@sqrt(comptime T:type, value: T) T

Performs the square root of a floating point number. Uses a dedicated hardware instruction when available. Currently only supports f32 and f64 at runtime. f128 at runtime is TODO.

This is a low-level intrinsic. Most code can usestd.math.sqrt instead.

@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:var) []constu8

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

@TagType

@TagType(T:type)type

For an enum, returns the integer type that is used to store the enumeration value.

For a union, returns the enum type that is used to store the tag value.

@This

@This()type

Returns the innermost struct 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 assert = std.debug.assert;test"@This()" {var items = []i32{1,2,3,4 };const list = List(i32){ .items = items[0..] };    assert(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 @This()...OKAll tests passed.

When@This() is used at global scope, it returns a reference to the current import. There is a proposal to remove the import type and use an empty struct type instead. See#1047 for details.

@truncate

@truncate(comptime T:type, integer) T

This function truncates bits from an integer type, resulting in a smaller integer type.

The following produces a crash in debug mode and undefined behavior in release mode:

const a:u16 =0xabcd;const b:u8 =u8(a);

However this is well defined and working code:

const a:u16 =0xabcd;const b:u8 =@truncate(u8, a);// b is now 0xcd

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

@typeId

@typeId(comptime T:type)@import("builtin").TypeId

Returns which kind of type something is. Possible values:

pubconst TypeId =enum {    Type,    Void,    Bool,    NoReturn,    Int,    Float,    Pointer,    Array,    Struct,    ComptimeFloat,    ComptimeInt,    Undefined,    Null,    Optional,    ErrorUnion,    Error,    Enum,    Union,    Fn,    Namespace,    Block,    BoundFn,    ArgTuple,    Opaque,};

@typeInfo

@typeInfo(comptime T:type)@import("builtin").TypeInfo

Returns information on the type. Returns a value of the following union:

pubconst TypeInfo =union(TypeId) {    Type:void,    Void:void,    Bool:void,    NoReturn:void,    Int: Int,    Float: Float,    Pointer: Pointer,    Array: Array,    Struct: Struct,    ComptimeFloat:void,    ComptimeInt:void,    Undefined:void,    Null:void,    Optional: Optional,    ErrorUnion: ErrorUnion,    ErrorSet: ErrorSet,    Enum: Enum,    Union: Union,    Fn: Fn,    Namespace:void,    BoundFn: Fn,    ArgTuple:void,    Opaque:void,    Promise: Promise,pubconst Int =struct {        is_signed:bool,        bits:u8,    };pubconst Float =struct {        bits:u8,    };pubconst Pointer =struct {        size: Size,        is_const:bool,        is_volatile:bool,        alignment:u32,        child:type,pubconst Size =enum {            One,            Many,            Slice,        };    };pubconst Array =struct {        len:usize,        child:type,    };pubconst ContainerLayout =enum {        Auto,        Extern,        Packed,    };pubconst StructField =struct {        name: []constu8,        offset: ?usize,        field_type:type,    };pubconst Struct =struct {        layout: ContainerLayout,        fields: []StructField,        defs: []Definition,    };pubconst Optional =struct {        child:type,    };pubconst ErrorUnion =struct {        error_set:type,        payload:type,    };pubconst Error =struct {        name: []constu8,        value:usize,    };pubconst ErrorSet =struct {        errors: []Error,    };pubconst EnumField =struct {        name: []constu8,        value:usize,    };pubconst Enum =struct {        layout: ContainerLayout,        tag_type:type,        fields: []EnumField,        defs: []Definition,    };pubconst UnionField =struct {        name: []constu8,        enum_field: ?EnumField,        field_type:type,    };pubconst Union =struct {        layout: ContainerLayout,        tag_type: ?type,        fields: []UnionField,        defs: []Definition,    };pubconst CallingConvention =enum {        Unspecified,        C,        Cold,        Naked,        Stdcall,        Async,    };pubconst FnArg =struct {        is_generic:bool,        is_noalias:bool,        arg_type: ?type,    };pubconst Fn =struct {        calling_convention: CallingConvention,        is_generic:bool,        is_var_args:bool,        return_type: ?type,        async_allocator_type: ?type,        args: []FnArg,    };pubconst Promise =struct {        child: ?type,    };pubconst Definition =struct {        name: []constu8,        is_pub:bool,        data: Data,pubconst Data =union(enum) {            Type:type,            Var:type,            Fn: FnDef,pubconst FnDef =struct {                fn_type:type,                inline_type: Inline,                calling_convention: CallingConvention,                is_var_args:bool,                is_extern:bool,                is_export:bool,                lib_name: ?[]constu8,                return_type:type,                arg_names: [][]constu8,pubconst Inline =enum {                    Auto,                    Always,                    Never,                };            };        };    };};

@typeName

@typeName(T:type) []u8

This function returns the string representation of a type.

@typeOf

@typeOf(expression)type

This function returns a compile-time constant, which is the type of the expression passed as an argument. The expression is evaluated.

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

ReleaseFast

$ zig build-exe example.zig --release-fast

ReleaseSafe

$ zig build-exe example.zig --release-safe

ReleaseSmall

$ zig build-exe example.zig --release-small

See also:

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 withsetRuntimeSafety. TheReleaseFast build mode disables all safety checks 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 safety check...reached unreachable code/home/andy/dev/zig/docgen_tmp/test.zig:2:5:0x205054 in ??? (test)    unreachable;^/home/andy/dev/zig/build/lib/zig/std/special/test_runner.zig:13:25:0x22294a in ??? (test)        if (test_fn.func()) |_| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:96:22:0x2226fb in ??? (test)            root.main() catch |err| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222675 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224d8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222390 in ??? (test)    @noInlineCall(posixCallMainAndExit);^Tests failed. Use the following command to reproduce the failure:/home/andy/dev/zig/docgen_tmp/test

Reaching Unreachable Code

At compile-time:

test.zig

comptime {    assert(false);}fnassert(ok:bool)void {if (!ok)unreachable;// assertion failure}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:5:14:error: unable to evaluate constant expression    if (!ok) unreachable; // assertion failure^/home/andy/dev/zig/docgen_tmp/test.zig:2:11:note: called from here    assert(false);^/home/andy/dev/zig/docgen_tmp/test.zig:1:10:note: called from herecomptime {^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {    std.debug.assert(false);}
$ zig build-exe test.zig$ ./testreached unreachable code/home/andy/dev/zig/build/lib/zig/std/debug/index.zig:120:13:0x205029 in ??? (test)            unreachable; // assertion failure^/home/andy/dev/zig/docgen_tmp/test.zig:4:21:0x2226cb in ??? (test)    std.debug.assert(false);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

Index out of Bounds

At compile-time:

test.zig

comptime {const array ="hello";const garbage = array[5];}
$ zig test test.zig/home/andy/dev/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$ ./testindex out of bounds/home/andy/dev/zig/docgen_tmp/test.zig:6:13:0x222709 in ??? (test)    return x[5];^/home/andy/dev/zig/docgen_tmp/test.zig:2:16:0x2226d4 in ??? (test)    var x = foo("hello");^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", unsigned);}
$ zig build-exe test.zig$ ./testattempt to cast negative value to unsigned integer/home/andy/dev/zig/docgen_tmp/test.zig:5:20:0x2226fe in ??? (test)    var unsigned = @intCast(u32, value);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

To obtain the maximum value of an unsigned integer, use@maxValue.

Cast Truncates Data

At compile-time:

test.zig

comptime {const spartan_count:u16 =300;const byte =@intCast(u8, spartan_count);}
$ zig test test.zig/home/andy/dev/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.warn("value: {}\n", byte);}
$ zig build-exe test.zig$ ./testinteger cast truncated bits/home/andy/dev/zig/docgen_tmp/test.zig:5:18:0x222707 in ??? (test)    const byte = @intCast(u8, spartan_count);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", byte);}
$ zig build-exe test.zig$ ./testinteger overflow/home/andy/dev/zig/docgen_tmp/test.zig:5:10:0x2226ee in ??? (test)    byte += 1;^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

Standard Library Math Functions

These functions provided by the standard library return possible errors.

Example of catching an overflow for addition:

test.zig

const math =@import("std").math;const warn =@import("std").debug.warn;pubfnmain() !void {var byte:u8 =255;    byte =if (math.add(u8, byte,1)) |result| resultelse |err| {        warn("unable to add one: {}\n",@errorName(err));return err;    };    warn("result: {}\n", byte);}
$ zig build-exe test.zig$ ./testunable to add one: Overflowerror: Overflow/home/andy/dev/zig/build/lib/zig/std/math/index.zig:252:5:0x2229fa in ??? (test)    return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer;^/home/andy/dev/zig/docgen_tmp/test.zig:8:9:0x222858 in ??? (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 warn =@import("std").debug.warn;pubfnmain()void {var byte:u8 =255;var result:u8 =undefined;if (@addWithOverflow(u8, byte,10, &result)) {        warn("overflowed result: {}\n", result);    }else {        warn("result: {}\n", result);    }}
$ zig build-exe test.zig$ ./testoverflowed result: 9

Wrapping Operations

These operations have guaranteed wraparound semantics.

test.zig

const assert =@import("std").debug.assert;test"wraparound addition and subtraction" {const x:i32 =@maxValue(i32);const min_val = x +%1;    assert(min_val ==@minValue(i32));const max_val = min_val -%1;    assert(max_val ==@maxValue(i32));}
$ zig test test.zigTest 1/1 wraparound addition and subtraction...OKAll tests passed.

Exact Left Shift Overflow

At compile-time:

test.zig

comptime {const x =@shlExact(u8(0b01010101),2);}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:2:15:error: operation caused overflow    const x = @shlExact(u8(0b01010101), 2);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var x:u8 =0b01010101;var y =@shlExact(x,2);    std.debug.warn("value: {}\n", y);}
$ zig build-exe test.zig$ ./testleft shift overflowed bits/home/andy/dev/zig/docgen_tmp/test.zig:5:13:0x222705 in ??? (test)    var y = @shlExact(x, 2);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

Exact Right Shift Overflow

At compile-time:

test.zig

comptime {const x =@shrExact(u8(0b10101010),2);}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:2:15:error: exact shift shifted out 1 bits    const x = @shrExact(u8(0b10101010), 2);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var x:u8 =0b10101010;var y =@shrExact(x,2);    std.debug.warn("value: {}\n", y);}
$ zig build-exe test.zig$ ./testright shift overflowed bits/home/andy/dev/zig/docgen_tmp/test.zig:5:13:0x222705 in ??? (test)    var y = @shrExact(x, 2);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", c);}
$ zig build-exe test.zig$ ./testdivision by zero/home/andy/dev/zig/docgen_tmp/test.zig:6:15:0x222712 in ??? (test)    var c = a / b;^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", c);}
$ zig build-exe test.zig$ ./testremainder division by zero or negative value/home/andy/dev/zig/docgen_tmp/test.zig:6:15:0x222712 in ??? (test)    var c = a % b;^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", c);}
$ zig build-exe test.zig$ ./testexact division produced remainder/home/andy/dev/zig/docgen_tmp/test.zig:6:13:0x222733 in ??? (test)    var c = @divExact(a, b);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

Slice Widen Remainder

At compile-time:

test.zig

comptime {var bytes = [5]u8{1,2,3,4,5 };var slice =@bytesToSlice(u32, bytes);}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:3:17:error: unable to convert [5]u8 to []align(1) const u32: size mismatch    var slice = @bytesToSlice(u32, bytes);^/home/andy/dev/zig/docgen_tmp/test.zig:3:31:note: u32 has size 4; remaining bytes: 1    var slice = @bytesToSlice(u32, bytes);^

At runtime:

test.zig

const std =@import("std");pubfnmain()void {var bytes = [5]u8{1,2,3,4,5 };var slice =@bytesToSlice(u32, bytes[0..]);    std.debug.warn("value: {}\n", slice[0]);}
$ zig build-exe test.zig$ ./testslice widening size mismatch/home/andy/dev/zig/docgen_tmp/test.zig:5:17:0x222756 in ??? (test)    var slice = @bytesToSlice(u32, bytes[0..]);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

Attempt to Unwrap Null

At compile-time:

test.zig

comptime {const optional_number: ?i32 =null;const number = optional_number.?;}
$ zig test test.zig/home/andy/dev/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.warn("value: {}\n", number);}
$ zig build-exe test.zig$ ./testattempt to unwrap null/home/andy/dev/zig/docgen_tmp/test.zig:5:33:0x2226ff in ??? (test)    var number = optional_number.?;^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

One way to avoid this crash is to test for null instead of assuming non-null, with theif expression:

test.zig

const warn =@import("std").debug.warn;pubfnmain()void {const optional_number: ?i32 =null;if (optional_number) |number| {        warn("got number: {}\n", number);    }else {        warn("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/home/andy/dev/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.warn("value: {}\n", number);}fngetNumberOrFail() !i32 {returnerror.UnableToReturnNumber;}
$ zig build-exe test.zig$ ./testattempt to unwrap error: UnableToReturnNumber/home/andy/dev/zig/docgen_tmp/test.zig:9:5:0x22276b in ??? (test)    return error.UnableToReturnNumber;^???:?:?:0x21fa3e in ??? (???)/home/andy/dev/zig/docgen_tmp/test.zig:4:38:0x22272c in ??? (test)    const number = getNumberOrFail() catch unreachable;^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

One way to avoid this crash is to test for an error instead of assuming a successful result, with theif expression:

test.zig

const warn =@import("std").debug.warn;pubfnmain()void {const result = getNumberOrFail();if (result) |number| {        warn("got number: {}\n", number);    }else |err| {        warn("got error: {}\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/home/andy/dev/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.warn("value: {}\n", number);}
$ zig build-exe test.zig$ ./testinvalid error code/home/andy/dev/zig/docgen_tmp/test.zig:6:23:0x22271a in ??? (test)    var invalid_err = @intToError(number);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/zig/docgen_tmp/test.zig:8:15:error: enum 'Foo' has no tag matching integer value 3    const b = @intToEnum(Foo, a);^/home/andy/dev/zig/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.warn("value: {}\n",@tagName(b));}
$ zig build-exe test.zig$ ./testinvalid enum value/home/andy/dev/zig/docgen_tmp/test.zig:11:13:0x2226fd in ??? (test)    var b = @intToEnum(Foo, a);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", x);}
$ zig build-exe test.zig$ ./testinvalid error code/home/andy/dev/zig/docgen_tmp/test.zig:15:15:0x222724 in ??? (test)    const x = @errSetCast(Set2, set1);^/home/andy/dev/zig/docgen_tmp/test.zig:12:8:0x2226ce in ??? (test)    foo(Set1.B);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x2226a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

Incorrect Pointer Alignment

At compile-time:

test.zig

comptime {const ptr =@intToPtr(*i32,0x1);const aligned =@alignCast(4, ptr);}
$ zig test test.zig/home/andy/dev/zig/docgen_tmp/test.zig:3:35:error: pointer address 0x1 is not aligned to 4 bytes    const aligned = @alignCast(4, ptr);^

At runtime:

test.zig

pubfnmain() !void {var arrayalign(4) = []u32{0x11111111,0x11111111 };const bytes =@sliceToBytes(array[0..]);if (foo(bytes) !=0x11111111)returnerror.Wrong;}fnfoo(bytes: []u8)u32 {const slice4 = bytes[1..5];const int_slice =@bytesToSlice(u32,@alignCast(4, slice4));return int_slice[0];}
$ zig build-exe test.zig$ ./testincorrect alignment/home/andy/dev/zig/docgen_tmp/test.zig:8:56:0x2229ff in ??? (test)    const int_slice = @bytesToSlice(u32, @alignCast(4, slice4));^/home/andy/dev/zig/docgen_tmp/test.zig:4:12:0x22283e in ??? (test)    if (foo(bytes) != 0x11111111) return error.Wrong;^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:96:22:0x2226db in ??? (test)            root.main() catch |err| {^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x222655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x2224b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x222370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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/home/andy/dev/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.warn("value: {}\n", f.float);}
$ zig build-exe test.zig$ ./testaccess of inactive union field/home/andy/dev/zig/docgen_tmp/test.zig:14:6:0x22a738 in ??? (test)    f.float = 12.34;^/home/andy/dev/zig/docgen_tmp/test.zig:10:8:0x22a6dc in ??? (test)    bar(&f);^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:86:22:0x22a6a9 in ??? (test)            root.main();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20:0x22a655 in ??? (test)    return callMain();^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39:0x22a4b8 in ??? (test)    std.os.posix.exit(callMainWithArgs(argc, argv, envp));^/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5:0x22a370 in ??? (test)    @noInlineCall(posixCallMainAndExit);^

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.warn("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.warn("value: {}\n", f.float);}fnbar(f: *Foo)void {    f.float =12.34;}
$ zig build-exe test.zig$ ./testvalue: 1.23400001e+01

Out of Bounds Float To Integer Cast

TODO

Memory

TODO: explain no default allocator in zig

TODO: show how to use the allocator interface

TODO: mention debug allocator

TODO: importance of checking for allocation failure

TODO: mention overcommit and the OOM Killer

TODO: mention recursion

See also:

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 == builtin.Os.windows)'\\'else'/';

Example of what is imported with@import("builtin"):

pubconst StackTrace =struct {    index:usize,    instruction_addresses: []usize,};pubconst Os =enum {    freestanding,    ananas,    cloudabi,    dragonfly,    freebsd,    fuchsia,    ios,    kfreebsd,    linux,    lv2,    macosx,    netbsd,    openbsd,    solaris,    windows,    haiku,    minix,    rtems,    nacl,    cnk,    aix,    cuda,    nvcl,    amdhsa,    ps4,    elfiamcu,    tvos,    watchos,    mesa3d,    contiki,    amdpal,    zen,};pubconst Arch =enum {    armv8_3a,    armv8_2a,    armv8_1a,    armv8,    armv8r,    armv8m_baseline,    armv8m_mainline,    armv7,    armv7em,    armv7m,    armv7s,    armv7k,    armv7ve,    armv6,    armv6m,    armv6k,    armv6t2,    armv5,    armv5te,    armv4t,    armebv8_3a,    armebv8_2a,    armebv8_1a,    armebv8,    armebv8r,    armebv8m_baseline,    armebv8m_mainline,    armebv7,    armebv7em,    armebv7m,    armebv7s,    armebv7k,    armebv7ve,    armebv6,    armebv6m,    armebv6k,    armebv6t2,    armebv5,    armebv5te,    armebv4t,    aarch64,    aarch64_be,    arc,    avr,    bpfel,    bpfeb,    hexagon,    mips,    mipsel,    mips64,    mips64el,    msp430,    nios2,    powerpc,    powerpc64,    powerpc64le,    r600,    amdgcn,    riscv32,    riscv64,    sparc,    sparcv9,    sparcel,    s390x,    tce,    tcele,    thumb,    thumbeb,i386,    x86_64,    xcore,    nvptx,    nvptx64,    le32,    le64,    amdil,    amdil64,    hsail,    hsail64,    spir,    spir64,    kalimbav3,    kalimbav4,    kalimbav5,    shave,    lanai,    wasm32,    wasm64,    renderscript32,    renderscript64,};pubconst Environ =enum {    unknown,    gnu,    gnuabin32,    gnuabi64,    gnueabi,    gnueabihf,    gnux32,    code16,    eabi,    eabihf,    android,    musl,    musleabi,    musleabihf,    msvc,    itanium,    cygnus,    coreclr,    simulator,};pubconst ObjectFormat =enum {    unknown,    coff,    elf,    macho,    wasm,};pubconst GlobalLinkage =enum {    Internal,    Strong,    Weak,    LinkOnce,};pubconst AtomicOrder =enum {    Unordered,    Monotonic,    Acquire,    Release,    AcqRel,    SeqCst,};pubconst AtomicRmwOp =enum {    Xchg,    Add,    Sub,    And,    Nand,    Or,    Xor,    Max,    Min,};pubconst Mode =enum {    Debug,    ReleaseSafe,    ReleaseFast,    ReleaseSmall,};pubconst TypeId =enum {    Type,    Void,    Bool,    NoReturn,    Int,    Float,    Pointer,    Array,    Struct,    ComptimeFloat,    ComptimeInt,    Undefined,    Null,    Optional,    ErrorUnion,    ErrorSet,    Enum,    Union,    Fn,    Namespace,    BoundFn,    ArgTuple,    Opaque,    Promise,};pubconst TypeInfo =union(TypeId) {    Type:void,    Void:void,    Bool:void,    NoReturn:void,    Int: Int,    Float: Float,    Pointer: Pointer,    Array: Array,    Struct: Struct,    ComptimeFloat:void,    ComptimeInt:void,    Undefined:void,    Null:void,    Optional: Optional,    ErrorUnion: ErrorUnion,    ErrorSet: ErrorSet,    Enum: Enum,    Union: Union,    Fn: Fn,    Namespace:void,    BoundFn: Fn,    ArgTuple:void,    Opaque:void,    Promise: Promise,pubconst Int =struct {        is_signed:bool,        bits:u8,    };pubconst Float =struct {        bits:u8,    };pubconst Pointer =struct {        size: Size,        is_const:bool,        is_volatile:bool,        alignment:u32,        child:type,pubconst Size =enum {            One,            Many,            Slice,        };    };pubconst Array =struct {        len:usize,        child:type,    };pubconst ContainerLayout =enum {        Auto,        Extern,        Packed,    };pubconst StructField =struct {        name: []constu8,        offset: ?usize,        field_type:type,    };pubconst Struct =struct {        layout: ContainerLayout,        fields: []StructField,        defs: []Definition,    };pubconst Optional =struct {        child:type,    };pubconst ErrorUnion =struct {        error_set:type,        payload:type,    };pubconst Error =struct {        name: []constu8,        value:usize,    };pubconst ErrorSet =struct {        errors: []Error,    };pubconst EnumField =struct {        name: []constu8,        value:usize,    };pubconst Enum =struct {        layout: ContainerLayout,        tag_type:type,        fields: []EnumField,        defs: []Definition,    };pubconst UnionField =struct {        name: []constu8,        enum_field: ?EnumField,        field_type:type,    };pubconst Union =struct {        layout: ContainerLayout,        tag_type: ?type,        fields: []UnionField,        defs: []Definition,    };pubconst CallingConvention =enum {        Unspecified,        C,        Cold,        Naked,        Stdcall,        Async,    };pubconst FnArg =struct {        is_generic:bool,        is_noalias:bool,        arg_type: ?type,    };pubconst Fn =struct {        calling_convention: CallingConvention,        is_generic:bool,        is_var_args:bool,        return_type: ?type,        async_allocator_type: ?type,        args: []FnArg,    };pubconst Promise =struct {        child: ?type,    };pubconst Definition =struct {        name: []constu8,        is_pub:bool,        data: Data,pubconst Data =union(enum) {            Type:type,            Var:type,            Fn: FnDef,pubconst FnDef =struct {                fn_type:type,                inline_type: Inline,                calling_convention: CallingConvention,                is_var_args:bool,                is_extern:bool,                is_export:bool,                lib_name: ?[]constu8,                return_type:type,                arg_names: [][]constu8,pubconst Inline =enum {                    Auto,                    Always,                    Never,                };            };        };    };};pubconst FloatMode =enum {    Strict,    Optimized,};pubconst Endian =enum {    Big,    Little,};pubconst endian = Endian.Little;pubconst is_test =false;pubconst os = Os.linux;pubconst arch = Arch.x86_64;pubconst environ = Environ.gnu;pubconst object_format = ObjectFormat.elf;pubconst mode = Mode.Debug;pubconst link_libc =false;pubconst have_error_return_tracing =false;pubconst __zig_test_fn_slice = {};// overwritten later

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

TODO: basic usage

TODO: lazy analysis

TODO: --test-filter

TODO: --test-name-prefix

TODO: testing in releasefast and releasesafe mode. assert still works

Zig Build System

TODO: explain purpose, it's supposed to replace make/cmake

TODO: example of building a zig executable

TODO: example of building a C library

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.

See also:

C String Literals

test.zig

externfnputs([*]constu8)void;pubfnmain()void {    puts(c"this has a null terminator");    puts(c\\and soc\\does thisc\\multiline C string literal    );}
$ zig build-exe test.zig --library c$ ./testthis has a null terminatorand sodoes thismultiline C string literal

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(c"hello\n");}
$ zig build-exe test.zig --library c$ ./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 == builtin.Mode.ReleaseFast);if (something) {@cDefine("_GNU_SOURCE", {});    }@cInclude("stdlib.h");if (something) {@cUndef("_GNU_SOURCE");    }@cInclude("soundio.h");});

See also:

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 shared library:

$ zig build-lib mathtest.zig

To make a static library:

$ zig build-lib mathtest.zig --static

Here is an example with theZig Build System:

test.c

// This header is generated by zig from mathtest.zig#include "mathtest.h"#include <assert.h>int main(int argc, char **argv) {    assert(add(42, 1337) == 1379);    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.addCExecutable("test");    exe.addCompileFlags([][]constu8{"-std=c99"});    exe.addSourceFile("test.c");    exe.linkLibrary(lib);    b.default_step.dependOn(&exe.step);const run_cmd = b.addCommand(".", b.env_map, [][]constu8{exe.getOutputPath()});    run_cmd.step.dependOn(&exe.step);const test_step = b.step("test","Test the program");    test_step.dependOn(&run_cmd.step);}

terminal

$ zig build$ ./test$ echo $?0

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_unsafe;const decoded_size = base64_decoder.calcSize(src);    base64_decoder.decode(dest[0..decoded_size], src);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.addCExecutable("test");    exe.addCompileFlags([][]constu8 {"-std=c99",    });    exe.addSourceFile("test.c");    exe.addObject(obj);    exe.setOutputPath(".");    b.default_step.dependOn(&exe.step);}

terminal

$ zig build$ ./testall your base are belong to us

See also:

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:  armv8_2a  armv8_1a  armv8  armv8r  armv8m_baseline  armv8m_mainline  armv7  armv7em  armv7m  armv7s  armv7k  armv7ve  armv6  armv6m  armv6k  armv6t2  armv5  armv5te  armv4t  armeb  aarch64  aarch64_be  avr  bpfel  bpfeb  hexagon  mips  mipsel  mips64  mips64el  msp430  nios2  powerpc  powerpc64  powerpc64le  r600  amdgcn  riscv32  riscv64  sparc  sparcv9  sparcel  s390x  tce  tcele  thumb  thumbeb  i386  x86_64 (native)  xcore  nvptx  nvptx64  le32  le64  amdil  amdil64  hsail  hsail64  spir  spir64  kalimbav3  kalimbav4  kalimbav5  shave  lanai  wasm32  wasm64  renderscript32  renderscript64Operating Systems:  freestanding  ananas  cloudabi  dragonfly  freebsd  fuchsia  ios  kfreebsd  linux (native)  lv2  macosx  netbsd  openbsd  solaris  windows  haiku  minix  rtems  nacl  cnk  bitrig  aix  cuda  nvcl  amdhsa  ps4  elfiamcu  tvos  watchos  mesa3d  contiki  zenEnvironments:  unknown  gnu (native)  gnuabi64  gnueabi  gnueabihf  gnux32  code16  eabi  eabihf  android  musl  musleabi  musleabihf  msvc  itanium  cygnus  amdopencl  coreclr  opencl

The Zig Standard Library (@import("std")) has architecture, environment, and operating sytsem 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 an all above platforms.

The current list of targets supported by the Zig Standard Library is:

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

Names

Roughly speaking:camelCaseFunctionName,TitleCaseTypeName,snake_case_variable_name. More precisely:

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.

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");var global_var:i32 =undefined;const const_name =42;const primitive_type_alias =f32;const string_alias = []u8;const StructName =struct {};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 {};// The initials BE (Big Endian) are just another word in Zig identifier names.fnreadU32Be()u32 {}

See the Zig Standard Library for more examples.

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 codepoints are never allowed:

The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possbly the last line of the file).

For some discussion on the rationale behind these design decisions, seeissue #663

Grammar

Root = many(TopLevelItem) EOFTopLevelItem = CompTimeExpression(Block) | TopLevelDecl | TestDeclTestDecl = "test" String BlockTopLevelDecl = option("pub") (FnDef | ExternDecl | GlobalVarDecl | UseDecl)GlobalVarDecl = option("export") VariableDeclaration ";"LocalVarDecl = option("comptime") VariableDeclarationVariableDeclaration = ("var" | "const") Symbol option(":" TypeExpr) option("align" "(" Expression ")") option("section" "(" Expression ")") "=" ExpressionContainerMember = (ContainerField | FnDef | GlobalVarDecl)ContainerField = Symbol option(":" PrefixOpExpression) option("=" PrefixOpExpression) ","UseDecl = "use" Expression ";"ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) ";"FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("<" Expression ">"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") (TypeExpr | "var")FnDef = option("inline" | "export") FnProto BlockParamDeclList = "(" list(ParamDecl, ",") ")"ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "var" | "...")Block = option(Symbol ":") "{" many(Statement) "}"Statement = LocalVarDecl ";" | Defer(Block) | Defer(Expression) ";" | BlockExpression(Block) | Expression ";" | ";"TypeExpr = (PrefixOpExpression "!" PrefixOpExpression) | PrefixOpExpressionBlockOrExpression = Block | ExpressionExpression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression | ResumeExpressionAsmExpression = "asm" option("volatile") "(" String option(AsmOutput) ")"AsmOutput = ":" list(AsmOutputItem, ",") option(AsmInput)AsmInput = ":" list(AsmInputItem, ",") option(AsmClobbers)AsmOutputItem = "[" Symbol "]" String "(" (Symbol | "->" TypeExpr) ")"AsmInputItem = "[" Symbol "]" String "(" Expression ")"AsmClobbers= ":" list(String, ",")UnwrapExpression = BoolOrExpression (UnwrapOptional | UnwrapError) | BoolOrExpressionUnwrapOptional = "orelse" ExpressionUnwrapError = "catch" option("|" Symbol "|") ExpressionAssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | UnwrapExpressionAssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "*%=" | "+%=" | "-%="BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body) | SuspendExpression(body)CompTimeExpression(body) = "comptime" bodySwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbol "|") Expression ","SwitchItem = Expression | (Expression "..." Expression)ForExpression(body) = option(Symbol ":") option("inline") "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body option("else" BlockExpression(body))BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpressionReturnExpression = "return" option(Expression)TryExpression = "try" ExpressionAwaitExpression = "await" ExpressionBreakExpression = "break" option(":" Symbol) option(Expression)CancelExpression = "cancel" Expression;ResumeExpression = "resume" Expression;Defer(body) = ("defer" | "deferror") bodyIfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))SuspendExpression(body) = "suspend" option( body )IfErrorExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))WhileExpression(body) = option(Symbol ":") option("inline") "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body))BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpressionComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpressionComparisonOperator = "==" | "!=" | "<" | ">" | "<=" | ">="BinaryOrExpression = BinaryXorExpression "|" BinaryOrExpression | BinaryXorExpressionBinaryXorExpression = BinaryAndExpression "^" BinaryXorExpression | BinaryAndExpressionBinaryAndExpression = BitShiftExpression "&" BinaryAndExpression | BitShiftExpressionBitShiftExpression = AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpressionBitShiftOperator = "<<" | ">>"AdditionExpression = MultiplyExpression AdditionOperator AdditionExpression | MultiplyExpressionAdditionOperator = "+" | "-" | "++" | "+%" | "-%"MultiplyExpression = CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpressionCurlySuffixExpression = TypeExpr option(ContainerInitExpression)MultiplyOperator = "||" | "*" | "/" | "%" | "**" | "*%"PrefixOpExpression = PrefixOp TypeExpr | SuffixOpExpressionSuffixOpExpression = ("async" option("<" SuffixOpExpression ">") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression | ".*" | ".?")FieldAccessExpression = "." SymbolFnCallExpression = "(" list(Expression, ",") ")"ArrayAccessExpression = "[" Expression "]"SliceExpression = "[" Expression ".." option(Expression) "]"ContainerInitExpression = "{" ContainerInitBody "}"ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")StructLiteralField = "." Symbol "=" ExpressionPrefixOp = "!" | "-" | "~" | (("*" | "[*]") option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "-%" | "try" | "await"PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ContainerDecl | ("continue" option(":" Symbol)) | ErrorSetDecl | PromiseTypePromiseType = "promise" option("->" TypeExpr)ArrayType : "[" option(Expression) "]" option("align" "(" Expression option(":" Integer ":" Integer) ")")) option("const") option("volatile") TypeExprGroupedExpression = "(" Expression ")"KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "unreachable" | "suspend"ErrorSetDecl = "error" "{" list(Symbol, ",") "}"ContainerDecl = option("extern" | "packed")  ("struct" option(GroupedExpression) | "union" option("enum" option(GroupedExpression) | GroupedExpression) | ("enum" option(GroupedExpression)))  "{" many(ContainerMember) "}"

Zen


[8]ページ先頭

©2009-2026 Movatter.jp