{#header_open|Introduction#}

Zig is an open-source programming language designed for robustness, optimality, and clarity.

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.

If you search for something specific in this documentation and do not find it, please file an issue or say something on IRC.

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.

{#header_close#} {#header_open|Hello World#} {#code_begin|exe|hello#} const std = @import("std"); pub fn main() !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"); } {#code_end#}

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 a warning message that you want to emit. For that you can use a simpler API:

{#code_begin|exe|hello#} const warn = @import("std").debug.warn; pub fn main() void { warn("Hello, world!\n"); } {#code_end#}

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

{#see_also|Values|@import|Errors|Root Source File#} {#header_close#} {#header_open|Values#} {#code_begin|exe|values#} const std = @import("std"); const warn = std.debug.warn; const os = std.os; const assert = std.debug.assert; pub fn main() void { // integers const one_plus_one: i32 = 1 + 1; warn("1 + 1 = {}\n", one_plus_one); // floats const seven_div_three: f32 = 7.0 / 3.0; warn("7.0 / 3.0 = {}\n", seven_div_three); // boolean warn("{}\n{}\n{}\n", true and false, true or false, !true); // nullable var nullable_value: ?[]const u8 = null; assert(nullable_value == null); warn("\nnullable 1\ntype: {}\nvalue: {}\n", @typeName(@typeOf(nullable_value)), nullable_value); nullable_value = "hi"; assert(nullable_value != null); warn("\nnullable 2\ntype: {}\nvalue: {}\n", @typeName(@typeOf(nullable_value)), nullable_value); // error union var 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); } {#code_end#} {#header_open|Primitive Types#}
Name C Equivalent Description
i2 (none) signed 2-bit integer
u2 (none) unsigned 2-bit integer
i3 (none) signed 3-bit integer
u3 (none) unsigned 3-bit integer
i4 (none) signed 4-bit integer
u4 (none) unsigned 4-bit integer
i5 (none) signed 5-bit integer
u5 (none) unsigned 5-bit integer
i6 (none) signed 6-bit integer
u6 (none) unsigned 6-bit integer
i7 (none) signed 7-bit integer
u7 (none) unsigned 7-bit integer
i8 int8_t signed 8-bit integer
u8 uint8_t unsigned 8-bit integer
i16 int16_t signed 16-bit integer
u16 uint16_t unsigned 16-bit integer
i32 int32_t signed 32-bit integer
u32 uint32_t unsigned 32-bit integer
i64 int64_t signed 64-bit integer
u64 uint64_t unsigned 64-bit integer
i128 __int128 signed 128-bit integer
u128 unsigned __int128 unsigned 128-bit integer
isize intptr_t signed pointer sized integer
usize uintptr_t unsigned pointer sized integer
c_short short for ABI compatibility with C
c_ushort unsigned short for ABI compatibility with C
c_int int for ABI compatibility with C
c_uint unsigned int for ABI compatibility with C
c_long long for ABI compatibility with C
c_ulong unsigned long for ABI compatibility with C
c_longlong long long for ABI compatibility with C
c_ulonglong unsigned long long for ABI compatibility with C
c_longdouble long double for ABI compatibility with C
c_void void for ABI compatibility with C
f32 float 32-bit floating point (23-bit mantissa)
f64 double 64-bit floating point (52-bit mantissa)
f128 (none) 128-bit floating point (112-bit mantissa)
bool bool true or false
void (none) 0 bit type
noreturn (none) the type of break, continue, return, unreachable, and while (true) {}
type (none) the type of types
error (none) an error code
{#see_also|Integers|Floats|void|Errors#} {#header_close#} {#header_open|Primitive Values#}
Name Description
true and false bool values
null used to set a nullable type to null
undefined used to leave a value unspecified
this refers to the thing in immediate scope
{#see_also|Nullables|this#} {#header_close#} {#header_open|String Literals#} {#code_begin|test#} 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) == &const u8); assert(null_terminated_bytes[5] == 0); } {#code_end#} {#see_also|Arrays|Zig Test#} {#header_open|Escape Sequences#}
Escape Sequence Name
\n Newline
\r Carriage Return
\t Tab
\\ Backslash
\' Single Quote
\" Double Quote
\xNN hexadecimal 8-bit character code (2 digits)
\uNNNN hexadecimal 16-bit Unicode character code UTF-8 encoded (4 digits)
\UNNNNNN hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)

Note that the maximum valid Unicode point is 0x10ffff.

{#header_close#} {#header_open|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.

{#code_begin|syntax#} const hello_world_in_c = \\#include \\ \\int main(int argc, char **argv) { \\ printf("hello world\n"); \\ return 0; \\} ; {#code_end#}

For a multiline C string literal, prepend c to each \\:

{#code_begin|syntax#} const c_string_literal = c\\#include c\\ c\\int main(int argc, char **argv) { c\\ printf("hello world\n"); c\\ return 0; c\\} ; {#code_end#}

In this example the variable c_string_literal has type &const char and has a terminating null byte.

{#see_also|@embedFile#} {#header_close#} {#header_close#} {#header_open|Assignment#}

Use const to assign a value to an identifier:

{#code_begin|test_err|cannot assign to constant#} const x = 1234; fn foo() 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(); } {#code_end#}

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

{#code_begin|test#} const assert = @import("std").debug.assert; test "var" { var y: i32 = 5678; y += 1; assert(y == 5679); } {#code_end#}

Variables must be initialized:

{#code_begin|test_err#} test "initialization" { var x: i32; x = 1; } {#code_end#}

Use undefined to leave variables uninitialized:

{#code_begin|test#} const assert = @import("std").debug.assert; test "init with undefined" { var x: i32 = undefined; x = 1; assert(x == 1); } {#code_end#} {#header_close#} {#header_close#} {#header_open|Integers#} {#header_open|Integer Literals#} {#code_begin|syntax#} const decimal_int = 98222; const hex_int = 0xff; const another_hex_int = 0xFF; const octal_int = 0o755; const binary_int = 0b11110000; {#code_end#} {#header_close#} {#header_open|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.

{#code_begin|syntax#} fn divide(a: i32, b: i32) i32 { return a / b; } {#code_end#}

In this function, values a and b are known only at runtime, and thus this division operation is vulnerable to both integer overflow and division by zero.

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

{#see_also|Integer Overflow|Division by Zero|Wrapping Operations#} {#header_close#} {#header_close#} {#header_open|Floats#} {#header_open|Float Literals#} {#code_begin|syntax#} 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; {#code_end#} {#header_close#} {#header_open|Floating Point Operations#}

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

{#code_begin|obj|foo#} {#code_release_fast#} const builtin = @import("builtin"); const big = f64(1 << 40); export fn foo_strict(x: f64) f64 { @setFloatMode(this, builtin.FloatMode.Strict); return x + big - big; } export fn foo_optimized(x: f64) f64 { return x + big - big; } {#code_end#}

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.

{#code_begin|exe|float_mode#} {#code_link_object|foo#} const warn = @import("std").debug.warn; extern fn foo_strict(x: f64) f64; extern fn foo_optimized(x: f64) f64; pub fn main() void { const x = 0.001; warn("optimized = {}\n", foo_optimized(x)); warn("strict = {}\n", foo_strict(x)); } {#code_end#} {#see_also|@setFloatMode|Division by Zero#} {#header_close#} {#header_close#} {#header_open|Operators#} {#header_open|Table of Operators#}
Syntax Relevant Types Description Example
a + b
a += b
  • {#link|Integers#}
  • {#link|Floats#}
Addition.
  • Can cause {#link|overflow|Default Operations#} for integers.
2 + 5 == 7
a +% b
a +%= b
  • {#link|Integers#}
Wrapping Addition.
  • Guaranteed to have twos-complement wrapping behavior.
u32(@maxValue(u32)) +% 1 == 0
a - b
a -= b
  • {#link|Integers#}
  • {#link|Floats#}
Subtraction.
  • Can cause {#link|overflow|Default Operations#} for integers.
2 - 5 == -3
a -% b
a -%= b
  • {#link|Integers#}
Wrapping Subtraction.
  • Guaranteed to have twos-complement wrapping behavior.
u32(0) -% 1 == @maxValue(u32)
-a
  • {#link|Integers#}
  • {#link|Floats#}
Negation.
  • Can cause {#link|overflow|Default Operations#} for integers.
-1 == 0 - 1
-%a
  • {#link|Integers#}
Wrapping Negation.
  • Guaranteed to have twos-complement wrapping behavior.
-%i32(@minValue(i32)) == @minValue(i32)
a * b
a *= b
  • {#link|Integers#}
  • {#link|Floats#}
Multiplication.
  • Can cause {#link|overflow|Default Operations#} for integers.
2 * 5 == 10
a *% b
a *%= b
  • {#link|Integers#}
Wrapping Multiplication.
  • Guaranteed to have twos-complement wrapping behavior.
u8(200) *% 2 == 144
a / b
a /= b
  • {#link|Integers#}
  • {#link|Floats#}
Divison.
  • Can cause {#link|overflow|Default Operations#} for integers.
  • Can cause {#link|Division by Zero#} for integers.
  • Can cause {#link|Division by Zero#} for floats in {#link|FloatMode.Optimized Mode|Floating Point Operations#}.
  • For non-compile-time-known signed integers, must use {#link|@divTrunc#}, {#link|@divFloor#}, or {#link|@divExact#} instead of /.
10 / 5 == 2
a % b
a %= b
  • {#link|Integers#}
  • {#link|Floats#}
Remainder Division.
  • Can cause {#link|Division by Zero#} for integers.
  • Can cause {#link|Division by Zero#} for floats in {#link|FloatMode.Optimized Mode|Floating Point Operations#}.
  • For non-compile-time-known signed integers, must use {#link|@rem#} or {#link|@mod#} instead of %.
10 % 3 == 1
a << b
a <<= b
  • {#link|Integers#}
Bit Shift Left.
  • See also {#link|@shlExact#}.
  • See also {#link|@shlWithOverflow#}.
1 << 8 == 256
a >> b
a >>= b
  • {#link|Integers#}
Bit Shift Right.
  • See also {#link|@shrExact#}.
10 >> 1 == 5
a & b
a &= b
  • {#link|Integers#}
Bitwise AND.
0b011 & 0b101 == 0b001
a | b
a |= b
  • {#link|Integers#}
Bitwise OR.
0b010 | 0b100 == 0b110
a ^ b
a ^= b
  • {#link|Integers#}
Bitwise XOR.
0b011 ^ 0b101 == 0b110
~a
  • {#link|Integers#}
Bitwise NOT.
~u8(0b0101111) == 0b1010000
a ?? b
  • {#link|Nullables#}
If a is null, returns b ("default value"), otherwise returns the unwrapped value of a. Note that b may be a value of type {#link|noreturn#}.
const value: ?u32 = null;
const unwrapped = value ?? 1234;
unwrapped == 1234
??a
  • {#link|Nullables#}
Equivalent to:
a ?? unreachable
const value: ?u32 = 5678;
??value == 5678
a catch b
a catch |err| b
  • {#link|Error Unions|Errors#}
If a is an error, returns b ("default value"), otherwise returns the unwrapped value of a. Note that b may be a value of type {#link|noreturn#}. err is the error and is in scope of the expression b.
const value: error!u32 = error.Broken;
const unwrapped = value catch 1234;
unwrapped == 1234
a and b
  • {#link|bool|Primitive Types#}
If a is false, returns false without evaluating b. Otherwise, retuns b.
false and true == false
a or b
  • {#link|bool|Primitive Types#}
If a is true, returns true without evaluating b. Otherwise, retuns b.
false or true == true
!a
  • {#link|bool|Primitive Types#}
Boolean NOT.
!false == true
a == b
  • {#link|Integers#}
  • {#link|Floats#}
  • {#link|bool|Primitive Types#}
  • {#link|type|Primitive Types#}
Returns true if a and b are equal, otherwise returns false.
(1 == 1) == true
a == null
  • {#link|Nullables#}
Returns true if a is null, otherwise returns false.
const value: ?u32 = null;
value == null
a != b
  • {#link|Integers#}
  • {#link|Floats#}
  • {#link|bool|Primitive Types#}
  • {#link|type|Primitive Types#}
Returns false if a and b are equal, otherwise returns true.
(1 != 1) == false
a > b
  • {#link|Integers#}
  • {#link|Floats#}
Returns true if a is greater than b, otherwise returns false.
(2 > 1) == true
a >= b
  • {#link|Integers#}
  • {#link|Floats#}
Returns true if a is greater than or equal to b, otherwise returns false.
(2 >= 1) == true
a < b
  • {#link|Integers#}
  • {#link|Floats#}
Returns true if a is less than b, otherwise returns false.
(1 < 2) == true
a <= b
  • {#link|Integers#}
  • {#link|Floats#}
Returns true if a is less than or equal to b, otherwise returns false.
(1 <= 2) == true
a ++ b
  • {#link|Arrays#}
Array concatenation.
  • Only available when a and b are {#link|compile-time known|comptime#}.
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
  • {#link|Arrays#}
Array multiplication.
  • Only available when a and b are {#link|compile-time known|comptime#}.
const mem = @import("std").mem;
const pattern = "ab" ** 3;
mem.eql(u8, pattern, "ababab")
*a
  • {#link|Pointers#}
Pointer dereference.
const x: u32 = 1234;
const ptr = &x;
*x == 1234
&a
All types Address of.
const x: u32 = 1234;
const ptr = &x;
*x == 1234
{#header_close#} {#header_open|Precedence#}
x() x[] x.y
a!b
!x -x -%x ~x *x &x ?x ??x
x{}
! * / % ** *%
+ - ++ +% -%
<< >>
&
^
|
== != < > <= >=
and
or
?? catch
= *= /= %= += -= <<= >>= &= ^= |=
{#header_close#} {#header_close#} {#header_open|Arrays#} {#code_begin|test|arrays#} const assert = @import("std").debug.assert; const mem = @import("std").mem; // array literal const message = []u8{'h', 'e', 'l', 'l', 'o'}; // get the size of an array comptime { assert(message.len == 5); } // a string literal is an array literal const 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 array var some_integers: [100]i32 = undefined; test "modify an array" { for (some_integers) |*item, i| { *item = i32(i); } assert(some_integers[10] == 10); assert(some_integers[99] == 99); } // array concatenation works if the values are known // at compile time const 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 arrays const hello = "hello"; const world = "world"; const hello_world = hello ++ " " ++ world; comptime { assert(mem.eql(u8, hello_world, "hello world")); } // ** does repeating patterns const pattern = "ab" ** 3; comptime { assert(mem.eql(u8, pattern, "ababab")); } // initialize an array to zero const all_zero = []u16{0} ** 10; comptime { assert(all_zero.len == 10); assert(all_zero[5] == 0); } // use compile-time code to initialize an array var fancy_array = init: { var initial_value: [10]Point = undefined; for (initial_value) |*pt, i| { *pt = Point { .x = i32(i), .y = 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 array var more_points = []Point{makePoint(3)} ** 10; fn makePoint(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); } {#code_end#} {#see_also|for|Slices#} {#header_close#} {#header_open|Pointers#} {#code_begin|test#} 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) == &const i32); // 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" { // Pointers do not support pointer arithmetic. If you // need such a thing, use array index syntax: var array = []u8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const ptr = &array[1]; assert(array[2] == 3); ptr[1] += 1; assert(array[2] == 4); } test "pointer slicing" { // In Zig, we prefer using slices over null-terminated pointers. // You can turn a pointer into a slice using slice syntax: var array = []u8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const ptr = &array[1]; const slice = ptr[1..3]; assert(slice.ptr == &ptr[1]); 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(&volatile u8, 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) == &volatile u8); } test "nullable pointers" { // Pointers cannot be null. If you want a null pointer, use the nullable // prefix `?` to make the pointer type nullable. var ptr: ?&i32 = null; var x: i32 = 1; ptr = &x; assert(*??ptr == 1); // Nullable 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 bytes align(@alignOf(u32)) = []u8{0x12, 0x12, 0x12, 0x12}; const u32_ptr = @ptrCast(&const u32, &bytes[0]); 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 = ([]const 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); } {#code_end#} {#header_open|Alignment#}

Each type has an alignment - 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 {#link|@alignOf#} to find out this value for any type.

Alignment depends on the CPU architecture, but is always a power of two, and less than 1 << 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:

{#code_begin|test#} 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); } } {#code_end#}

In the same way that a &i32 can be implicitly cast to a &const i32, 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:

{#code_begin|test#} const assert = @import("std").debug.assert; var foo: u8 align(4) = 100; test "global variable alignment" { assert(@typeOf(&foo).alignment == 4); assert(@typeOf(&foo) == &align(4) u8); const slice = (&foo)[0..1]; assert(@typeOf(slice) == []align(4) u8); } fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; } fn noop1() align(1) void {} fn noop4() 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(); } {#code_end#}

If you have a pointer or a slice that has a small alignment, but you know that it actually has a bigger alignment, use {#link|@alignCast#} to change the pointer into a more aligned pointer. This is a no-op at runtime, but inserts a {#link|safety check|Incorrect Pointer Alignment#}:

{#code_begin|test_safety|incorrect alignment#} const assert = @import("std").debug.assert; test "pointer alignment safety" { var array align(4) = []u32{0x11111111, 0x11111111}; const bytes = ([]u8)(array[0..]); assert(foo(bytes) == 0x11111111); } fn foo(bytes: []u8) u32 { const slice4 = bytes[1..5]; const int_slice = ([]u32)(@alignCast(4, slice4)); return int_slice[0]; } {#code_end#} {#header_close#} {#header_open|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 of u8. Pointers to u8 can alias any memory.

As an example, this code produces undefined behavior:

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

Instead, use {#link|@bitCast#}:

@bitCast(u32, f32(12.34))

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

{#see_also|Slices|Memory#} {#header_close#} {#header_close#} {#header_open|Slices#} {#code_begin|test_safety|index out of bounds#} 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.ptr == &array[0]); assert(slice.len == array.len); // Slices have array bounds checking. If you try to access something out // of bounds, you'll get a safety check failure: slice[10] += 1; } {#code_end#}

This is one reason we prefer slices to pointers.

{#code_begin|test|slices#} 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 u8 const hello: []const u8 = "hello"; const world: []const u8 = "世界"; 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[0]; // 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 array align(@alignOf(u32)) = []u8{0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13}; const slice = ([]const u32)(array[0..]); assert(slice.len == 2); assert(slice[0] == 0x12121212); assert(slice[1] == 0x13131313); } {#code_end#} {#see_also|Pointers|for|Arrays#} {#header_close#} {#header_open|struct#} {#code_begin|test|structs#} // 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 = packed struct { 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, pub fn init(x: f32, y: f32, z: f32) Vec3 { return Vec3 { .x = x, .y = y, .z = z, }; } pub fn dot(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 { pub const PI = 3.14; }; test "struct namespaced variable" { assert(Empty.PI == 3.14); assert(@sizeOf(Empty) == 0); // you can still instantiate an empty struct const 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: fn setYBasedOnX(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: fn LinkedList(comptime T: type) type { return struct { pub const 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); } {#code_end#} {#see_also|comptime|@fieldParentPtr#} {#header_close#} {#header_open|enum#} {#code_begin|test|enums#} 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(u2(Value.Zero) == 0); assert(u2(Value.One) == 1); assert(u2(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(u32(Value2.Hundred) == 100); assert(u32(Value2.Thousand) == 1000); assert(u32(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, pub fn isClubs(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")); } {#code_end#} {#header_open|extern enum#}

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

{#code_begin|obj_err|parameter of type 'Foo' not allowed in function with calling convention 'ccc'#} const Foo = enum { A, B, C }; export fn entry(foo: Foo) void { } {#code_end#}

For a C-ABI-compatible enum, use extern enum:

{#code_begin|obj#} const Foo = extern enum { A, B, C }; export fn entry(foo: Foo) void { } {#code_end#} {#header_close#} {#header_open|packed enum#}

By default, the size of enums is not guaranteed.

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

{#code_begin|test#} const std = @import("std"); test "packed enum" { const Number = packed enum(u8) { One, Two, Three, }; std.debug.assert(@sizeOf(Number) == @sizeOf(u8)); } {#code_end#} {#header_close#} {#see_also|@memberName|@memberCount|@tagName|@sizeOf#} {#header_close#} {#header_open|union#} {#code_begin|test|union#} 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: []const u8, 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, fn truthy(self: &const Variant) bool { return switch (*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")); } {#code_end#}

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.

{#header_close#} {#header_open|switch#} {#code_begin|test|switch#} 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 :blk 6; }, // 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("windows not supported"); }, else => {}, } } {#code_end#} {#see_also|comptime|enum|@compileError|Compile Variables#} {#header_close#} {#header_open|while#} {#code_begin|test|while#} const assert = @import("std").debug.assert; test "while basic" { // A while loop is used to repeatedly execute an expression until // some condition is no longer true. var i: usize = 0; while (i < 10) { i += 1; } assert(i == 10); } test "while break" { // You can use break to exit a while loop early. var i: usize = 0; while (true) { if (i == 10) break; i += 1; } assert(i == 10); } test "while continue" { // You can use continue to jump back to the beginning of the loop. var i: usize = 0; while (true) { i += 1; if (i < 10) continue; break; } assert(i == 10); } test "while loop continuation expression" { // You can give an expression to the while loop to execute when // the loop is continued. This is respected by the continue control flow. var i: usize = 0; while (i < 10) : (i += 1) {} assert(i == 10); } test "while loop continuation expression, more complicated" { // More complex blocks can be used as an expression in the loop continue // expression. var i1: usize = 1; var j1: usize = 1; while (i1 * j1 < 2000) : ({ i1 *= 2; j1 *= 3; }) { const my_ij1 = i1 * j1; assert(my_ij1 < 2000); } } test "while else" { assert(rangeHasNumber(0, 10, 5)); assert(!rangeHasNumber(0, 10, 15)); } fn rangeHasNumber(begin: usize, end: usize, number: usize) bool { var i = begin; // While loops are expressions. The result of the expression is the // result of the else clause of a while loop, which is executed when // the condition of the while loop is tested as false. return while (i < end) : (i += 1) { if (i == number) { // break expressions, like return expressions, accept a value // parameter. This is the result of the while expression. // When you break from a while loop, the else branch is not // evaluated. break true; } } else false; } test "while null capture" { // Just like if expressions, while loops can take a nullable as the // condition and capture the payload. When null is encountered the loop // exits. var sum1: u32 = 0; numbers_left = 3; while (eventuallyNullSequence()) |value| { sum1 += value; } assert(sum1 == 3); // The else branch is allowed on nullable iteration. In this case, it will // be executed on the first null value encountered. var sum2: u32 = 0; numbers_left = 3; while (eventuallyNullSequence()) |value| { sum2 += value; } else { assert(sum1 == 3); } // Just like if expressions, while loops can also 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. var sum3: u32 = 0; numbers_left = 3; while (eventuallyErrorSequence()) |value| { sum3 += value; } else |err| { assert(err == error.ReachedZero); } } var numbers_left: u32 = undefined; fn eventuallyNullSequence() ?u32 { return if (numbers_left == 0) null else blk: { numbers_left -= 1; break :blk numbers_left; }; } fn eventuallyErrorSequence() error!u32 { return if (numbers_left == 0) error.ReachedZero else blk: { numbers_left -= 1; break :blk numbers_left; }; } test "inline while loop" { // 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. comptime var i = 0; var sum: usize = 0; inline while (i < 3) : (i += 1) { const T = switch (i) { 0 => f32, 1 => i8, 2 => bool, else => unreachable, }; sum += typeNameLength(T); } assert(sum == 9); } fn typeNameLength(comptime T: type) usize { return @typeName(T).len; } {#code_end#} {#see_also|if|Nullables|Errors|comptime|unreachable#} {#header_close#} {#header_open|for#} {#code_begin|test|for#} 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 += 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) { break 9; } else { sum += ??value; } } else blk: { assert(sum == 7); break :blk sum; }; } test "inline for loop" { const nums = []i32{2, 4, 6}; // 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. var sum: usize = 0; inline for (nums) |i| { const T = switch (i) { 2 => f32, 4 => i8, 6 => bool, else => unreachable, }; sum += typeNameLength(T); } assert(sum == 9); } fn typeNameLength(comptime T: type) usize { return @typeName(T).len; } {#code_end#} {#see_also|while|comptime|Arrays|Slices#} {#header_close#} {#header_open|if#} {#code_begin|test|if#} // If expressions have three uses, corresponding to the three types: // * bool // * ?T // * error!T const 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); } else if (a == 9) { unreachable; } else { unreachable; } // If expressions are used instead of a ternary expression. const result = if (a != b) 47 else 3089; assert(result == 47); } test "if nullable" { // 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; } } {#code_end#} {#see_also|Nullables|Errors#} {#header_close#} {#header_open|defer#} {#code_begin|test|defer#} 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. fn deferExample() 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. fn deferUnwindExample() 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. fn deferErrorExample(is_error: bool) !void { warn("\nstart of function\n"); // This will always be executed on exit defer { warn("end of function\n"); } errdefer { warn("encountered an error!\n"); } if (is_error) { return error.DeferError; } } test "errdefer unwinding" { _ = deferErrorExample(false); _ = deferErrorExample(true); } {#code_end#} {#see_also|Errors#} {#header_close#} {#header_open|unreachable#}

In Debug and ReleaseSafe mode, and when using zig test, unreachable emits a call to panic with the message reached unreachable code.

In ReleaseFast mode, the optimizer uses the assumption that unreachable code will never be hit to perform optimizations. However, zig test even in ReleaseFast mode still emits unreachable as calls to panic.

{#header_open|Basics#} {#code_begin|test#} // 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; } } {#code_end#}

In fact, this is how assert is implemented:

{#code_begin|test_err#} fn assert(ok: bool) void { if (!ok) unreachable; // assertion failure } // This test will fail because we hit unreachable. test "this will fail" { assert(false); } {#code_end#} {#header_close#} {#header_open|At Compile-Time#} {#code_begin|test_err|unreachable code#} 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); } } {#code_end#} {#see_also|Zig Test|Build Mode|comptime#} {#header_close#} {#header_close#} {#header_open|noreturn#}

noreturn is the type of:

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

When resolving types together, such as if clauses or switch prongs, the noreturn type is compatible with every other type. Consider:

{#code_begin|test#} fn foo(condition: bool, b: u32) void { const a = if (condition) b else return; @panic("do something with a"); } test "noreturn" { foo(false, 1); } {#code_end#}

Another use case for noreturn is the exit function:

{#code_begin|test#} {#target_windows#} pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: c_uint) noreturn; test "foo" { const value = bar() catch ExitProcess(1); assert(value == 1234); } fn bar() error!u32 { return 1234; } const assert = @import("std").debug.assert; {#code_end#} {#header_close#} {#header_open|Functions#} {#code_begin|test|functions#} const assert = @import("std").debug.assert; // Functions are declared like this fn add(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. export fn sub(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" stdcallcc fn ExitProcess(exit_code: u32) noreturn; extern "c" fn atan2(a: f64, b: f64) f64; // The @setCold builtin tells the optimizer that a function is rarely called. fn abort() noreturn { @setCold(true); while (true) {} } // nakedcc makes a function not have any function prologue or epilogue. // This can be useful when integrating with assembly. nakedcc fn _start() noreturn { abort(); } // The pub specifier allows the function to be visible when importing. // Another file can use @import and call sub2 pub fn sub2(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; fn do_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); } {#code_end#}

Function values are like pointers:

{#code_begin|obj#} const assert = @import("std").debug.assert; comptime { assert(@typeOf(foo) == fn()void); assert(@sizeOf(fn()void) == @sizeOf(?fn()void)); } fn foo() void { } {#code_end#} {#header_open|Pass-by-value Parameters#}

In Zig, structs, unions, and enums with payloads cannot be passed by value to a function.

{#code_begin|test_err|not copyable; cannot pass by value#} const Foo = struct { x: i32, }; fn bar(foo: Foo) void {} test "pass aggregate type by value to function" { bar(Foo {.x = 12,}); } {#code_end#}

Instead, one must use &const. Zig allows implicitly casting something to a const pointer to it:

{#code_begin|test#} const Foo = struct { x: i32, }; fn bar(foo: &const Foo) void {} test "implicitly cast to const pointer" { bar(Foo {.x = 12,}); } {#code_end#}

However, the C ABI does allow passing structs and unions by value. So functions which use the C calling convention may pass structs and unions by value.

{#header_close#} {#header_open|Function Reflection#} {#code_begin|test#} const assert = @import("std").debug.assert; test "fn reflection" { assert(@typeOf(assert).ReturnType == void); assert(@typeOf(assert).is_var_args == false); } {#code_end#} {#header_close#} {#header_close#} {#header_open|Errors#} {#header_open|Error Set Type#}

An error set is like an {#link|enum#}. 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 a u16. See #768.

You can implicitly cast an error from a subset to its superset:

{#code_begin|test#} 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); } fn foo(err: AllocationError) FileOpenError { return err; } {#code_end#}

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

{#code_begin|test_err|not a member of destination error set#} const FileOpenError = error { AccessDenied, OutOfMemory, FileNotFound, }; const AllocationError = error { OutOfMemory, }; test "implicit cast superset to subset" { foo(FileOpenError.OutOfMemory) catch {}; } fn foo(err: FileOpenError) AllocationError { return err; } {#code_end#}

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

{#code_begin|syntax#} const err = error.FileNotFound; {#code_end#}

This is equivalent to:

{#code_begin|syntax#} const err = (error {FileNotFound}).FileNotFound; {#code_end#}

This becomes useful when using {#link|Inferred Error Sets#}.

{#header_open|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 a {#link|switch#}.

{#header_close#} {#header_close#} {#header_open|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:

{#code_begin|test#} pub fn parseU64(buf: []const u8, radix: u8) !u64 { var x: u64 = 0; for (buf) |c| { const digit = charToDigit(c); if (digit >= radix) { return error.InvalidChar; } // x *= radix if (@mulWithOverflow(u64, x, radix, &x)) { return error.Overflow; } // x += digit if (@addWithOverflow(u64, x, digit, &x)) { return error.Overflow; } } return x; } fn charToDigit(c: u8) u8 { return switch (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); } {#code_end#}

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 a u64. Both types implicitly cast to error!u64.

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

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

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

{#code_begin|syntax#} fn doAThing(str: []u8) void { const number = parseU64(str, 10) catch 13; // ... } {#code_end#}

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 binary catch operator must match the unwrapped error union type, or be of type noreturn.

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

{#code_begin|syntax#} fn doAThing(str: []u8) !void { const number = parseU64(str, 10) catch |err| return err; // ... } {#code_end#}

There is a shortcut for this. The try expression:

{#code_begin|syntax#} fn doAThing(str: []u8) !void { const number = try parseU64(str, 10); // ... } {#code_end#}

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:

{#code_begin|syntax#}const number = parseU64("1234", 10) catch unreachable;{#code_end#}

Here we know for sure that "1234" will parse successfully. So we put the unreachable 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 there was 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 the if and switch expression:

{#code_begin|syntax#} fn doAThing(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, } } {#code_end#}

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

Example:

{#code_begin|syntax#} fn createFoo(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() ?? return error.OutOfMemory; // tmp_buf is truly a temporary resource, and we for sure want to clean it up // before this block leaves scope defer deallocateTmpBuffer(tmp_buf); if (param > 1337) return error.InvalidParam; // here the errdefer will not run since we're returning success from the function. // but the defer will run! return foo; } {#code_end#}

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

A couple of other tidbits about error handling:

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

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

{#code_begin|test#} 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); } {#code_end#}

TODO the || operator for error sets

{#header_open|Inferred Error Sets#}

TODO

{#header_close#} {#header_close#} {#header_open|Error Return Traces#}

TODO

{#header_close#} {#header_close#} {#header_open|Nullables#}

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

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

{#code_begin|syntax#} // normal integer const normal_int: i32 = 1234; // nullable integer const nullable_int: ?i32 = 5678; {#code_end#}

Now the variable nullable_int could be an i32, or null.

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

Zig does not have them.

Instead, you can use a nullable pointer. This secretly compiles down to a normal pointer, since we know we can use 0 as the null value for the nullable 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 reference
void *malloc(size_t size);

struct Foo *do_a_thing(void) {
    char *ptr = malloc(1234);
    if (!ptr) return NULL;
    // ...
}

Zig code

{#code_begin|syntax#} // malloc prototype included for reference extern fn malloc(size: size_t) ?&u8; fn doAThing() ?&Foo { const ptr = malloc(1234) ?? return null; // ... } {#code_end#}

Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr" is &u8 not ?&u8. The ?? operator unwrapped the nullable type and therefore ptr 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:

{#code_begin|syntax#} fn doAThing(nullable_foo: ?&Foo) void { // do some stuff if (nullable_foo) |foo| { doSomethingWithFoo(foo); } // do some stuff } {#code_end#}

Once again, the notable thing here is that inside the if block, foo is no longer a nullable 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)) in GCC. The optimizer can sometimes make better decisions knowing that pointer arguments cannot be null.

{#header_open|Nullable Type#}

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

{#code_begin|test#} const assert = @import("std").debug.assert; test "nullable type" { // Declare a nullable and implicitly cast from null: var foo: ?i32 = null; // Implicitly cast from child type of a nullable foo = 1234; // Use compile-time reflection to access the child type of the nullable: comptime assert(@typeOf(foo).Child == i32); } {#code_end#} {#header_close#} {#header_close#} {#header_open|Casting#}

TODO: explain implicit vs explicit casting

TODO: resolve peer types builtin

TODO: truncate builtin

TODO: bitcast builtin

TODO: int to ptr builtin

TODO: ptr to int builtin

TODO: ptrcast builtin

TODO: explain number literals vs concrete types

{#header_close#} {#header_open|void#}

TODO: assigning void has no codegen

TODO: hashmap with void becomes a set

TODO: difference between c_void and void

TODO: void is the default return value of functions

TODO: functions require assigning the return value

{#header_close#} {#header_open|this#}

TODO: example of this referring to Self struct

TODO: example of this referring to recursion function

TODO: example of this referring to basic block for @setRuntimeSafety

{#header_close#} {#header_open|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.

{#header_open|Introducing the Compile-Time Concept#} {#header_open|Compile-Time Parameters#}

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

{#code_begin|syntax#} fn max(comptime T: type, a: T, b: T) T { return if (a > b) a else b; } fn gimmeTheBiggerFloat(a: f32, b: f32) f32 { return max(f32, a, b); } fn gimmeTheBiggerInteger(a: u64, b: u64) u64 { return max(u64, a, b); } {#code_end#}

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 at compile-time, which is why the parameter T in the above snippet must be marked with comptime.

A comptime parameter means that:

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

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

{#code_begin|test_err|unable to evaluate constant expression#} fn max(comptime T: type, a: T, b: T) T { return if (a > b) a else b; } test "try to pass a runtime type" { foo(false); } fn foo(condition: bool) void { const result = max( if (condition) f32 else u64, 1234, 5678); } {#code_end#}

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 have compile-time duck typing.

For example:

{#code_begin|test_err|operator not allowed for type 'bool'#} fn max(comptime T: type, a: T, b: T) T { return if (a > b) a else b; } test "try to compare bools" { _ = max(bool, true, false); } {#code_end#}

On the flip side, inside the function definition with the comptime 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:

{#code_begin|test#} fn max(comptime T: type, a: T, b: T) T { if (T == bool) { return a or b; } else if (a > b) { return a; } else { return b; } } test "try to compare bools" { @import("std").debug.assert(max(bool, false, true) == true); } {#code_end#}

This works because Zig implicitly inlines if 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 for max in this situation looks like this:

{#code_begin|syntax#} fn max(a: bool, b: bool) bool { return a or b; } {#code_end#}

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 for switch expressions - they are implicitly inlined when the target expression is compile-time known.

{#header_close#} {#header_open|Compile-Time Variables#}

In Zig, the programmer can label variables as comptime. 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 can inline loops allows us to write a function which is partially evaluated at compile-time and partially at run-time.

For example:

{#code_begin|test|comptime_vars#} const assert = @import("std").debug.assert; const CmdFn = struct { name: []const u8, func: fn(i32) i32, }; const cmd_fns = []CmdFn{ CmdFn {.name = "one", .func = one}, CmdFn {.name = "two", .func = two}, CmdFn {.name = "three", .func = three}, }; fn one(value: i32) i32 { return value + 1; } fn two(value: i32) i32 { return value + 2; } fn three(value: i32) i32 { return value + 3; } fn performFn(comptime prefix_char: u8, start_value: i32) i32 { var result: i32 = start_value; comptime var i = 0; inline while (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); } {#code_end#}

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 function performFn is generated three different times, for the different values of prefix_char provided:

{#code_begin|syntax#} // From the line: // assert(performFn('t', 1) == 6); fn performFn(start_value: i32) i32 { var result: i32 = start_value; result = two(result); result = three(result); return result; } {#code_end#} {#code_begin|syntax#} // From the line: // assert(performFn('o', 0) == 1); fn performFn(start_value: i32) i32 { var result: i32 = start_value; result = one(result); return result; } {#code_end#} {#code_begin|syntax#} // From the line: // assert(performFn('w', 99) == 99); fn performFn(start_value: i32) i32 { var result: i32 = start_value; return result; } {#code_end#}

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 what should 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.

{#header_close#} {#header_open|Compile-Time Expressions#}

In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can use a comptime 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:

{#code_begin|test_err|unable to evaluate constant expression#} extern fn exit() noreturn; test "foo" { comptime { exit(); } } {#code_end#}

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

Within a comptime expression:

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

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

Let's look at an example:

{#code_begin|test#} const assert = @import("std").debug.assert; fn fibonacci(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-time comptime { assert(fibonacci(7) == 13); } } {#code_end#}

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

{#code_begin|test_err|operation caused overflow#} const assert = @import("std").debug.assert; fn fibonacci(index: u32) u32 { //if (index < 2) return index; return fibonacci(index - 1) + fibonacci(index - 2); } test "fibonacci" { comptime { assert(fibonacci(7) == 13); } } {#code_end#}

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?

{#code_begin|test_err|evaluation exceeded 1000 backwards branches#} const assert = @import("std").debug.assert; fn fibonacci(index: i32) i32 { //if (index < 2) return index; return fibonacci(index - 1) + fibonacci(index - 2); } test "fibonacci" { comptime { assert(fibonacci(7) == 13); } } {#code_end#}

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 {#link|@setEvalBranchQuota#} to change the default number 1000 to something else.

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

{#code_begin|test_err|encountered @panic at compile-time#} const assert = @import("std").debug.assert; fn fibonacci(index: i32) i32 { if (index < 2) return index; return fibonacci(index - 1) + fibonacci(index - 2); } test "fibonacci" { comptime { assert(fibonacci(7) == 99999); } } {#code_end#}

What happened is Zig started interpreting the assert function with the parameter ok set to false. When the interpreter hit unreachable 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 implicitly comptime expressions. This means that we can use functions to initialize complex static data. For example:

{#code_begin|test#} const first_25_primes = firstNPrimes(25); const sum_of_first_25_primes = sum(first_25_primes); fn firstNPrimes(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; } fn sum(numbers: []const i32) 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); } {#code_end#}

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 the sum function as is with a slice of numbers whose length and values were only known at run-time.

{#header_close#} {#header_close#} {#header_open|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 generic List data structure, that we will instantiate with the type i32. In Zig we refer to the type as List(i32).

{#code_begin|syntax#} fn List(comptime T: type) type { return struct { items: []T, len: usize, }; } {#code_end#}

That's it. It's a function that returns an anonymous struct. 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:

{#code_begin|syntax#} const Node = struct { next: &Node, name: []u8, }; {#code_end#}

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.

{#header_close#} {#header_open|Case Study: printf in Zig#}

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

{#code_begin|exe|printf#} const warn = @import("std").debug.warn; const a_number: i32 = 1234; const a_string = "foobar"; pub fn main() void { warn("here is a string: '{}' here is a number: {}\n", a_string, a_number); } {#code_end#}

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

{#code_begin|syntax#} /// Calls print and then flushes the buffer. pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) error!void { const State = enum { Start, OpenBrace, CloseBrace, }; comptime var start_index: usize = 0; comptime var state = State.Start; comptime var next_arg: usize = 0; inline for (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(); } {#code_end#}

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:

{#code_begin|syntax#} pub fn printf(self: &OutStream, arg0: i32, arg1: []const u8) !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(); } {#code_end#}

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

{#code_begin|syntax#} pub fn printValue(self: &OutStream, value: var) !void { const T = @typeOf(value); if (@isInteger(T)) { return self.printInt(T, value); } else if (@isFloat(T)) { return self.printFloat(T, value); } else if (@canImplicitCast([]const u8, value)) { const casted_value = ([]const u8)(value); return self.write(casted_value); } else { @compileError("Unable to print type '" ++ @typeName(T) ++ "'"); } } {#code_end#}

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

{#code_begin|test_err|Unused arguments#} 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); } {#code_end#}

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 []const u8:

{#code_begin|exe|printf#} 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"; pub fn main() void { warn(fmt, a_string, a_number); } {#code_end#}

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.

TODO: suggestion to not use inline unless necessary

{#header_close#} {#header_close#} {#header_open|inline#}

TODO: inline while

TODO: inline for

TODO: suggestion to not use inline unless necessary

{#header_close#} {#header_open|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

{#header_close#} {#header_open|Atomics#}

TODO: @fence()

TODO: @atomic rmw

TODO: builtin atomic memory ordering enum

{#header_close#} {#header_open|Builtin Functions#}

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

{#header_open|@addWithOverflow#}
@addWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool

Performs *result = a + b. If overflow or underflow occurs, stores the overflowed bits in result and returns true. If no overflow or underflow occurs, returns false.

{#header_close#} {#header_open|@ArgType#}

TODO

{#header_close#} {#header_open|@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, a bool, or an integer whose bit count meets these requirements:

  • At least 8
  • At most the same as usize
  • Power of 2

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

{#header_close#} {#header_open|@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:

  • Convert f32 to u32 bits
  • Convert i32 to u32 preserving twos complement

Works at compile-time if value 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.

{#header_close#} {#header_open|@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.

{#header_close#} {#header_open|@alignCast#}
@alignCast(comptime alignment: u29, ptr: var) -> var

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

A {#link|pointer alignment safety check|Incorrect Pointer Alignment#} is added to the generated code to make sure the pointer is aligned as promised.

{#header_close#} {#header_open|@alignOf#}
@alignOf(comptime T: type) -> (number literal)

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 {#link|@sizeOf(T)|@sizeOf#}.

{#see_also|Alignment#} {#header_close#} {#header_open|@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|Import from C Header File|@cInclude|@cImport|@cUndef|void#} {#header_close#} {#header_open|@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:

  • To avoid a symbol collision, for example if foo.h and bar.h both #define CONNECTION_COUNT
  • To analyze the C code with different preprocessor defines
{#see_also|Import from C Header File|@cInclude|@cDefine|@cUndef#} {#header_close#} {#header_open|@cInclude#}
@cInclude(comptime path: []u8)

This function can only occur inside @cImport.

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

{#see_also|Import from C Header File|@cImport|@cDefine|@cUndef#} {#header_close#} {#header_open|@cUndef#}
@cUndef(comptime name: []u8)

This function can only occur inside @cImport.

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

{#see_also|Import from C Header File|@cImport|@cDefine|@cInclude#} {#header_close#} {#header_open|@canImplicitCast#}
@canImplicitCast(comptime T: type, value) -> bool

Returns whether a value can be implicitly casted to a given type.

{#header_close#} {#header_open|@clz#}
@clz(x: T) -> U

This function counts the number of leading zeroes in x which is an integer type T.

The return type U is an unsigned integer with the minimum number of bits that can represent the value T.bit_count.

If x is zero, @clz returns T.bit_count.

{#header_close#} {#header_open|@cmpxchg#}
@cmpxchg(ptr: &T, cmp: T, new: T, success_order: AtomicOrder, fail_order: AtomicOrder) -> bool

This function performs an atomic compare exchange operation.

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

@typeOf(ptr).alignment must be >= @sizeOf(T).

{#see_also|Compile Variables#} {#header_close#} {#header_open|@compileError#}
@compileError(comptime msg: []u8)

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

There are several ways that code avoids being semantically checked, such as using if or switch with compile time constants, and comptime functions.

{#header_close#} {#header_open|@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.

{#code_begin|test_err|found compile log statement#} 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); } {#code_end#}

will ouput:

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

{#code_begin|test#} 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); } {#code_end#} {#header_close#} {#header_open|@ctz#}
@ctz(x: T) -> U

This function counts the number of trailing zeroes in x which is an integer type T.

The return type U is an unsigned integer with the minimum number of bits that can represent the value T.bit_count.

If x is zero, @ctz returns T.bit_count.

{#header_close#} {#header_open|@divExact#}
@divExact(numerator: T, denominator: T) -> T

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

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

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

{#see_also|@divTrunc|@divFloor#} {#header_close#} {#header_open|@divFloor#}
@divFloor(numerator: T, denominator: T) -> T

Floored division. Rounds toward negative infinity. For unsigned integers it is the same as numerator / denominator. Caller guarantees denominator != 0 and !(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1).

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

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

{#see_also|@divTrunc|@divExact#} {#header_close#} {#header_open|@divTrunc#}
@divTrunc(numerator: T, denominator: T) -> T

Truncated division. Rounds toward zero. For unsigned integers it is the same as numerator / denominator. Caller guarantees denominator != 0 and !(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1).

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

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

{#see_also|@divFloor|@divExact#} {#header_close#} {#header_open|@embedFile#}
@embedFile(comptime path: []const u8) -> [X]u8

This function returns a compile time constant fixed-size array with length equal to the byte count of the file given by path. 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|@import#} {#header_close#} {#header_open|@export#}
@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) -> []const u8

Creates a symbol in the output object file.

{#header_close#} {#header_open|@tagName#}
@tagName(value: var) -> []const u8

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

{#header_close#} {#header_open|@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.

{#header_close#} {#header_open|@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 for err, then no error name table will be generated.

{#header_close#} {#header_open|@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`.

{#header_close#} {#header_open|@fence#}
@fence(order: AtomicOrder)

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

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

{#see_also|Compile Variables#} {#header_close#} {#header_open|@fieldParentPtr#}
@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8,
    field_ptr: &T) -> &ParentType

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

{#header_close#} {#header_open|@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.

{#header_close#} {#header_open|@import#}
@import(comptime path: []u8) -> (namespace)

This function finds a zig file corresponding to path 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:

  • @import("std") - Zig Standard Library
  • @import("builtin") - Compiler-provided types and variables
{#see_also|Compile Variables|@embedFile#} {#header_close#} {#header_open|@inlineCall#}
@inlineCall(function: X, args: ...) -> Y

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

{#code_begin|test#} const assert = @import("std").debug.assert; test "inline function call" { assert(@inlineCall(add, 3, 9) == 12); } fn add(a: i32, b: i32) i32 { return a + b; } {#code_end#}

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|@noInlineCall#} {#header_close#} {#header_open|@intToPtr#}
@intToPtr(comptime DestType: type, int: usize) -> DestType

Converts an integer to a pointer. To convert the other way, use {#link|@ptrToInt#}.

{#header_close#} {#header_open|@IntType#}
@IntType(comptime is_signed: bool, comptime bit_count: u8) -> type

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

{#header_close#} {#header_open|@maxValue#}
@maxValue(comptime T: type) -> (number literal)

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

The result is a compile time constant.

{#header_close#} {#header_open|@memberCount#}
@memberCount(comptime T: type) -> (number literal)

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.

{#header_close#} {#header_open|@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.

{#header_close#} {#header_open|@memberType#}
@memberType(comptime T: type, comptime index: usize) -> type

Returns the field type of a struct or union.

{#header_close#} {#header_open|@memcpy#}
@memcpy(noalias dest: &u8, noalias source: &const u8, byte_count: usize)

This function copies bytes from one region of memory to another. dest and source 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]);
{#header_close#} {#header_open|@memset#}
@memset(dest: &u8, c: u8, byte_count: usize)

This function sets a region of memory to c. 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);
{#header_close#} {#header_open|@minValue#}
@minValue(comptime T: type) -> (number literal)

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

The result is a compile time constant.

{#header_close#} {#header_open|@mod#}
@mod(numerator: T, denominator: T) -> T

Modulus division. For unsigned integers this is the same as numerator % denominator. Caller guarantees denominator > 0.

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

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

{#see_also|@rem#} {#header_close#} {#header_open|@mulWithOverflow#}
@mulWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool

Performs *result = a * b. If overflow or underflow occurs, stores the overflowed bits in result and returns true. If no overflow or underflow occurs, returns false.

{#header_close#} {#header_open|@noInlineCall#}
@noInlineCall(function: var, args: ...) -> var

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

const assert = @import("std").debug.assert;
test "noinline function call" {
    assert(@noInlineCall(add, 3, 9) == 12);
}

fn add(a: i32, b: i32) -> i32 { a + b }

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|@inlineCall#} {#header_close#} {#header_open|@offsetOf#}
@offsetOf(comptime T: type, comptime field_name: [] const u8) -> (number literal)

This function returns the byte offset of a field relative to its containing struct.

{#header_close#} {#header_open|@OpaqueType#}
@OpaqueType() -> type

Creates a new type with an unknown size and alignment.

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

{#code_begin|test_err|expected type '&Derp', found '&Wat'#} const Derp = @OpaqueType(); const Wat = @OpaqueType(); extern fn bar(d: &Derp) void; export fn foo(w: &Wat) void { bar(w); } test "call foo" { foo(undefined); } {#code_end#} {#header_close#} {#header_open|@panic#}
@panic(message: []const u8) -> noreturn

Invokes the panic handler function. By default the panic handler function calls the public panic function exposed in the root source file, or if there is not one specified, invokes the one provided in std/special/panic.zig.

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

  • From library code, calling the programmer's panic function if they exposed one in the root source file.
  • When mixing C and Zig code, calling the canonical panic implementation across multiple .o files.
{#see_also|Root Source File#} {#header_close#} {#header_open|@ptrCast#}
@ptrCast(comptime DestType: type, value: var) -> DestType

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

{#header_close#} {#header_open|@ptrToInt#}
@ptrToInt(value: var) -> usize

Converts value to a usize which is the address of the pointer. value can be one of these types:

  • &T
  • ?&T
  • fn()
  • ?fn()

To convert the other way, use {#link|@intToPtr#}

{#header_close#} {#header_open|@rem#}
@rem(numerator: T, denominator: T) -> T

Remainder division. For unsigned integers this is the same as numerator % denominator. Caller guarantees denominator > 0.

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

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

{#see_also|@mod#} {#header_close#} {#header_open|@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.

{#header_close#} {#header_open|@setAlignStack#}
@setAlignStack(comptime alignment: u29)

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

{#header_close#} {#header_open|@setCold#}
@setCold(is_cold: bool)

Tells the optimizer that a function is rarely called.

{#header_close#} {#header_open|@setRuntimeSafety#}
@setRuntimeSafety(safety_on: bool)

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

{#header_close#} {#header_open|@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 the new_quota is smaller than the default quota (1000) or a previously explicitly set quota, it is ignored.

Example:

{#code_begin|test_err|evaluation exceeded 1000 backwards branches#} test "foo" { comptime { var i = 0; while (i < 1001) : (i += 1) {} } } {#code_end#}

Now we use @setEvalBranchQuota:

{#code_begin|test#} test "foo" { comptime { @setEvalBranchQuota(1001); var i = 0; while (i < 1001) : (i += 1) {} } } {#code_end#} {#see_also|comptime#} {#header_close#} {#header_open|@setFloatMode#}
@setFloatMode(scope, mode: @import("builtin").FloatMode)

Sets the floating point mode for a given scope. Possible values are:

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

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

{#see_also|Compile Variables#} {#header_close#} {#header_open|@setGlobalSection#}
@setGlobalSection(global_variable_name, comptime section_name: []const u8) -> bool

Puts the global variable in the specified section.

{#header_close#} {#header_open|@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 of shift_amt is an unsigned integer with log2(T.bit_count) bits. This is because shift_amt >= T.bit_count is undefined behavior.

{#see_also|@shrExact|@shlWithOverflow#} {#header_close#} {#header_open|@shlWithOverflow#}
@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: &T) -> bool

Performs *result = a << b. If overflow or underflow occurs, stores the overflowed bits in result and returns true. If no overflow or underflow occurs, returns false.

The type of shift_amt is an unsigned integer with log2(T.bit_count) bits. This is because shift_amt >= T.bit_count is undefined behavior.

{#see_also|@shlExact|@shrExact#} {#header_close#} {#header_open|@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 of shift_amt is an unsigned integer with log2(T.bit_count) bits. This is because shift_amt >= T.bit_count is undefined behavior.

{#see_also|@shlExact|@shlWithOverflow#} {#header_close#} {#header_open|@sizeOf#}
@sizeOf(comptime T: type) -> (number literal)

This function returns the number of bytes it takes to store T in memory.

The result is a target-specific compile time constant.

{#header_close#} {#header_open|@subWithOverflow#}
@subWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool

Performs *result = a - b. If overflow or underflow occurs, stores the overflowed bits in result and returns true. If no overflow or underflow occurs, returns false.

{#header_close#} {#header_open|@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.

{#header_close#} {#header_open|@typeId#}
@typeId(comptime T: type) -> @import("builtin").TypeId

Returns which kind of type something is. Possible values:

{#code_begin|syntax#} pub const TypeId = enum { Type, Void, Bool, NoReturn, Int, Float, Pointer, Array, Struct, FloatLiteral, IntLiteral, UndefinedLiteral, NullLiteral, Nullable, ErrorUnion, Error, Enum, Union, Fn, Namespace, Block, BoundFn, ArgTuple, Opaque, }; {#code_end#} {#header_close#} {#header_open|@typeName#}
@typeName(T: type) -> []u8

This function returns the string representation of a type.

{#header_close#} {#header_open|@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.

{#header_close#} {#header_close#} {#header_open|Build Mode#}

Zig has three build modes:

  • {#link|Debug#} (default)
  • {#link|ReleaseFast#}
  • {#link|ReleaseSafe#}

To add standard build options to a build.zig file:

{#code_begin|syntax#} const Builder = @import("std").build.Builder; pub fn build(b: &Builder) void { const exe = b.addExecutable("example", "example.zig"); exe.setBuildMode(b.standardReleaseOptions()); b.default_step.dependOn(&exe.step); } {#code_end#}

This causes these options to be available:

  -Drelease-safe=(bool)  optimizations on and safety on
  -Drelease-fast=(bool)  optimizations on and safety off
{#header_open|Debug#}
$ zig build-exe example.zig
  • Fast compilation speed
  • Safety checks enabled
  • Slow runtime performance
{#header_close#} {#header_open|ReleaseFast#}
$ zig build-exe example.zig --release-fast
  • Fast runtime performance
  • Safety checks disabled
  • Slow compilation speed
{#header_close#} {#header_open|ReleaseSafe#}
$ zig build-exe example.zig --release-safe
  • Medium runtime performance
  • Safety checks enabled
  • Slow compilation speed
{#see_also|Compile Variables|Zig Build System|Undefined Behavior#} {#header_close#} {#header_close#} {#header_open|Undefined Behavior#}

Zig has many instances of undefined behavior. If undefined behavior is detected at compile-time, Zig emits an error. Most undefined behavior that cannot be detected at compile-time can be detected at runtime. In these cases, Zig has safety checks. Safety checks can be disabled on a per-block basis with @setRuntimeSafety. The {#link|ReleaseFast#} build mode disables all safety checks in order to facilitate optimizations.

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

{#code_begin|test_err|reached unreachable code#} test "safety check" { unreachable; } {#code_end#} {#header_open|Reaching Unreachable Code#}

At compile-time:

{#code_begin|test_err|unable to evaluate constant expression#} comptime { assert(false); } fn assert(ok: bool) void { if (!ok) unreachable; // assertion failure } {#code_end#}

At runtime crashes with the message reached unreachable code and a stack trace.

{#header_close#} {#header_open|Index out of Bounds#}

At compile-time:

{#code_begin|test_err|index 5 outside array of size 5#} comptime { const array = "hello"; const garbage = array[5]; } {#code_end#}

At runtime crashes with the message index out of bounds and a stack trace.

{#header_close#} {#header_open|Cast Negative Number to Unsigned Integer#}

At compile-time:

{#code_begin|test_err|attempt to cast negative value to unsigned integer#} comptime { const value: i32 = -1; const unsigned = u32(value); } {#code_end#}

At runtime crashes with the message attempt to cast negative value to unsigned integer and a stack trace.

If you are trying to obtain the maximum value of an unsigned integer, use @maxValue(T), where T is the integer type, such as u32.

{#header_close#} {#header_open|Cast Truncates Data#}

At compile-time:

{#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#} comptime { const spartan_count: u16 = 300; const byte = u8(spartan_count); } {#code_end#}

At runtime crashes with the message integer cast truncated bits and a stack trace.

If you are trying to truncate bits, use @truncate(T, value), where T is the integer type, such as u32, and value is the value you want to truncate.

{#header_close#} {#header_open|Integer Overflow#} {#header_open|Default Operations#}

The following operators can cause integer overflow:

  • + (addition)
  • - (subtraction)
  • - (negation)
  • * (multiplication)
  • / (division)
  • @divTrunc (division)
  • @divFloor (division)
  • @divExact (division)

Example with addition at compile-time:

{#code_begin|test_err|operation caused overflow#} comptime { var byte: u8 = 255; byte += 1; } {#code_end#}

At runtime crashes with the message integer overflow and a stack trace.

{#header_close#} {#header_open|Standard Library Math Functions#}

These functions provided by the standard library return possible errors.

  • @import("std").math.add
  • @import("std").math.sub
  • @import("std").math.mul
  • @import("std").math.divTrunc
  • @import("std").math.divFloor
  • @import("std").math.divExact
  • @import("std").math.shl

Example of catching an overflow for addition:

{#code_begin|exe_err#} const math = @import("std").math; const warn = @import("std").debug.warn; pub fn main() !void { var byte: u8 = 255; byte = if (math.add(u8, byte, 1)) |result| result else |err| { warn("unable to add one: {}\n", @errorName(err)); return err; }; warn("result: {}\n", byte); } {#code_end#} {#header_close#} {#header_open|Builtin Overflow Functions#}

These builtins return a bool of whether or not overflow occurred, as well as returning the overflowed bits:

  • @addWithOverflow
  • @subWithOverflow
  • @mulWithOverflow
  • @shlWithOverflow

Example of @addWithOverflow:

{#code_begin|exe#} const warn = @import("std").debug.warn; pub fn main() 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); } } {#code_end#} {#header_close#} {#header_open|Wrapping Operations#}

These operations have guaranteed wraparound semantics.

  • +% (wraparound addition)
  • -% (wraparound subtraction)
  • -% (wraparound negation)
  • *% (wraparound multiplication)
{#code_begin|test#} 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)); } {#code_end#} {#header_close#} {#header_close#} {#header_open|Exact Left Shift Overflow#}

At compile-time:

{#code_begin|test_err|operation caused overflow#} comptime { const x = @shlExact(u8(0b01010101), 2); } {#code_end#}

At runtime crashes with the message left shift overflowed bits and a stack trace.

{#header_close#} {#header_open|Exact Right Shift Overflow#}

At compile-time:

{#code_begin|test_err|exact shift shifted out 1 bits#} comptime { const x = @shrExact(u8(0b10101010), 2); } {#code_end#}

At runtime crashes with the message right shift overflowed bits and a stack trace.

{#header_close#} {#header_open|Division by Zero#}

At compile-time:

{#code_begin|test_err|division by zero#} comptime { const a: i32 = 1; const b: i32 = 0; const c = a / b; } {#code_end#}

At runtime crashes with the message division by zero and a stack trace.

{#header_close#} {#header_open|Remainder Division by Zero#}

At compile-time:

{#code_begin|test_err|division by zero#} comptime { const a: i32 = 10; const b: i32 = 0; const c = a % b; } {#code_end#}

At runtime crashes with the message remainder division by zero and a stack trace.

{#header_close#} {#header_open|Exact Division Remainder#}

TODO

{#header_close#} {#header_open|Slice Widen Remainder#}

TODO

{#header_close#} {#header_open|Attempt to Unwrap Null#}

At compile-time:

{#code_begin|test_err|unable to unwrap null#} comptime { const nullable_number: ?i32 = null; const number = ??nullable_number; } {#code_end#}

At runtime crashes with the message attempt to unwrap null and a stack trace.

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

{#code_begin|exe|test#} const warn = @import("std").debug.warn; pub fn main() void { const nullable_number: ?i32 = null; if (nullable_number) |number| { warn("got number: {}\n", number); } else { warn("it's null\n"); } } {#code_end#} {#header_close#} {#header_open|Attempt to Unwrap Error#}

At compile-time:

{#code_begin|test_err|caught unexpected error 'UnableToReturnNumber'#} comptime { const number = getNumberOrFail() catch unreachable; } fn getNumberOrFail() !i32 { return error.UnableToReturnNumber; } {#code_end#}

At runtime crashes with the message attempt to unwrap error: ErrorCode and a stack trace.

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

{#code_begin|exe#} const warn = @import("std").debug.warn; pub fn main() void { const result = getNumberOrFail(); if (result) |number| { warn("got number: {}\n", number); } else |err| { warn("got error: {}\n", @errorName(err)); } } fn getNumberOrFail() !i32 { return error.UnableToReturnNumber; } {#code_end#} {#header_close#} {#header_open|Invalid Error Code#}

At compile-time:

{#code_begin|test_err|integer value 11 represents no error#} comptime { const err = error.AnError; const number = u32(err) + 10; const invalid_err = error(number); } {#code_end#}

At runtime crashes with the message invalid error code and a stack trace.

{#header_close#} {#header_open|Invalid Enum Cast#}

TODO

{#header_close#} {#header_open|Incorrect Pointer Alignment#}

TODO

{#header_close#} {#header_open|Wrong Union Field Access#}

TODO

{#header_close#} {#header_close#} {#header_open|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|Pointers#} {#header_close#} {#header_open|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.

{#code_begin|syntax#} const builtin = @import("builtin"); const separator = if (builtin.os == builtin.Os.windows) '\\' else '/'; {#code_end#}

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

{#code_begin|syntax#} pub const StackTrace = struct { index: usize, instruction_addresses: []usize, }; pub const Os = enum { freestanding, ananas, cloudabi, dragonfly, freebsd, fuchsia, ios, kfreebsd, linux, lv2, macosx, netbsd, openbsd, solaris, windows, haiku, minix, rtems, nacl, cnk, bitrig, aix, cuda, nvcl, amdhsa, ps4, elfiamcu, tvos, watchos, mesa3d, contiki, zen, }; pub const Arch = enum { 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, xcore, nvptx, nvptx64, le32, le64, amdil, amdil64, hsail, hsail64, spir, spir64, kalimbav3, kalimbav4, kalimbav5, shave, lanai, wasm32, wasm64, renderscript32, renderscript64, }; pub const Environ = enum { unknown, gnu, gnuabi64, gnueabi, gnueabihf, gnux32, code16, eabi, eabihf, android, musl, musleabi, musleabihf, msvc, itanium, cygnus, amdopencl, coreclr, opencl, }; pub const ObjectFormat = enum { unknown, coff, elf, macho, wasm, }; pub const GlobalLinkage = enum { Internal, Strong, Weak, LinkOnce, }; pub const AtomicOrder = enum { Unordered, Monotonic, Acquire, Release, AcqRel, SeqCst, }; pub const Mode = enum { Debug, ReleaseSafe, ReleaseFast, }; pub const TypeId = enum { Type, Void, Bool, NoReturn, Int, Float, Pointer, Array, Struct, FloatLiteral, IntLiteral, UndefinedLiteral, NullLiteral, Nullable, ErrorUnion, Error, Enum, Union, Fn, Namespace, Block, BoundFn, ArgTuple, Opaque, }; pub const FloatMode = enum { Optimized, Strict, }; pub const Endian = enum { Big, Little, }; pub const endian = Endian.Little; pub const is_test = false; pub const os = Os.linux; pub const arch = Arch.x86_64; pub const environ = Environ.gnu; pub const object_format = ObjectFormat.elf; pub const mode = Mode.Debug; pub const link_libc = false; pub const have_error_return_tracing = true; {#code_end#} {#see_also|Build Mode#} {#header_close#} {#header_open|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() }

{#header_close#} {#header_open|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

{#header_close#} {#header_open|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

{#header_close#} {#header_open|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.

{#header_open|C Type Primitives#}

These have guaranteed C ABI compatibility and can be used like any other type.

  • c_short
  • c_ushort
  • c_int
  • c_uint
  • c_long
  • c_ulong
  • c_longlong
  • c_ulonglong
  • c_longdouble
  • c_void
{#see_also|Primitive Types#} {#header_close#} {#header_open|C String Literals#} {#code_begin|exe#} {#link_libc#} extern fn puts(&const u8) void; pub fn main() void { puts(c"this has a null terminator"); puts( c\\and so c\\does this c\\multiline C string literal ); } {#code_end#} {#see_also|String Literals#} {#header_close#} {#header_open|Import from C Header File#}

The @cImport builtin function can be used to directly import symbols from .h files:

{#code_begin|exe#} {#link_libc#} const c = @cImport({ // See https://github.com/zig-lang/zig/issues/515 @cDefine("_NO_CRT_STDIO_INLINE", "1"); @cInclude("stdio.h"); }); pub fn main() void { _ = c.printf(c"hello\n"); } {#code_end#}

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:

{#code_begin|syntax#} 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"); }); {#code_end#} {#see_also|@cImport|@cInclude|@cDefine|@cUndef|@import#} {#header_close#} {#header_open|Mixing Object Files#}

You can mix Zig object files with any other object files that respect the C ABI. Example:

base64.zig

{#code_begin|syntax#} const base64 = @import("std").base64; export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, 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; } {#code_end#}

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

{#code_begin|syntax#} const Builder = @import("std").build.Builder; pub fn build(b: &Builder) void { const obj = b.addObject("base64", "base64.zig"); const exe = b.addCExecutable("test"); exe.addCompileFlags([][]const u8 { "-std=c99", }); exe.addSourceFile("test.c"); exe.addObject(obj); exe.setOutputPath("."); b.default_step.dependOn(&exe.step); } {#code_end#} {#header_close#} {#header_open|Terminal#}
$ zig build
$ ./test
all your base are belong to us
{#see_also|Targets|Zig Build System#} {#header_close#} {#header_close#} {#header_open|Targets#}

Zig supports generating code for all targets that LLVM supports. Here is what it looks like to execute zig targets on a Linux x86_64 computer:

$ zig targets
Architectures:
  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
  renderscript64

Operating 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
  zen

Environments:
  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. It currently supports Linux x86_64. Not all standard library code requires operating system abstractions, however, so things such as generic data structures work an all above platforms.

{#header_close#} {#header_open|Style Guide#}

These coding conventions are not enforced by the compiler, but they are shipped in this documentation along with the compiler in order to provide a point of reference, should anyone wish to point to an authority on agreed upon Zig coding style.

{#header_open|Whitespace#}
  • 4 space indentation
  • Open braces on same line, unless you need to wrap.
  • If a list of things is longer than 2, put each item on its own line and exercise the abilty to put an extra comma at the end.
  • Line length: aim for 100; use common sense.
{#header_close#} {#header_open|Names#}

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

  • If x is a struct (or an alias of a struct), then x should be TitleCase.
  • If x otherwise identifies a type, x should have snake_case.
  • If x is callable, and x's return type is type, then x should be TitleCase.
  • If x is otherwise callable, then x should be camelCase.
  • Otherwise, x should be snake_case.

Acronyms, initialisms, proper nouns, or any other word that has capitalization rules in written English are subject to naming conventions just like any other word. Even acronyms that are only 2 letters long are subject to these conventions.

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 as ENOENT, follow the established convention.

{#header_close#} {#header_open|Examples#} {#code_begin|syntax#} 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; fn functionName(param_name: TypeName) void { var functionPointer = functionName; functionPointer(); functionPointer = otherFunction; functionPointer(); } const functionAlias = functionName; fn ListTemplateFunction(comptime ChildType: type, comptime fixed_size: usize) type { return List(ChildType, fixed_size); } fn ShortList(comptime T: type, comptime n: usize) type { return struct { field_name: [n]T, fn methodName() void {} }; } // The word XML loses its casing when used in Zig identifiers. const xml_document = \\ \\ \\ ; const XmlParser = struct {}; // The initials BE (Big Endian) are just another word in Zig identifier names. fn readU32Be() u32 {} {#code_end#}

See the Zig Standard Library for more examples.

{#header_close#} {#header_close#} {#header_open|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:

  • Ascii control characters, except for U+000a (LF): U+0000 - U+0009, U+000b - U+0001f, U+007f. (Note that Windows line endings (CRLF) are not allowed, and hard tabs are not allowed.)
  • Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).

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, see issue #663

{#header_close#} {#header_open|Grammar#}
Root = many(TopLevelItem) EOF

TopLevelItem = CompTimeExpression(Block) | TopLevelDecl | TestDecl

TestDecl = "test" String Block

TopLevelDecl = option("pub") (FnDef | ExternDecl | GlobalVarDecl | UseDecl)

GlobalVarDecl = option("export") VariableDeclaration ";"

LocalVarDecl = option("comptime") VariableDeclaration

VariableDeclaration = ("var" | "const") Symbol option(":" TypeExpr) option("align" "(" Expression ")") option("section" "(" Expression ")") "=" Expression

ContainerMember = (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 Block

ParamDeclList = "(" 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) | PrefixOpExpression

BlockOrExpression = Block | Expression

Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | CancelExpression | ResumeExpression

AsmExpression = "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 (UnwrapNullable | UnwrapError) | BoolOrExpression

UnwrapNullable = "??" Expression

UnwrapError = "catch" option("|" Symbol "|") Expression

AssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | UnwrapExpression

AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "*%=" | "+%=" | "-%="

BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body) | SuspendExpression(body)

CompTimeExpression(body) = "comptime" body

SwitchExpression = "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 | BoolAndExpression

ReturnExpression = "return" option(Expression)

TryExpression = "try" Expression

AwaitExpression = "await" Expression

BreakExpression = "break" option(":" Symbol) option(Expression)

CancelExpression = "cancel" Expression;

ResumeExpression = "resume" Expression;

Defer(body) = ("defer" | "deferror") body

IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))

SuspendExpression(body) = "suspend" option(("|" Symbol "|" 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 | ComparisonExpression

ComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression

ComparisonOperator = "==" | "!=" | "<" | ">" | "<=" | ">="

BinaryOrExpression = BinaryXorExpression "|" BinaryOrExpression | BinaryXorExpression

BinaryXorExpression = BinaryAndExpression "^" BinaryXorExpression | BinaryAndExpression

BinaryAndExpression = BitShiftExpression "&" BinaryAndExpression | BitShiftExpression

BitShiftExpression = AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression

BitShiftOperator = "<<" | ">>"

AdditionExpression = MultiplyExpression AdditionOperator AdditionExpression | MultiplyExpression

AdditionOperator = "+" | "-" | "++" | "+%" | "-%"

MultiplyExpression = CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression

CurlySuffixExpression = TypeExpr option(ContainerInitExpression)

MultiplyOperator = "||" | "*" | "/" | "%" | "**" | "*%"

PrefixOpExpression = PrefixOp TypeExpr | SuffixOpExpression

SuffixOpExpression = ("async" option("<" SuffixOpExpression ">") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)

FieldAccessExpression = "." Symbol

FnCallExpression = "(" list(Expression, ",") ")"

ArrayAccessExpression = "[" Expression "]"

SliceExpression = "[" Expression ".." option(Expression) "]"

ContainerInitExpression = "{" ContainerInitBody "}"

ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")

StructLiteralField = "." Symbol "=" Expression

PrefixOp = "!" | "-" | "~" | "*" | ("&" 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 | PromiseType

PromiseType = "promise" option("->" TypeExpr)

ArrayType : "[" option(Expression) "]" option("align" "(" Expression option(":" Integer ":" Integer) ")")) option("const") option("volatile") TypeExpr

GroupedExpression = "(" Expression ")"

KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "unreachable" | "suspend"

ErrorSetDecl = "error" "{" list(Symbol, ",") "}"

ContainerDecl = option("extern" | "packed")
  ("struct" option(GroupedExpression) | "union" option("enum" option(GroupedExpression) | GroupedExpression) | ("enum" option(GroupedExpression)))
  "{" many(ContainerMember) "}"
{#header_close#} {#header_open|Zen#}
  • Communicate intent precisely.
  • Edge cases matter.
  • Favor reading code over writing code.
  • Only one obvious way to do things.
  • Runtime crashes are better than bugs.
  • Compile errors are better than runtime crashes.
  • Incremental improvements.
  • Avoid local maximums.
  • Reduce the amount one must remember.
  • Minimize energy spent on coding style.
  • Together we serve end users.
{#header_close#}