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.
There are no multiline comments in Zig (e.g. like /* */
comments in C). This helps allow Zig to have the property that each line
of code can be tokenized out of context.
A doc comment is one that begins with exactly three slashes (i.e.
///
but not ////
);
multiple doc comments in a row are merged together to form a multiline
doc comment. The doc comment documents whatever immediately follows it.
Doc comments are only allowed in certain places; eventually, it will become a compile error have a doc comment in an unexpected place, such as in the middle of an expression, or just before a non-doc comment.
{#header_close#} {#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); // optional var optional_value: ?[]const u8 = null; assert(optional_value == null); warn("\noptional 1\ntype: {}\nvalue: {}\n", @typeName(@typeOf(optional_value)), optional_value); optional_value = "hi"; assert(optional_value != null); warn("\noptional 2\ntype: {}\nvalue: {}\n", @typeName(@typeOf(optional_value)), optional_value); // error 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 |
---|---|---|
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 |
f16 |
float |
16-bit floating point (10-bit mantissa) IEEE-754-2008 binary16 |
f32 |
float |
32-bit floating point (23-bit mantissa) IEEE-754-2008 binary32 |
f64 |
double |
64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64 |
f128 |
(none) | 128-bit floating point (112-bit mantissa) IEEE-754-2008 binary128 |
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 |
comptime_int |
(none) | Only allowed for {#link|comptime#}-known values. The type of integer literals. |
comptime_float |
(none) | Only allowed for {#link|comptime#}-known values. The type of float literals. |
In addition to the integer types above, arbitrary bit-width integers can be referenced by using
an identifier of i
or u followed by digits. For example, the identifier
i7
refers to a signed 7-bit integer.
Name | Description |
---|---|
true and false |
bool values |
null |
used to set an optional type to null |
undefined |
used to leave a value unspecified |
this |
refers to the thing in immediate scope |
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
.
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.
For a multiline C string literal, prepend c
to each \\
:
In this example the variable c_string_literal
has type [*]const char
and
has a terminating null byte.
Use the const
keyword to assign a value to an identifier:
const
applies to all of the bytes that the identifier immediately addresses. {#link|Pointers#} have their own const-ness.
If you need a variable that you can modify, use the var
keyword:
Variables must be initialized:
{#code_begin|test_err#} test "initialization" { var x: i32; x = 1; } {#code_end#} {#header_open|undefined#}Use undefined
to leave variables uninitialized:
undefined
can be {#link|implicitly cast|Implicit Casts#} to any type.
Once this happens, it is no longer possible to detect that the value is undefined
.
undefined
means the value could be anything, even something that is nonsense
according to the type. Translated into English, undefined
means "Not a meaningful
value. Using this value would be a bug. The value will be unused, or overwritten before being used."
In {#link|Debug#} mode, Zig writes 0xaa
bytes to undefined memory. This is to catch
bugs early, and to help detect use of undefined memory in a debugger.
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.
Zig has the following floating point types:
f16
- IEEE-754-2008 binary16f32
- IEEE-754-2008 binary32f64
- IEEE-754-2008 binary64f128
- IEEE-754-2008 binary128c_longdouble
- matches long double
for the target C ABI
Float literals have type comptime_float
which is guaranteed to hold at least all possible values
that the largest other floating point type can hold. Float literals {#link|implicitly cast|Implicit Casts#} to any other type.
By default floating point operations use Strict
mode,
but you can switch to Optimized
mode on a per-block basis:
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 |
---|---|---|---|
|
|
Addition.
|
|
|
|
Wrapping Addition.
|
|
|
|
Subtraction.
|
|
|
|
Wrapping Subtraction.
|
|
|
|
Negation.
|
|
|
|
Wrapping Negation.
|
|
|
|
Multiplication.
|
|
|
|
Wrapping Multiplication.
|
|
|
|
Divison.
|
|
|
|
Remainder Division.
|
|
|
|
Bit Shift Left.
|
|
|
|
Bit Shift Right.
|
|
|
|
Bitwise AND.
|
|
|
|
Bitwise OR.
|
|
|
|
Bitwise XOR.
|
|
|
|
Bitwise NOT. |
|
|
|
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#}.
|
|
|
|
Equivalent to:
|
|
|
|
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 .
|
|
|
|
If a is false , returns false
without evaluating b . Otherwise, returns b .
|
|
|
|
If a is true , returns true
without evaluating b . Otherwise, returns b .
|
|
|
|
Boolean NOT. |
|
|
|
Returns true if a and b are equal, otherwise returns false .
Invokes {#link|Peer Type Resolution#} for the operands.
|
|
|
|
Returns true if a is null , otherwise returns false .
|
|
|
|
Returns false if a and b are equal, otherwise returns true .
Invokes {#link|Peer Type Resolution#} for the operands.
|
|
|
|
Returns true if a is greater than b, otherwise returns false .
Invokes {#link|Peer Type Resolution#} for the operands.
|
|
|
|
Returns true if a is greater than or equal to b, otherwise returns false .
Invokes {#link|Peer Type Resolution#} for the operands.
|
|
|
|
Returns true if a is less than b, otherwise returns false .
Invokes {#link|Peer Type Resolution#} for the operands.
|
|
|
|
Returns true if a is less than or equal to b, otherwise returns false .
Invokes {#link|Peer Type Resolution#} for the operands.
|
|
|
|
Array concatenation.
|
|
|
|
Array multiplication.
|
|
|
|
Pointer dereference. |
|
|
All types | Address of. |
|
|
|
{#link|Merging Error Sets#} |
|
x() x[] x.y
a!b
!x -x -%x ~x &x ?x
x{} x.* x.?
! * / % ** *% ||
+ - ++ +% -%
<< >>
&
^
|
== != < > <= >=
and
or
orelse 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.* = @intCast(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 = @intCast(i32, i),
.y = @intCast(i32, i) * 2,
};
}
break :init initial_value;
};
const Point = struct {
x: i32,
y: i32,
};
test "compile-time array initalization" {
assert(fancy_array[4].x == 4);
assert(fancy_array[4].y == 8);
}
// call a function to initialize an 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" {
// Taking an address of an individual element gives a
// pointer to a single item. This kind of pointer
// does not support pointer arithmetic.
var array = []u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const ptr = &array[2];
assert(@typeOf(ptr) == *u8);
assert(array[2] == 3);
ptr.* += 1;
assert(array[2] == 4);
}
test "pointer slicing" {
// In Zig, we prefer slices over pointers to null-terminated arrays.
// You can turn an array into a slice using slice syntax:
var array = []u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const slice = array[2..4];
assert(slice.len == 2);
// Slices have bounds checking and are therefore protected
// against this kind of undefined behavior. This is one reason
// we prefer slices to pointers.
assert(array[3] == 4);
slice[1] += 1;
assert(array[3] == 5);
}
comptime {
// Pointers work at compile-time too, as long as you don't use
// @ptrCast.
var x: i32 = 1;
const ptr = &x;
ptr.* += 1;
x += 1;
assert(ptr.* == 3);
}
test "@ptrToInt and @intToPtr" {
// To convert an integer address into a pointer, use @intToPtr:
const ptr = @intToPtr(*i32, 0xdeadbeef);
// To convert a pointer to an integer, use @ptrToInt:
const addr = @ptrToInt(ptr);
assert(@typeOf(addr) == usize);
assert(addr == 0xdeadbeef);
}
comptime {
// Zig is able to do this at compile-time, as long as
// ptr is never dereferenced.
const ptr = @intToPtr(*i32, 0xdeadbeef);
const addr = @ptrToInt(ptr);
assert(@typeOf(addr) == usize);
assert(addr == 0xdeadbeef);
}
test "volatile" {
// In Zig, loads and stores are assumed to not have side effects.
// If a given load or store should have side effects, such as
// Memory Mapped Input/Output (MMIO), use `volatile`:
const mmio_ptr = @intToPtr(*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 "optional pointers" {
// Pointers cannot be null. If you want a null pointer, use the optional
// prefix `?` to make the pointer type optional.
var ptr: ?*i32 = null;
var x: i32 = 1;
ptr = &x;
assert(ptr.?.* == 1);
// Optional pointers are the same size as normal pointers, because pointer
// value 0 is used as the null value.
assert(@sizeOf(?*i32) == @sizeOf(*i32));
}
test "pointer casting" {
// To convert one pointer type to another, use @ptrCast. This is an unsafe
// operation that Zig cannot protect you against. Use @ptrCast only when other
// conversions are not possible.
const bytes align(@alignOf(u32)) = []u8{ 0x12, 0x12, 0x12, 0x12 };
const u32_ptr = @ptrCast(*const u32, &bytes);
assert(u32_ptr.* == 0x12121212);
// Even this example is contrived - there are better ways to do the above than
// pointer casting. For example, using a slice narrowing cast:
const u32_value = @bytesToSlice(u32, bytes[0..])[0];
assert(u32_value == 0x12121212);
// And even another way, the most straightforward way to do it:
assert(@bitCast(u32, bytes) == 0x12121212);
}
test "pointer child type" {
// pointer types have a `child` field which tells you the type they point to.
assert((*u32).Child == u32);
}
{#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 {#link|implicitly cast|Implicit Casts#} 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 = (*[1]u8)(&foo)[0..]; 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 = @sliceToBytes(array[0..]); assert(foo(bytes) == 0x11111111); } fn foo(bytes: []u8) u32 { const slice4 = bytes[1..5]; const int_slice = @bytesToSlice(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.
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; // 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 = @bytesToSlice(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#} {#header_open|struct Naming#}Since all structs are anonymous, Zig infers the type name based on a few rules.
return
expression, it gets named after
the function it is returning from, with the parameter values serialized.(anonymous struct at file.zig:7:38)
.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
:
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:
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|blocks#}Blocks are used to limit the scope of variable declarations:
{#code_begin|test_err|undeclared identifier#} test "access variable after block scope" { { var x: i32 = 1; } x += 1; } {#code_end#}Blocks are expressions. When labeled, break
can be used
to return a value from the block:
Here, blk
can be any name.
A while loop is used to repeatedly execute an expression until some condition is no longer true.
{#code_begin|test|while#} const assert = @import("std").debug.assert; test "while basic" { var i: usize = 0; while (i < 10) { i += 1; } assert(i == 10); } {#code_end#}
Use break
to exit a while loop early.
Use continue
to jump back to the beginning of the loop.
While loops support a continue expression which is executed when the loop
is continued. The continue
keyword respects this expression.
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.
break
, like return
, accepts a value
parameter. This is the result of the while
expression.
When you break
from a while loop, the else
branch is not
evaluated.
When a while
loop is labeled, it can be referenced from a break
or continue
from within a nested loop:
Just like {#link|if#} expressions, while loops can take an optional as the condition and capture the payload. When {#link|null#} is encountered the loop exits.
When the |x|
syntax is present on a while
expression,
the while condition must have an {#link|Optional Type#}.
The else
branch is allowed on optional iteration. In this case, it will
be executed on the first null value encountered.
Just like {#link|if#} expressions, while loops can take an error union as the condition and capture the payload or the error code. When the condition results in an error code the else branch is evaluated and the loop is finished.
When the else |x|
syntax is present on a while
expression,
the while condition must have an {#link|Error Union Type#}.
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.
{#code_begin|test#} const assert = @import("std").debug.assert; test "inline while loop" { 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#}
It is recommended to use inline
loops only for one of these reasons:
When a for
loop is labeled, it can be referenced from a break
or continue
from within a nested loop:
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.
{#code_begin|test#} const assert = @import("std").debug.assert; test "inline for loop" { const nums = []i32{2, 4, 6}; 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#}
It is recommended to use inline
loops only for one of these reasons:
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
.
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:
Another use case for noreturn
is the exit
function:
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 can be passed directly to a function:
{#code_begin|test#} const Point = struct { x: i32, y: i32, }; fn foo(point: Point) i32 { return point.x + point.y; } const assert = @import("std").debug.assert; test "pass aggregate type by non-copy value to function" { assert(foo(Point{ .x = 1, .y = 2 }) == 3); } {#code_end#}In this case, the value may be passed by reference, or by value, whichever way Zig decides will be faster.
For extern functions, Zig follows the C ABI for passing structs and unions by value.
{#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 {#link|implicitly cast|Implicit Casts#} 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 {#link|implicitly cast|Implicit Casts#} to error!u64
.
What it looks like to use this function varies depending on what you're trying to do. One of the following:
If you want to provide a default value, you can use the catch
binary operator:
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:
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 {#link|if#} and {#link|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#} {#header_open|errdefer#}
The other component to error handling is defer statements.
In addition to an unconditional {#link|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() orelse 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.
{#header_close#}A couple of other tidbits about error handling:
catch unreachable
and
get the added benefit of crashing in Debug and ReleaseSafe modes if your assumption was wrong.
An error union is created with the !
binary operator.
You can use compile-time reflection to access the child type of an error union:
Use the ||
operator to merge two error sets together. The resulting
error set contains the errors of both error sets. Doc comments from the left-hand
side override doc comments from the right-hand side. In this example, the doc
comments for C.PathNotFound
is A doc comment
.
This is especially useful for functions which return different error sets depending
on {#link|comptime#} branches. For example, the Zig standard library uses
LinuxFileOpenError || WindowsFileOpenError
for the error set of opening
files.
Because many functions in Zig return a possible error, Zig supports inferring the error set. To infer the error set for a function, use this syntax:
{#code_begin|test#} // With an inferred error set pub fn add_inferred(comptime T: type, a: T, b: T) !T { var answer: T = undefined; return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; } // With an explicit error set pub fn add_explicit(comptime T: type, a: T, b: T) Error!T { var answer: T = undefined; return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; } const Error = error { Overflow, }; const std = @import("std"); test "inferred error set" { if (add_inferred(u8, 255, 1)) |_| unreachable else |err| switch (err) { error.Overflow => {}, // ok } } {#code_end#}When a function has an inferred error set, that function becomes generic and thus it becomes trickier to do certain things with it, such as obtain a function pointer, or have an error set that is consistent across different build targets. Additionally, inferred error sets are incompatible with recursion.
In these situations, it is recommended to use an explicit error set. You can generally start with an empty error set and let compile errors guide you toward completing the set.
These limitations may be overcome in a future version of Zig.
{#header_close#} {#header_close#} {#header_open|Error Return Traces#}Error Return Traces show all the points in the code that an error was returned to the calling function. This makes it practical to use {#link|try#} everywhere and then still be able to know what happened if an error ends up bubbling all the way out of your application.
{#code_begin|exe_err#} pub fn main() !void { try foo(12); } fn foo(x: i32) !void { if (x >= 5) { try bar(); } else { try bang2(); } } fn bar() !void { if (baz()) { try quux(); } else |err| switch (err) { error.FileNotFound => try hello(), else => try another(), } } fn baz() !void { try bang1(); } fn quux() !void { try bang2(); } fn hello() !void { try bang2(); } fn another() !void { try bang1(); } fn bang1() !void { return error.FileNotFound; } fn bang2() !void { return error.PermissionDenied; } {#code_end#}Look closely at this example. This is no stack trace.
You can see that the final error bubbled up was PermissionDenied
,
but the original error that started this whole thing was FileNotFound
. In the bar
function, the code handles the original error code,
and then returns another one, from the switch statement. Error Return Traces make this clear, whereas a stack trace would look like this:
Here, the stack trace does not explain how the control
flow in bar
got to the hello()
call.
One would have to open a debugger or further instrument the application
in order to find out. The error return trace, on the other hand,
shows exactly how the error bubbled up.
This debugging feature makes it easier to iterate quickly on code that robustly handles all error conditions. This means that Zig developers will naturally find themselves writing correct, robust code in order to increase their development pace.
Error Return Traces are enabled by default in {#link|Debug#} and {#link|ReleaseSafe#} builds and disabled by default in {#link|ReleaseFast#} and {#link|ReleaseSmall#} builds.
There are a few ways to activate this error return tracing feature:
catch unreachable
and you have not overridden the default panic handlerstd.debug.dumpStackTrace
to print it. This function returns comptime-known {#link|null#} when building without error return tracing support.To analyze performance cost, there are two cases:
For the case when no errors are returned, the cost is a single memory write operation, only in the first non-failable function in the call graph that calls a failable function, i.e. when a function returning void
calls a function returning error
.
This is to initialize this struct in the stack memory:
Here, N is the maximum function call depth as determined by call graph analysis. Recursion is ignored and counts for 2.
A pointer to StackTrace
is passed as a secret parameter to every function that can return an error, but it's always the first parameter, so it can likely sit in a register and stay there.
That's it for the path when no errors occur. It's practically free in terms of performance.
When generating the code for a function that returns an error, just before the return
statement (only for the return
statements that return errors), Zig generates a call to this function:
The cost is 2 math operations plus some memory reads and writes. The memory accessed is constrained and should remain cached for the duration of the error return bubbling.
As for code size cost, 1 function call before a return statement is no big deal. Even so,
I have a plan to make the call to
__zig_return_error
a tail call, which brings the code size cost down to actually zero. What is a return statement in code without error return tracing can become a jump instruction in code with error return tracing.
One area that Zig provides safety without compromising efficiency or readability is with the optional type.
The question mark symbolizes the optional type. You can convert a type to an optional type by putting a question mark in front of it, like this:
{#code_begin|syntax#} // normal integer const normal_int: i32 = 1234; // optional integer const optional_int: ?i32 = 5678; {#code_end#}
Now the variable optional_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 an optional pointer. This secretly compiles down to a normal pointer, since we know we can use 0 as the null value for the optional type. But the compiler can check your work and make sure you don't assign null to something that can't be null.
Typically the downside of not having null is that it makes the code more verbose to write. But, let's compare some equivalent C code and Zig code.
Task: call malloc, if the result is null, return null.
C code
// malloc prototype included for 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) orelse 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 orelse
keyword
unwrapped the optional 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(optional_foo: ?*Foo) void { // do some stuff if (optional_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 an optional pointer, it is a pointer, which
cannot be null.
One benefit to this is that functions which take pointers as arguments can
be annotated with the "nonnull" attribute - __attribute__((nonnull))
in
GCC.
The optimizer can sometimes make better decisions knowing that pointer arguments
cannot be null.
An optional is created by putting ?
in front of a type. You can use compile-time
reflection to access the child type of an optional:
Just like {#link|undefined#}, null
has its own type, and the only way to use it is to
cast it to a different type:
A type cast converts a value of one type to another. Zig has {#link|Implicit Casts#} for conversions that are known to be completely safe and unambiguous, and {#link|Explicit Casts#} for conversions that one would not want to happen on accident. There is also a third kind of type conversion called {#link|Peer Type Resolution#} for the case when a result type must be decided given multiple operand types.
{#header_open|Implicit Casts#}An implicit cast occurs when one type is expected, but different type is provided:
{#code_begin|test#} test "implicit cast - variable declaration" { var a: u8 = 1; var b: u16 = a; } test "implicit cast - function call" { var a: u8 = 1; foo(a); } fn foo(b: u16) void {} test "implicit cast - invoke a type as a function" { var a: u8 = 1; var b = u16(a); } {#code_end#}Implicit casts are only allowed when it is completely unambiguous how to get from one type to another, and the transformation is guaranteed to be safe.
{#header_open|Implicit Cast: Stricter Qualification#}Values which have the same representation at runtime can be cast to increase the strictness of the qualifiers, no matter how nested the qualifiers are:
const
- non-const to const is allowedvolatile
- non-volatile to volatile is allowedalign
- bigger to smaller alignment is allowed These casts are no-ops at runtime since the value representation does not change.
{#code_begin|test#} test "implicit cast - const qualification" { var a: i32 = 1; var b: *i32 = &a; foo(b); } fn foo(a: *const i32) void {} {#code_end#}In addition, pointers implicitly cast to const optional pointers:
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; const mem = std.mem; test "cast *[1][*]const u8 to [*]const ?[*]const u8" { const window_name = [1][*]const u8{c"window name"}; const x: [*]const ?[*]const u8 = &window_name; assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name")); } {#code_end#} {#header_close#} {#header_open|Implicit Cast: Integer and Float Widening#}{#link|Integers#} implicitly cast to integer types which can represent every value of the old type, and likewise {#link|Floats#} implicitly cast to float types which can represent every value of the old type.
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; const mem = std.mem; test "integer widening" { var a: u8 = 250; var b: u16 = a; var c: u32 = b; var d: u64 = c; var e: u64 = d; var f: u128 = e; assert(f == a); } test "implicit unsigned integer to signed integer" { var a: u8 = 250; var b: i16 = a; assert(b == 250); } test "float widening" { var a: f16 = 12.34; var b: f32 = a; var c: f64 = b; var d: f128 = c; assert(d == a); } {#code_end#} {#header_close#} {#header_open|Implicit Cast: Arrays#}TODO: [N]T to []const T
TODO: *const [N]T to []const T
TODO: [N]T to *const []const T
TODO: [N]T to ?[]const T
TODO: *[N]T to []T
TODO: *[N]T to [*]T
TODO: *[N]T to ?[*]T
TODO: *T to *[1]T
TODO: [N]T to E![]const T
{#header_close#} {#header_open|Implicit Cast: Optionals#}TODO: T to ?T
TODO: T to E!?T
TODO: null to ?T
{#header_close#} {#header_open|Implicit Cast: T to E!T#}TODO
{#header_close#} {#header_open|Implicit Cast: E to E!T#}TODO
{#header_close#} {#header_open|Implicit Cast: comptime_int to *const integer#}TODO
{#header_close#} {#header_open|Implicit Cast: comptime_float to *const float#}TODO
{#header_close#} {#header_open|Implicit Cast: compile-time known numbers#}TODO
{#header_close#} {#header_open|Implicit Cast: union to enum#}TODO
{#header_close#} {#header_open|Implicit Cast: enum to union#}TODO
{#header_close#} {#header_open|Implicit Cast: T to *T when @sizeOf(T) == 0#}TODO
{#header_close#} {#header_open|Implicit Cast: undefined#}TODO
{#header_close#} {#header_open|Implicit Cast: T to *const T#}TODO
{#header_close#} {#header_close#} {#header_open|Explicit Casts#}Explicit casts are performed via {#link|Builtin Functions#}. Some explicit casts are safe; some are not. Some explicit casts perform language-level assertions; some do not. Some explicit casts are no-ops at runtime; some are not.
Peer Type Resolution occurs in these places:
This kind of type resolution chooses a type that all peer types can implicitly cast into. Here are some examples:
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; const mem = std.mem; test "peer resolve int widening" { var a: i8 = 12; var b: i16 = 34; var c = a + b; assert(c == 46); assert(@typeOf(c) == i16); } test "peer resolve arrays of different size to const slice" { assert(mem.eql(u8, boolToStr(true), "true")); assert(mem.eql(u8, boolToStr(false), "false")); comptime assert(mem.eql(u8, boolToStr(true), "true")); comptime assert(mem.eql(u8, boolToStr(false), "false")); } fn boolToStr(b: bool) []const u8 { return if (b) "true" else "false"; } test "peer resolve array and const slice" { testPeerResolveArrayConstSlice(true); comptime testPeerResolveArrayConstSlice(true); } fn testPeerResolveArrayConstSlice(b: bool) void { const value1 = if (b) "aoeu" else ([]const u8)("zz"); const value2 = if (b) ([]const u8)("zz") else "aoeu"; assert(mem.eql(u8, value1, "aoeu")); assert(mem.eql(u8, value2, "zz")); } test "peer type resolution: ?T and T" { assert(peerTypeTAndOptionalT(true, false).? == 0); assert(peerTypeTAndOptionalT(false, false).? == 3); comptime { assert(peerTypeTAndOptionalT(true, false).? == 0); assert(peerTypeTAndOptionalT(false, false).? == 3); } } fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { if (c) { return if (b) null else usize(0); } return usize(3); } test "peer type resolution: [0]u8 and []const u8" { assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); comptime { assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); } } fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 { if (a) { return []const u8{}; } return slice[0..1]; } test "peer type resolution: [0]u8, []const u8, and error![]u8" { { var data = "hi"; const slice = data[0..]; assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); } comptime { var data = "hi"; const slice = data[0..]; assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); } } fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) error![]u8 { if (a) { return []u8{}; } return slice[0..1]; } {#code_end#} {#header_close#} {#header_close#} {#header_open|void#}
void
represents a type that has no value. Code that makes use of void values is
not included in the final generated code:
When this turns into LLVM IR, there is no code generated in the body of entry
,
even in debug mode. For example, on x86_64:
0000000000000010 <entry>:
10: 55 push %rbp
11: 48 89 e5 mov %rsp,%rbp
14: 5d pop %rbp
15: c3 retq
These assembly instructions do not have any code associated with the void values - they only perform the function call prologue and epilog.
void
can be useful for instantiating generic types. For example, given a
Map(Key, Value)
, one can pass void
for the Value
type to make it into a Set
:
Note that this is different than using a dummy value for the hash map value.
By using void
as the type of the value, the hash map entry type has no value field, and
thus the hash map takes up less space. Further, all the code that deals with storing and loading the
value is deleted, as seen above.
void
is distinct from c_void
, which is defined like this:
pub const c_void = @OpaqueType();
.
void
has a known size of 0 bytes, and c_void
has an unknown, but non-zero, size.
Expressions of type void
are the only ones whose value can be ignored. For example:
However, if the expression has type void
:
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:
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:
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:
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.
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:
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:
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:
comptime
variables.if
, while
, for
, and switch
expressions are evaluated at compile-time, or emit a compile error if this is not possible.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?
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:
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.
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)
.
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.
Putting all of this together, let's see how printf
works in Zig.
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:
And now, what happens if we give too many arguments to printf
?
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
:
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.
{#header_close#} {#see_also|inline while|inline for#} {#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|Coroutines#}A coroutine is a generalization of a function.
When you call a function, it creates a stack frame, and then the function runs until it reaches a return statement, and then the stack frame is destroyed. At the callsite, the next line of code does not run until the function returns.
A coroutine is like a function, but it can be suspended and resumed any number of times, and then it must be explicitly destroyed. When a coroutine suspends, it returns to the resumer.
{#header_open|Minimal Coroutine Example#}
Declare a coroutine with the async
keyword.
The expression in angle brackets must evaluate to a struct
which has these fields:
allocFn: fn (self: *Allocator, byte_count: usize, alignment: u29) Error![]u8
- where Error
can be any error set.freeFn: fn (self: *Allocator, old_mem: []u8) void
You may notice that this corresponds to the std.mem.Allocator
interface.
This makes it convenient to integrate with existing allocators. Note, however,
that the language feature does not depend on the standard library, and any struct which
has these fields is allowed.
Omitting the angle bracket expression when defining an async function makes the function generic. Zig will infer the allocator type when the async function is called.
Call a coroutine with the async
keyword. Here, the expression in angle brackets
is a pointer to the allocator struct that the coroutine expects.
The result of an async function call is a promise->T
type, where T
is the return type of the async function. Once a promise has been created, it must be
consumed, either with cancel
or await
:
Async functions start executing when created, so in the following example, the entire async function completes before it is canceled:
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; var x: i32 = 1; test "create a coroutine and cancel it" { const p = try asyncAt any point, an async function may suspend itself. This causes control flow to return to the caller or resumer. The following code demonstrates where control flow goes:
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; test "coroutine suspend, resume, cancel" { seq('a'); const p = try asyncWhen an async function suspends itself, it must be sure that it will be resumed or canceled somehow, for example by registering its promise handle in an event loop. Use a suspend capture block to gain access to the promise:
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; test "coroutine suspend with block" { const p = try async
Every suspend point in an async function represents a point at which the coroutine
could be destroyed. If that happens, defer
expressions that are in
scope are run, as well as errdefer
expressions.
{#link|Await#} counts as a suspend point.
{#header_open|Resuming from Suspend Blocks#}
Upon entering a suspend
block, the coroutine is already considered
suspended, and can be resumed. For example, if you started another kernel thread,
and had that thread call resume
on the promise handle provided by the
suspend
block, the new thread would begin executing after the suspend
block, while the old thread continued executing the suspend block.
However, the coroutine can be directly resumed from the suspend block, in which case it never returns to its resumer and continues executing.
{#code_begin|test#} const std = @import("std"); const assert = std.debug.assert; test "resume from suspend" { var buf: [500]u8 = undefined; var a = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; var my_result: i32 = 1; const p = try async testResumeFromSuspend(&my_result); cancel p; std.debug.assert(my_result == 2); } async fn testResumeFromSuspend(my_result: *i32) void { suspend { resume @handle(); } my_result.* += 1; suspend; my_result.* += 1; } {#code_end#}This is guaranteed to be a tail call, and therefore will not cause a new stack frame.
{#header_close#} {#header_close#} {#header_open|Await#}
The await
keyword is used to coordinate with an async function's
return
statement.
await
is valid only in an async
function, and it takes
as an operand a promise handle.
If the async function associated with the promise handle has already returned,
then await
destroys the target async function, and gives the return value.
Otherwise, await
suspends the current async function, registering its
promise handle with the target coroutine. It becomes the target coroutine's responsibility
to have ensured that it will be resumed or destroyed. When the target coroutine reaches
its return statement, it gives the return value to the awaiter, destroys itself, and then
resumes the awaiter.
A promise handle must be consumed exactly once after it is created, either by cancel
or await
.
await
counts as a suspend point, and therefore at every await
,
a coroutine can be potentially destroyed, which would run defer
and errdefer
expressions.
In general, suspend
is lower level than await
. Most application
code will use only async
and await
, but event loop
implementations will make use of suspend
internally.
There are a few issues with coroutines that are considered unresolved. Best be aware of them, as the situation is likely to change before 1.0.0:
Builtin functions 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.
@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
.
@ArgType(comptime T: type, comptime n: usize) type
This builtin function takes a function type and returns the type of the parameter at index n
.
T
must be a function type.
Note: This function is deprecated. Use {#link|@typeInfo#} instead.
{#header_close#} {#header_open|@atomicLoad#}@atomicLoad(comptime T: type, ptr: *const T, comptime ordering: builtin.AtomicOrder) T
This builtin function atomically dereferences a pointer and returns the value.
T
must be a pointer type, a bool
,
or an integer whose bit count meets these requirements:
TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe we can remove this restriction
{#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:
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:
f32
to u32
bitsi32
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.
@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|@boolToInt#}@boolToInt(value: bool) u1
Converts true
to u1(1)
and false
to
u1(0)
.
If the value is known at compile-time, the return type is comptime_int
instead of u1
.
@bytesToSlice(comptime Element: type, bytes: []u8) []Element
Converts a slice of bytes or array of bytes into a slice of Element
.
The resulting slice has the same {#link|pointer|Pointers#} properties as the parameter.
Attempting to convert a number of bytes with a length that does not evenly divide into a slice of elements results in safety-protected {#link|Undefined Behavior#}.
{#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:
#define CONNECTION_COUNT
@cInclude(comptime path: []u8)
This function can only occur inside @cImport
.
This appends #include <$path>\n
to the c_import
temporary buffer.
@cUndef(comptime name: []u8)
This function can only occur inside @cImport
.
This appends #undef $name
to the @cImport
temporary buffer.
@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
.
@cmpxchgStrong(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T
This function performs a strong atomic compare exchange operation. It's the equivalent of this code, except atomic:
{#code_begin|syntax#} fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_value: T) ?T { const old_value = ptr.*; if (old_value == expected_value) { ptr.* = new_value; return null; } else { return old_value; } } {#code_end#}If you are using cmpxchg in a loop, {#link|@cmpxchgWeak#} is the better choice, because it can be implemented more efficiently in machine instructions.
AtomicOrder
can be found with @import("builtin").AtomicOrder
.
@typeOf(ptr).alignment
must be >= @sizeOf(T).
@cmpxchgWeak(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T
This function performs a weak atomic compare exchange operation. It's the equivalent of this code, except atomic:
{#code_begin|syntax#} fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_value: T) ?T { const old_value = ptr.*; if (old_value == expected_value and usuallyTrueButSometimesFalse()) { ptr.* = new_value; return null; } else { return old_value; } } {#code_end#}
If you are using cmpxchg in a loop, the sporadic failure will be no problem, and cmpxchgWeak
is the better choice, because it can be implemented more efficiently in machine instructions.
However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}.
AtomicOrder
can be found with @import("builtin").AtomicOrder
.
@typeOf(ptr).alignment
must be >= @sizeOf(T).
@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.
@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:
@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
.
@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
.
@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
.
@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
.
@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
.
@enumToInt(enum_value: var) var
Converts an enumeration value into its integer tag type.
{#see_also|@intToEnum#} {#header_close#} {#header_open|@errSetCast#}@errSetCast(comptime T: DestType, value: var) DestType
Converts an error value from one error set to another error set. Attempting to convert an error which is not in the destination error set results in safety-protected {#link|Undefined Behavior#}.
{#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.
@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|@errorToInt#}@errorToInt(err: var) @IntType(false, @sizeOf(error) * 8)
Supports the following types:
E!void
Converts an error to the integer representation of an error.
It is generally recommended to avoid this cast, as the integer representation of an error is not stable across source code changes.
{#see_also|@intToError#} {#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|@fence#}@fence(order: AtomicOrder)
The fence
function is used to introduce happens-before edges between operations.
AtomicOrder
can be found with @import("builtin").AtomicOrder
.
@field(lhs: var, comptime field_name: []const u8) (field)
Preforms field access equivalent to lhs.->field_name-<
.
@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|@floatCast#}@floatCast(comptime DestType: type, value: var) DestType
Convert from one float type to another. This cast is safe, but may cause the numeric value to lose precision.
{#header_close#} {#header_open|@floatToInt#}@floatToInt(comptime DestType: type, float: var) DestType
Converts the integer part of a floating point number to the destination type.
If the integer part of the floating point number cannot fit in the destination type, it invokes safety-checked {#link|Undefined Behavior#}.
{#see_also|@intToFloat#} {#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|@handle#}@handle()
This function returns a promise->T
type, where T
is the return type of the async function in scope.
This function is only valid within an async 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@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.
@intCast(comptime DestType: type, int: var) DestType
Converts an integer to another integer while keeping the same numerical value. Attempting to convert a number which is out of range of the destination type results in safety-protected {#link|Undefined Behavior#}.
{#header_close#} {#header_open|@intToEnum#}@intToEnum(comptime DestType: type, int_value: @TagType(DestType)) DestType
Converts an integer into an {#link|enum#} value.
Attempting to convert an integer which represents no value in the chosen enum type invokes safety-checked {#link|Undefined Behavior#}.
{#see_also|@enumToInt#} {#header_close#} {#header_open|@intToError#}@intToError(value: @IntType(false, @sizeOf(error) * 8)) error
Converts from the integer representation of an error into the global error set type.
It is generally recommended to avoid this cast, as the integer representation of an error is not stable across source code changes.
Attempting to convert an integer that does not correspond to any error results in safety-protected {#link|Undefined Behavior#}.
{#see_also|@errorToInt#} {#header_close#} {#header_open|@intToFloat#}@intToFloat(comptime DestType: type, int: var) DestType
Converts an integer to the closest floating point representation. To convert the other way, use {#link|@floatToInt#}. This cast is always safe.
{#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: u32) 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
.
@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
.
@newStackCall(new_stack: []u8, function: var, args: ...) var
This calls a function, in the same way that invoking an expression with parentheses does. However,
instead of using the same stack as the caller, the function uses the stack provided in the new_stack
parameter.
@noInlineCall(function: var, args: ...) var
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 "noinline function call" { assert(@noInlineCall(add, 3, 9) == 12); } fn add(a: i32, b: i32) i32 { return a + b; } {#code_end#}
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.
@byteOffsetOf(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|@bitOffsetOf#}@bitOffsetOf(comptime T: type, comptime field_name: [] const u8) (number literal)
This function returns the bit 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:
@popCount(integer: var) var
Counts the number of bits set in an integer.
If integer
is known at {#link|comptime#}, the return type is comptime_int
.
Otherwise, the return type is an unsigned integer with the minimum number
of bits that can represent the bit count of the integer type.
@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
.
@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.
@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
:
@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
- Floating point operations may do all of the following:
-ffast-math
in GCC.
Strict
(default) - Floating point operations follow strict IEEE compliance.
@setGlobalLinkage(global_variable_name, comptime linkage: GlobalLinkage)
GlobalLinkage
can be found with @import("builtin").GlobalLinkage
.
@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.
@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.
@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.
@sizeOf(comptime T: type) comptime_int
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|@sliceToBytes#}@sliceToBytes(value: var) []u8
Converts a slice or array to a slice of u8
. The resulting slice has the same
{#link|pointer|Pointers#} properties as the parameter.
@sqrt(comptime T: type, value: T) T
Performs the square root of a floating point number. Uses a dedicated hardware instruction when available. Currently only supports f32 and f64 at runtime. f128 at runtime is TODO.
This is a low-level intrinsic. Most code can use std.math.sqrt
instead.
@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
.
@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|@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, ComptimeFloat, ComptimeInt, Undefined, Null, Optional, ErrorUnion, Error, Enum, Union, Fn, Namespace, Block, BoundFn, ArgTuple, Opaque, }; {#code_end#} {#header_close#} {#header_open|@typeInfo#}@typeInfo(comptime T: type) @import("builtin").TypeInfo
Returns information on the type. Returns a value of the following union:
{#code_begin|syntax#} pub const TypeInfo = union(TypeId) { Type: void, Void: void, Bool: void, NoReturn: void, Int: Int, Float: Float, Pointer: Pointer, Array: Array, Struct: Struct, ComptimeFloat: void, ComptimeInt: void, Undefined: void, Null: void, Optional: Optional, ErrorUnion: ErrorUnion, ErrorSet: ErrorSet, Enum: Enum, Union: Union, Fn: Fn, Namespace: void, Block: void, BoundFn: Fn, ArgTuple: void, Opaque: void, Promise: Promise, pub const Int = struct { is_signed: bool, bits: u8, }; pub const Float = struct { bits: u8, }; pub const Pointer = struct { size: Size, is_const: bool, is_volatile: bool, alignment: u29, child: type, pub const Size = enum { One, Many, Slice, }; }; pub const Array = struct { len: usize, child: type, }; pub const ContainerLayout = enum { Auto, Extern, Packed, }; pub const StructField = struct { name: []const u8, offset: ?usize, field_type: type, }; pub const Struct = struct { layout: ContainerLayout, fields: []StructField, defs: []Definition, }; pub const Optional = struct { child: type, }; pub const ErrorUnion = struct { error_set: type, payload: type, }; pub const Error = struct { name: []const u8, value: usize, }; pub const ErrorSet = struct { errors: []Error, }; pub const EnumField = struct { name: []const u8, value: usize, }; pub const Enum = struct { layout: ContainerLayout, tag_type: type, fields: []EnumField, defs: []Definition, }; pub const UnionField = struct { name: []const u8, enum_field: ?EnumField, field_type: type, }; pub const Union = struct { layout: ContainerLayout, tag_type: ?type, fields: []UnionField, defs: []Definition, }; pub const CallingConvention = enum { Unspecified, C, Cold, Naked, Stdcall, Async, }; pub const FnArg = struct { is_generic: bool, is_noalias: bool, arg_type: ?type, }; pub const Fn = struct { calling_convention: CallingConvention, is_generic: bool, is_var_args: bool, return_type: ?type, async_allocator_type: ?type, args: []FnArg, }; pub const Promise = struct { child: ?type, }; pub const Definition = struct { name: []const u8, is_pub: bool, data: Data, pub const Data = union(enum) { Type: type, Var: type, Fn: FnDef, pub const FnDef = struct { fn_type: type, inline_type: Inline, calling_convention: CallingConvention, is_var_args: bool, is_extern: bool, is_export: bool, lib_name: ?[]const u8, return_type: type, arg_names: [][] const u8, pub const Inline = enum { Auto, Always, Never, }; }; }; }; }; {#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 four build modes:
To add standard build options to a build.zig
file:
This causes these options to be available:
-Drelease-safe=[bool] optimizations on and safety on
-Drelease-fast=[bool] optimizations on and safety off
-Drelease-small=[bool] size optimizations on and safety off
{#header_open|Debug#}
$ zig build-exe example.zig
$ zig build-exe example.zig --release-fast
$ zig build-exe example.zig --release-safe
$ zig build-exe example.zig --release-small
Zig has many instances of undefined behavior. If undefined behavior is detected at compile-time, Zig emits a compile error and refuses to continue. Most undefined behavior that cannot be detected at compile-time can be detected at runtime. In these cases, Zig has safety checks. Safety checks can be disabled on a per-block basis with {#link|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:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { std.debug.assert(false); } {#code_end#} {#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:
{#code_begin|exe_err#} pub fn main() void { var x = foo("hello"); } fn foo(x: []const u8) u8 { return x[5]; } {#code_end#} {#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 = @intCast(u32, value); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var value: i32 = -1; var unsigned = @intCast(u32, value); std.debug.warn("value: {}\n", unsigned); } {#code_end#}To obtain the maximum value of an unsigned integer, use {#link|@maxValue#}.
{#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 = @intCast(u8, spartan_count); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var spartan_count: u16 = 300; const byte = @intCast(u8, spartan_count); std.debug.warn("value: {}\n", byte); } {#code_end#}To truncate bits, use {#link|@truncate#}.
{#header_close#} {#header_open|Integer Overflow#} {#header_open|Default Operations#}The following operators can cause integer overflow:
+
(addition)-
(subtraction)-
(negation)*
(multiplication)/
(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:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var byte: u8 = 255; byte += 1; std.debug.warn("value: {}\n", byte); } {#code_end#} {#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:
Example of {#link|@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)At compile-time:
{#code_begin|test_err|operation caused overflow#} comptime { const x = @shlExact(u8(0b01010101), 2); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var x: u8 = 0b01010101; var y = @shlExact(x, 2); std.debug.warn("value: {}\n", y); } {#code_end#} {#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:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var x: u8 = 0b10101010; var y = @shrExact(x, 2); std.debug.warn("value: {}\n", y); } {#code_end#} {#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:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var a: u32 = 1; var b: u32 = 0; var c = a / b; std.debug.warn("value: {}\n", c); } {#code_end#} {#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:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var a: u32 = 10; var b: u32 = 0; var c = a % b; std.debug.warn("value: {}\n", c); } {#code_end#} {#header_close#} {#header_open|Exact Division Remainder#}At compile-time:
{#code_begin|test_err|exact division had a remainder#} comptime { const a: u32 = 10; const b: u32 = 3; const c = @divExact(a, b); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var a: u32 = 10; var b: u32 = 3; var c = @divExact(a, b); std.debug.warn("value: {}\n", c); } {#code_end#} {#header_close#} {#header_open|Slice Widen Remainder#}At compile-time:
{#code_begin|test_err|unable to convert#} comptime { var bytes = [5]u8{ 1, 2, 3, 4, 5 }; var slice = @bytesToSlice(u32, bytes); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var bytes = [5]u8{ 1, 2, 3, 4, 5 }; var slice = @bytesToSlice(u32, bytes[0..]); std.debug.warn("value: {}\n", slice[0]); } {#code_end#} {#header_close#} {#header_open|Attempt to Unwrap Null#}At compile-time:
{#code_begin|test_err|unable to unwrap null#} comptime { const optional_number: ?i32 = null; const number = optional_number.?; } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var optional_number: ?i32 = null; var number = optional_number.?; std.debug.warn("value: {}\n", number); } {#code_end#}One way to avoid this crash is to test for null instead of assuming non-null, with
the if
expression:
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:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { const number = getNumberOrFail() catch unreachable; std.debug.warn("value: {}\n", number); } fn getNumberOrFail() !i32 { return error.UnableToReturnNumber; } {#code_end#}One way to avoid this crash is to test for an error instead of assuming a successful result, with
the if
expression:
At compile-time:
{#code_begin|test_err|integer value 11 represents no error#} comptime { const err = error.AnError; const number = @errorToInt(err) + 10; const invalid_err = @intToError(number); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); pub fn main() void { var err = error.AnError; var number = @errorToInt(err) + 500; var invalid_err = @intToError(number); std.debug.warn("value: {}\n", number); } {#code_end#} {#header_close#} {#header_open|Invalid Enum Cast#}At compile-time:
{#code_begin|test_err|has no tag matching integer value 3#} const Foo = enum { A, B, C, }; comptime { const a: u2 = 3; const b = @intToEnum(Foo, a); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); const Foo = enum { A, B, C, }; pub fn main() void { var a: u2 = 3; var b = @intToEnum(Foo, a); std.debug.warn("value: {}\n", @tagName(b)); } {#code_end#} {#header_close#} {#header_open|Invalid Error Set Cast#}At compile-time:
{#code_begin|test_err|error.B not a member of error set 'Set2'#} const Set1 = error{ A, B, }; const Set2 = error{ A, C, }; comptime { _ = @errSetCast(Set2, Set1.B); } {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); const Set1 = error{ A, B, }; const Set2 = error{ A, C, }; pub fn main() void { foo(Set1.B); } fn foo(set1: Set1) void { const x = @errSetCast(Set2, set1); std.debug.warn("value: {}\n", x); } {#code_end#} {#header_close#} {#header_open|Incorrect Pointer Alignment#}At compile-time:
{#code_begin|test_err|pointer address 0x1 is not aligned to 4 bytes#} comptime { const ptr = @intToPtr(*i32, 0x1); const aligned = @alignCast(4, ptr); } {#code_end#}At runtime:
{#code_begin|exe_err#} pub fn main() !void { var array align(4) = []u32{ 0x11111111, 0x11111111 }; const bytes = @sliceToBytes(array[0..]); if (foo(bytes) != 0x11111111) return error.Wrong; } fn foo(bytes: []u8) u32 { const slice4 = bytes[1..5]; const int_slice = @bytesToSlice(u32, @alignCast(4, slice4)); return int_slice[0]; } {#code_end#} {#header_close#} {#header_open|Wrong Union Field Access#}At compile-time:
{#code_begin|test_err|accessing union field 'float' while field 'int' is set#} comptime { var f = Foo{ .int = 42 }; f.float = 12.34; } const Foo = union { float: f32, int: u32, }; {#code_end#}At runtime:
{#code_begin|exe_err#} const std = @import("std"); const Foo = union { float: f32, int: u32, }; pub fn main() void { var f = Foo{ .int = 42 }; bar(&f); } fn bar(f: *Foo) void { f.float = 12.34; std.debug.warn("value: {}\n", f.float); } {#code_end#}
This safety is not available for extern
or packed
unions.
To change the active field of a union, assign the entire union, like this:
{#code_begin|exe#} const std = @import("std"); const Foo = union { float: f32, int: u32, }; pub fn main() void { var f = Foo{ .int = 42 }; bar(&f); } fn bar(f: *Foo) void { f.* = Foo{ .float = 12.34 }; std.debug.warn("value: {}\n", f.float); } {#code_end#}To change the active field of a union when a meaningful value for the field is not known, use {#link|undefined#}, like this:
{#code_begin|exe#} const std = @import("std"); const Foo = union { float: f32, int: u32, }; pub fn main() void { var f = Foo{ .int = 42 }; f = Foo{ .float = undefined }; bar(&f); std.debug.warn("value: {}\n", f.float); } fn bar(f: *Foo) void { f.float = 12.34; } {#code_end#} {#header_close#} {#header_open|Out of Bounds Float To Integer Cast#}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.
Example of what is imported with @import("builtin")
:
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
The @cImport
builtin function can be used
to directly import symbols from .h files:
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:
One of the primary use cases for Zig is exporting a library with the C ABI for other programming languages
to call into. The export
keyword in front of functions, variables, and types causes them to
be part of the library API:
mathtest.zig
{#code_begin|syntax#} export fn add(a: i32, b: i32) i32 { return a + b; } {#code_end#}To make a shared library:
$ zig build-lib mathtest.zig
To make a static library:
$ zig build-lib mathtest.zig --static
Here is an example with the {#link|Zig Build System#}:
test.c
// This header is generated by zig from mathtest.zig
#include "mathtest.h"
#include <assert.h>
int main(int argc, char **argv) {
assert(add(42, 1337) == 1379);
return 0;
}
build.zig
{#code_begin|syntax#} const Builder = @import("std").build.Builder; pub fn build(b: *Builder) void { const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0)); const exe = b.addCExecutable("test"); exe.addCompileFlags([][]const u8{"-std=c99"}); exe.addSourceFile("test.c"); exe.linkLibrary(lib); b.default_step.dependOn(&exe.step); const run_cmd = b.addCommand(".", b.env_map, [][]const u8{exe.getOutputPath()}); run_cmd.step.dependOn(&exe.step); const test_step = b.step("test", "Test the program"); test_step.dependOn(&run_cmd.step); } {#code_end#}terminal
$ zig build
$ ./test
$ echo $?
0
{#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#}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.
Not all standard library code requires operating system abstractions, however,
so things such as generic data structures work an all above platforms.
The current list of targets supported by the Zig Standard Library is:
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#}
Roughly speaking: camelCaseFunctionName
, TitleCaseTypeName
,
snake_case_variable_name
. More precisely:
x
is a struct
(or an alias of a struct
),
then x
should be TitleCase
.
x
otherwise identifies a type, x
should have snake_case
.
x
is callable, and x
's return type is type
, then x
should be TitleCase
.
x
is otherwise callable, then x
should be camelCase
.
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.
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:
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 (UnwrapOptional | UnwrapError) | BoolOrExpression
UnwrapOptional = "orelse" 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( 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#}