Documentation - The Zig Programming Language
Documentation - The Zig Programming Language
Zig Version
0.1.1 | 0.2.0 | 0.3.0 | 0.4.0 | 0.5.0 | 0.6.0 | 0.7.1 | 0.8.1 | 0.9.1 | 0.10.1 | 0.11.0 | 0.12.0 | master |
Table of Contents
• Introduction
• Zig Standard Library
• Hello World
• Comments
◦ Doc Comments
◦ Top-Level Doc Comments
• Values
◦ Primitive Types
◦ Primitive Values
◦ String Literals and Unicode Code Point Literals
▪ Escape Sequences
▪ Multiline String Literals
◦ Assignment
▪ undefined
• Zig Test
◦ Test Declarations
▪ Doctests
◦ Test Failure
◦ Skip Tests
◦ Report Memory Leaks
◦ Detecting Test Build
◦ Test Output and Logging
◦ The Testing Namespace
◦ Test Tool Documentation
• Variables
◦ Identifiers
◦ Container Level Variables
◦ Static Local Variables
◦ Thread Local Variables
◦ Local Variables
• Integers
◦ Integer Literals
◦ Runtime Integer Values
• Floats
◦ Float Literals
◦ Floating Point Operations
• Operators
◦ Table of Operators
◦ Precedence
• Arrays
◦ Multidimensional Arrays
◦ Sentinel-Terminated Arrays
• Vectors
• Pointers
◦ volatile
◦ Alignment
◦ allowzero
◦ Sentinel-Terminated Pointers
• Slices
◦ Sentinel-Terminated Slices
• struct
◦ Default Field Values
◦ extern struct
◦ packed struct
◦ Struct Naming
◦ Anonymous Struct Literals
◦ Tuples
• enum
◦ extern enum
◦ Enum Literals
◦ Non-exhaustive enum
• union
◦ Tagged union
◦ extern union
◦ packed union
◦ Anonymous Union Literals
• opaque
• Blocks
◦ Shadowing
◦ Empty Blocks
• switch
◦ Exhaustive Switching
◦ Switching with Enum Literals
◦ Inline Switch Prongs
• while
◦ Labeled while
◦ while with Optionals
◦ while with Error Unions
◦ inline while
• for
◦ Labeled for
◦ inline for
• if
◦ if with Optionals
• defer
• unreachable
◦ Basics
◦ At Compile-Time
• noreturn
• Functions
◦ Pass-by-value Parameters
◦ Function Parameter Type Inference
◦ inline fn
◦ Function Reflection
• Errors
◦ Error Set Type
▪ The Global Error Set
◦ Error Union Type
▪ catch
▪ try
▪ errdefer
▪ Common errdefer Slip-Ups
▪ Merging Error Sets
▪ Inferred Error Sets
◦ Error Return Traces
▪ Implementation Details
• Optionals
◦ Optional Type
◦ null
◦ Optional Pointers
• Casting
◦ Type Coercion
▪ Type Coercion: Stricter Qualification
▪ Type Coercion: Integer and Float Widening
▪ Type Coercion: Float to Int
▪ Type Coercion: Slices, Arrays and Pointers
▪ Type Coercion: Optionals
▪ Type Coercion: Error Unions
▪ Type Coercion: Compile-Time Known Numbers
▪ Type Coercion: Unions and Enums
▪ Type Coercion: undefined
▪ Type Coercion: Tuples to Arrays
◦ Explicit Casts
◦ Peer Type Resolution
• Zero Bit Types
◦ void
• Result Location Semantics
◦ Result Types
◦ Result Locations
• usingnamespace
• comptime
◦ Introducing the Compile-Time Concept
▪ Compile-Time Parameters
▪ Compile-Time Variables
▪ Compile-Time Expressions
◦ Generic Data Structures
◦ Case Study: print in Zig
• Assembly
◦ Output Constraints
◦ Input Constraints
◦ Clobbers
◦ Global Assembly
• Atomics
• Async Functions
• Builtin Functions
◦ @addrSpaceCast ◦ @byteSwap
◦ @addWithOverflow ◦ @bitReverse
◦ @alignCast ◦ @offsetOf
◦ @alignOf ◦ @call
◦ @as ◦ @cDefine
◦ @atomicLoad ◦ @cImport
◦ @atomicRmw ◦ @cInclude
◦ @atomicStore ◦ @clz
◦ @bitCast ◦ @cmpxchgStrong
◦ @bitOffsetOf ◦ @cmpxchgWeak
◦ @bitSizeOf ◦ @compileError
◦ @breakpoint ◦ @compileLog
◦ @mulAdd ◦ @constCast
◦ @ctz ◦ @popCount
◦ @cUndef ◦ @prefetch
◦ @cVaArg ◦ @ptrCast
◦ @cVaCopy ◦ @ptrFromInt
◦ @cVaEnd ◦ @rem
◦ @cVaStart ◦ @returnAddress
◦ @divExact ◦ @select
◦ @divFloor ◦ @setAlignStack
◦ @divTrunc ◦ @setCold
◦ @embedFile ◦ @setEvalBranchQuota
◦ @enumFromInt ◦ @setFloatMode
◦ @errorFromInt ◦ @setRuntimeSafety
◦ @errorName ◦ @shlExact
◦ @errorReturnTrace ◦ @shlWithOverflow
◦ @errorCast ◦ @shrExact
◦ @export ◦ @shuffle
◦ @extern ◦ @sizeOf
◦ @fence ◦ @splat
◦ @field ◦ @reduce
◦ @fieldParentPtr ◦ @src
◦ @floatCast ◦ @sqrt
◦ @floatFromInt ◦ @sin
◦ @frameAddress ◦ @cos
◦ @hasDecl ◦ @tan
◦ @hasField ◦ @exp
◦ @import ◦ @exp2
◦ @inComptime ◦ @log
◦ @intCast ◦ @log2
◦ @intFromBool ◦ @log10
◦ @intFromEnum ◦ @abs
◦ @intFromError ◦ @floor
◦ @intFromFloat ◦ @ceil
◦ @intFromPtr ◦ @trunc
◦ @max ◦ @round
◦ @memcpy ◦ @subWithOverflow
◦ @memset ◦ @tagName
◦ @min ◦ @This
◦ @wasmMemorySize ◦ @trap
◦ @wasmMemoryGrow ◦ @truncate
◦ @mod ◦ @Type
◦ @mulWithOverflow ◦ @typeInfo
◦ @panic ◦ @typeName
◦ @TypeOf ◦ @workGroupId
◦ @unionInit ◦ @workGroupSize
◦ @Vector ◦ @workItemId
◦ @volatileCast
• Build Mode
◦ Debug
◦ ReleaseFast
◦ ReleaseSafe
◦ ReleaseSmall
• Single Threaded Builds
• Undefined Behavior
◦ Reaching Unreachable Code
◦ Index out of Bounds
◦ Cast Negative Number to Unsigned Integer
◦ Cast Truncates Data
◦ Integer Overflow
▪ Default Operations
▪ Standard Library Math Functions
▪ Builtin Overflow Functions
▪ Wrapping Operations
◦ Exact Left Shift Overflow
◦ Exact Right Shift Overflow
◦ Division by Zero
◦ Remainder Division by Zero
◦ Exact Division Remainder
◦ Attempt to Unwrap Null
◦ Attempt to Unwrap Error
◦ Invalid Error Code
◦ Invalid Enum Cast
◦ Invalid Error Set Cast
◦ Incorrect Pointer Alignment
◦ Wrong Union Field Access
◦ Out of Bounds Float to Integer Cast
◦ Pointer Cast Invalid Null
• Memory
◦ Choosing an Allocator
◦ Where are the bytes?
◦ Implementing an Allocator
◦ Heap Allocation Failure
◦ Recursion
◦ Lifetime and Ownership
• Compile Variables
• Root Source File
• Zig Build System
• C
◦ C Type Primitives
◦ Import from C Header File
◦ C Translation CLI
▪ Command line flags
▪ Using -target and -cflags
▪ @cImport vs translate-c
◦ C Translation Caching
◦ Translation failures
◦ C Macros
◦ C Pointers
◦ C Variadic Functions
◦ Exporting a C Library
◦ Mixing Object Files
• WebAssembly
◦ Freestanding
◦ WASI
• Targets
• Style Guide
◦ Avoid Redundancy in Names
◦ Avoid Redundant Names in Fully-Qualified Namespaces
◦ Whitespace
◦ Names
◦ Examples
◦ Doc Comment Guidance
• Source Encoding
• Keyword Reference
• Appendix
◦ Containers
◦ Grammar
◦ Zen
Introduction
Zig is a general-purpose programming language and toolchain for maintaining robust,
optimal, and reusable software.
Robust
Behavior is correct even for edge cases such as out of memory.
Optimal
Write programs the best way they can behave and perform.
Reusable
The same code works in many environments which have different constraints.
Maintainable
Precisely communicate intent to the compiler and other programmers. The language
imposes a low overhead to reading code and is resilient to changing requirements and
environments.
Often the most efficient way to learn something new is to see examples, so this
documentation shows how to use each of Zig's features. It is all on one page so you can
search with your browser's search tool.
The code samples in this document are compiled and tested as part of the main test suite of
Zig.
This HTML document depends on no external files, so you can use it offline.
Zig's Standard Library contains commonly used algorithms, data structures, and definitions
to help you build programs or libraries. You will see many examples of Zig's Standard Library
used in this documentation. To learn more about the Zig Standard Library, visit the link
above.
Hello World
hello.zig
const std = @import("std");
Shell
$ zig build-exe hello.zig
$ ./hello
Hello, world!
Most of the time, it is more appropriate to write to stderr rather than stdout, and whether
or not the message is successfully written to the stream is irrelevant. For this common case,
there is a simpler API:
hello_again.zig
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, world!\n", .{});
}
Shell
$ zig build-exe hello_again.zig
$ ./hello_again
Hello, world!
In this case, the ! may be omitted from the return type because no errors are returned
from the function.
See also:
• Values
• @import
• Errors
• Root Source File
• Source Encoding
Comments
Zig supports 3 types of comments. Normal comments are ignored, but doc comments and
top-level doc comments are used by the compiler to generate the package documentation.
Shell
zig test -femit-docs main.zig
comments.zig
const print = @import("std").debug.print;
//print("Hello?", .{});
Shell
$ zig build-exe comments.zig
$ ./comments
Hello, world!
There are no multiline comments in Zig (e.g. like /* */ comments in C). This allows Zig to
have the property that each line of code can be tokenized out of context.
Doc Comments
A doc comment is one that begins with exactly three slashes (i.e. /// but not //// );
multiple doc comments in a row are merged together to form a multiline doc comment. The
doc comment documents whatever immediately follows it.
doc_comments.zig
/// A structure for storing a timestamp, with nanosecond precision (this is a
/// multiline doc comment).
const Timestamp = struct {
/// The number of seconds since the epoch (this is also a doc comment).
seconds: i64, // signed so we can represent pre-1970 (not a doc comment)
/// The number of nanoseconds past the second (doc comment again).
nanos: u32,
/// Returns a `Timestamp` struct representing the Unix epoch; that is, the
/// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).
pub fn unixEpoch() Timestamp {
return Timestamp{
.seconds = 0,
.nanos = 0,
};
}
};
Doc comments are only allowed in certain places; it is a compile error to have a doc
comment in an unexpected place, such as in the middle of an expression, or just before a
non-doc comment.
invalid_doc-comment.zig
/// doc-comment
//! top-level doc-comment
const std = @import("std");
Shell
$ zig build-obj invalid_doc-comment.zig
docgen_tmp/invalid_doc-comment.zig:1:16: error: expected type expression, found 'a document comment'
/// doc-comment
^
unattached_doc-comment.zig
pub fn main() void {}
Shell
$ zig build-obj unattached_doc-comment.zig
docgen_tmp/unattached_doc-comment.zig:3:1: error: unattached documentation comment
/// End of file
^~~~~~~~~~~~~~~
Doc comments can be interleaved with normal comments. Currently, when producing the
package documentation, normal comments are merged with doc comments.
It is a compile error if a top-level doc comment is not placed at the start of a container,
before any expressions.
tldoc_comments.zig
//! This module provides functions for retrieving the current date and
//! time with varying degrees of precision and accuracy. It does not
//! depend on libc, but will use functions from it if available.
const S = struct {
//! Top level comments are allowed inside a container other than a module,
//! but it is not very useful. Currently, when producing the package
//! documentation, these comments are ignored.
};
Values
values.zig
// Top-level declarations are order-independent:
const print = std.debug.print;
const std = @import("std");
const os = std.os;
const assert = std.debug.assert;
// floats
const seven_div_three: f32 = 7.0 / 3.0;
print("7.0 / 3.0 = {}\n", .{seven_div_three});
// boolean
print("{}\n{}\n{}\n", .{
true and false,
true or false,
!true,
});
// optional
var optional_value: ?[]const u8 = null;
assert(optional_value == null);
optional_value = "hi";
assert(optional_value != null);
// error union
var number_or_error: anyerror!i32 = error.ArgNotFound;
Shell
$ zig build-exe values.zig
$ ./values
1 + 1 = 2
7.0 / 3.0 = 2.3333333e0
false
true
false
optional 1
type: ?[]const u8
value: null
optional 2
type: ?[]const u8
value: hi
error union 1
type: anyerror!i32
value: error.ArgNotFound
error union 2
type: anyerror!i32
value: 1234
Primitive Types
Primitive Types
unsigned
u128 unsigned 128-bit integer
__int128
uintptr_t ,
usize unsigned pointer sized integer. Also see #5185
size_t
unsigned long
c_ulonglong for ABI compatibility with C
long
In addition to the integer types above, arbitrary bit-width integers can be referenced by
using an identifier of i or u followed by digits. For example, the identifier i7 refers to a
signed 7-bit integer. The maximum allowed bit-width of an integer type is 65535 .
See also:
• Integers
• Floats
• void
• Errors
• @Type
Primitive Values
Primitive Values
Name Description
See also:
• Optionals
• undefined
String Literals and Unicode Code Point Literals
String literals are constant single-item Pointers to null-terminated byte arrays. The type of
string literals encodes both the length, and the fact that they are null-terminated, and thus
they can be coerced to both Slices and Null-Terminated Pointers. Dereferencing string
literals converts them to Arrays.
Because Zig source code is UTF-8 encoded, any non-ASCII bytes appearing within a string
literal in source code carry their UTF-8 meaning into the content of the string in the Zig
program; the bytes are not modified by the compiler. It is possible to embed non-UTF-8
bytes into a string literal using \xNN notation.
Indexing into a string containing non-ASCII bytes returns individual bytes, whether valid
UTF-8 or not.
Unicode code point literals have type comptime_int , the same as Integer Literals. All Escape
Sequences are valid in both string literals and Unicode code point literals.
string_literals.zig
const print = @import("std").debug.print;
const mem = @import("std").mem; // will be used to compare bytes
Shell
$ zig build-exe string_literals.zig
$ ./string_literals
*const [5:0]u8
5
e
0
true
128169
128175
�
true
true
0xfe
0x9f
See also:
• Arrays
• Source Encoding
Escape Sequences
Escape Sequences
\n Newline
\r Carriage Return
\t Tab
\\ Backslash
Multiline string literals have no escapes and can span across multiple lines. To start a
multiline string literal, use the \\ token. Just like a comment, the string literal goes until
the end of the line. The end of the line is not included in the string literal. However, if the
next line begins with \\ then a newline is appended and the string literal continues.
multiline_string_literals.zig
const hello_world_in_c =
\\#include <stdio.h>
\\
\\int main(int argc, char **argv) {
\\ printf("hello world\n");
\\ return 0;
\\}
;
See also:
• @embedFile
Assignment
Use the const keyword to assign a value to an identifier:
constant_identifier_cannot_change.zig
const x = 1234;
fn foo() void {
// It works at file scope as well as inside functions.
const y = 5678;
Shell
$ zig build-exe constant_identifier_cannot_change.zig
constant_identifier_cannot_change.zig:8:7: error: cannot assign to constant
y += 1;
~~^~~~
referenced by:
main: constant_identifier_cannot_change.zig:12:5
callMain: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:17
remaining reference traces hidden; use '-freference-trace' to see all reference traces
const applies to all of the bytes that the identifier immediately addresses. Pointers have
their own const-ness.
If you need a variable that you can modify, use the var keyword:
mutable_var.zig
const print = @import("std").debug.print;
y += 1;
print("{d}", .{y});
}
Shell
$ zig build-exe mutable_var.zig
$ ./mutable_var
5679
var_must_be_initialized.zig
pub fn main() void {
var x: i32;
x = 1;
}
Shell
$ zig build-exe var_must_be_initialized.zig
var_must_be_initialized.zig:2:15: error: expected '=', found ';'
var x: i32;
^
undefined
assign_undefined.zig
const print = @import("std").debug.print;
Shell
$ zig build-exe assign_undefined.zig
$ ./assign_undefined
1
undefined can be coerced to any type. Once this happens, it is no longer possible to detect
that the value is undefined . undefined means the value could be anything, even something
that is nonsense according to the type. Translated into English, undefined means "Not a
meaningful value. Using this value would be a bug. The value will be unused, or overwritten
before being used."
In Debug mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and
to help detect use of undefined memory in a debugger. However, this behavior is only an
implementation feature, not a language semantic, so it is not guaranteed to be observable
to code.
Zig Test
Code written within one or more test declarations can be used to ensure behavior meets
expectations:
testing_introduction.zig
const std = @import("std");
test addOne {
// A test name can also be written using an identifier.
// This is a doctest, and serves as documentation for `addOne`.
try std.testing.expect(addOne(41) == 42);
}
/// The function `addOne` adds one to the number given as its argument.
fn addOne(number: i32) i32 {
return number + 1;
}
Shell
$ zig test testing_introduction.zig
1/2 testing_introduction.test.expect addOne adds one to 41... OK
2/2 testing_introduction.decltest.addOne... OK
All 2 tests passed.
The testing_introduction.zig code sample tests the function addOne to ensure that it
returns 42 given the input 41 . From this test's perspective, the addOne function is said to
be code under test.
zig testis a tool that creates and runs a test build. By default, it builds and runs an
executable program using the default test runner provided by the Zig Standard Library as its
main entry point. During the build, test declarations found while resolving the given Zig
source file are included for the default test runner to run and report on.
This documentation discusses the features of the default test runner as provided by
the Zig Standard Library. Its source code is located in lib/test_runner.zig .
The shell output shown above displays two lines after the zig test command. These lines
are printed to standard error by the default test runner:
Lines like this indicate which test, out of the total number of tests, is being run. In this
case, 1/2 indicates that the first test, out of a total of two tests, is being run. Note that,
when the test runner program's standard error is output to the terminal, these lines
are cleared when a test succeeds.
2/2 testing_introduction.decltest.addOne...
When the test name is an identifier, the default test runner uses the text decltest
instead of test.
All 2 tests passed.
This line indicates the total number of tests that have passed.
Test Declarations
Test declarations contain the keyword test , followed by an optional name written as a
string literal or an identifier, followed by a block containing any valid Zig code that is
allowed in a function.
Non-named test blocks always run during test builds and are exempt from Skip Tests.
Test declarations are similar to Functions: they have a return type and a block of code. The
implicit return type of test is the Error Union Type anyerror!void , and it cannot be
changed. When a Zig source file is not built using the zig test tool, the test declarations are
omitted from the build.
Test declarations can be written in the same file, where code under test is written, or in a
separate Zig source file. Since test declarations are top-level declarations, they are order-
independent and can be written before or after the code under test.
See also:
Doctests
Test declarations named using an identifier are doctests. The identifier must refer to
another declaration in scope. A doctest, like a doc comment, serves as documentation for
the associated declaration, and will appear in the generated documentation for the
declaration.
An effective doctest should be self-contained and focused on the declaration being tested,
answering questions a new user might have about its interface or intended usage, while
avoiding unnecessary or confusing details. A doctest is not a substitute for a doc comment,
but rather a supplement and companion providing a testable, code-driven example, verified
by zig test.
Test Failure
The default test runner checks for an error returned from a test. When a test returns an
error, the test is considered a failure and its error return trace is output to standard error.
The total number of failures will be reported after all tests have run.
testing_failure.zig
const std = @import("std");
Shell
$ zig test testing_failure.zig
1/2 testing_failure.test.expect this to fail... FAIL (TestUnexpectedResult)
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/testing.zig:540:14: 0x1038ebf in exp
if (!ok) return error.TestUnexpectedResult;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/testing_failure.zig:4:5: 0x1038fd5 in test
try std.testing.expect(false);
^
2/2 testing_failure.test.expect this to succeed... OK
1 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/72e7a2de6b571d78928d146ffe650af4/te
Skip Tests
One way to skip tests is to filter them out by using the zig test command line parameter
--test-filter [text]. This makes the test build only include tests whose name contains the
supplied filter text. Note that non-named tests are run even when using the --test-filter
[text] command line parameter.
To programmatically skip a test, make a test return the error error.SkipZigTest and the
default test runner will consider the test as being skipped. The total number of skipped
tests will be reported after all tests have run.
testing_skip.zig
test "this will be skipped" {
return error.SkipZigTest;
}
Shell
$ zig test testing_skip.zig
1/1 testing_skip.test.this will be skipped... SKIP
0 passed; 1 skipped; 0 failed.
When code allocates Memory using the Zig Standard Library's testing allocator,
std.testing.allocator , the default test runner will report any leaks that are found from
using the testing allocator:
testing_detect_leak.zig
const std = @import("std");
Shell
$ zig test testing_detect_leak.zig
1/1 testing_detect_leak.test.detect leak... OK
[gpa] (err): memory address 0x7fd0fba04000 leaked:
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/array_list.zig:457:67: 0x104b67e in
const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/array_list.zig:434:51: 0x10416c0 in
return self.ensureTotalCapacityPrecise(better_capacity);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/array_list.zig:483:41: 0x103e0b0 in
try self.ensureTotalCapacity(newlen);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/array_list.zig:262:49: 0x103affd in
const new_item_ptr = try self.addOne();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/testing_detect_leak.zig:6:20: 0x1039202 in
�');
try list.append('�
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x10486
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103e98
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x103b4f9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x103b061 in _start
asm volatile (switch (native_arch) {
^
See also:
• defer
• Memory
testing_detect_test.zig
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
test "builtin.is_test" {
try expect(isATest());
}
fn isATest() bool {
return builtin.is_test;
}
Shell
$ zig test testing_detect_test.zig
1/1 testing_detect_test.test.builtin.is_test... OK
All 1 tests passed.
The default test runner and the Zig Standard Library's testing namespace output messages
to standard error.
The Testing Namespace
The Zig Standard Library's testing namespace contains useful functions to help you create
tests. In addition to the expect function, this document uses a couple of more functions as
exemplified here:
testing_namespace.zig
const std = @import("std");
Shell
$ zig test testing_namespace.zig
1/2 testing_namespace.test.expectEqual demo... OK
2/2 testing_namespace.test.expectError demo... OK
All 2 tests passed.
The Zig Standard Library also contains functions to compare Slices, strings, and more. See
the rest of the std.testing namespace in the Zig Standard Library for more available
functions.
zig test has a few command line parameters which affect the compilation. See zig test
Variables
A variable is a unit of Memory storage.
It is generally preferable to use const rather than var when declaring a variable. This
causes less work for both humans and computers to do when reading code, and creates
more optimization opportunities.
The extern keyword or @extern builtin function can be used to link against a variable that is
exported from another object. The export keyword or @export builtin function can be used
to make a variable available to other objects at link time. In both cases, the type of the
variable must be C ABI compatible.
See also:
• Exporting a C Library
Identifiers
Variable identifiers are never allowed to shadow identifiers from an outer scope.
Identifiers must start with an alphabetic character or underscore and may be followed by
any number of alphanumeric characters or underscores. They must not overlap with any
keywords. See Keyword Reference.
If a name that does not fit these requirements is needed, such as for linking with external
libraries, the @"" syntax may be used.
identifiers.zig
const @"identifier with spaces in it" = 0xff;
const @"1SmallStep4Man" = 112358;
const c = @import("std").c;
pub extern "c" fn @"error"() void;
pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int;
Container level variables have static lifetime and are order-independent and lazily analyzed.
The initialization value of container level variables is implicitly comptime. If a container level
variable is const then its value is comptime -known, otherwise it is runtime-known.
test_container_level_variables.zig
var y: i32 = add(10, x);
const x: i32 = add(12, 34);
Shell
$ zig test test_container_level_variables.zig
1/1 test_container_level_variables.test.container level variables... OK
All 1 tests passed.
Container level variables may be declared inside a struct, union, enum, or opaque:
test_namespaced_container_level_variable.zig
const std = @import("std");
const expect = std.testing.expect;
const S = struct {
var x: i32 = 1234;
};
fn foo() i32 {
S.x += 1;
return S.x;
}
Shell
$ zig test test_namespaced_container_level_variable.zig
1/1 test_namespaced_container_level_variable.test.namespaced container level variable... OK
All 1 tests passed.
It is also possible to have local variables with static lifetime by using containers inside
functions.
test_static_local_variable.zig
const std = @import("std");
const expect = std.testing.expect;
fn foo() i32 {
const S = struct {
var x: i32 = 1234;
};
S.x += 1;
return S.x;
}
Shell
$ zig test test_static_local_variable.zig
1/1 test_static_local_variable.test.static local variable... OK
All 1 tests passed.
test_thread_local_variables.zig
const std = @import("std");
const assert = std.debug.assert;
fn testTls() void {
assert(x == 1234);
x += 1;
assert(x == 1235);
}
Shell
$ zig test test_thread_local_variables.zig
1/1 test_thread_local_variables.test.thread local storage... OK
All 1 tests passed.
For Single Threaded Builds, all thread local variables are treated as regular Container Level
Variables.
Local Variables
Local variables occur inside Functions, comptime blocks, and @cImport blocks.
When a local variable is const , it means that after initialization, the variable's value will not
change. If the initialization value of a const variable is comptime-known, then the variable is
also comptime -known.
A local variable may be qualified with the comptime keyword. This causes the variable's value
to be comptime -known, and all loads and stores of the variable to happen during semantic
analysis of the program, rather than at runtime. All variables declared in a comptime
expression are implicitly comptime variables.
test_comptime_variables.zig
const std = @import("std");
const expect = std.testing.expect;
x += 1;
y += 1;
try expect(x == 2);
try expect(y == 2);
if (y != 2) {
// This compile error never triggers because y is a comptime variable,
// and so `y != 2` is a comptime value, and this if is statically evaluated.
@compileError("wrong y value");
}
}
Shell
$ zig test test_comptime_variables.zig
1/1 test_comptime_variables.test.comptime vars... OK
All 1 tests passed.
Integers
Integer Literals
integer_literals.zig
const decimal_int = 98222;
const hex_int = 0xff;
const another_hex_int = 0xFF;
const octal_int = 0o755;
const binary_int = 0b11110000;
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.
runtime_vs_comptime.zig
fn divide(a: i32, b: i32) i32 {
return a / b;
}
In this function, values a and b are known only at runtime, and thus this division operation
is vulnerable to both Integer Overflow and Division by Zero.
See also:
• Wrapping Operations
Floats
Zig has the following floating point types:
Float Literals
Float literals have type comptime_float which is guaranteed to have the same precision and
operations of the largest other floating point type, which is f128 .
Float literals coerce to any floating point type, and to any integer type when there is no
fractional component.
float_literals.zig
const floating_point = 123.0E+77;
const another_float = 123.0;
const yet_another = 123.0e+77;
There is no syntax for NaN, infinity, or negative infinity. For these special values, one must
use the standard library:
float_special_values.zig
const std = @import("std");
By default floating point operations use Strict mode, but you can switch to Optimized
float_mode_obj.zig
const std = @import("std");
const big = @as(f64, 1 << 40);
Shell
$ zig build-obj float_mode_obj.zig -O ReleaseFast
For this test we have to separate code into two object files - otherwise the optimizer figures
out all the values at compile-time, which operates in strict mode.
float_mode_exe.zig
const print = @import("std").debug.print;
Shell
$ zig build-exe float_mode_exe.zig float_mode_obj.o
$ ./float_mode_exe
optimized = 1e-3
strict = 9.765625e-4
See also:
• @setFloatMode
• Division by Zero
Operators
There is no operator overloading. When you see an operator in Zig, you know that it is doing
something from this table, and nothing else.
Table of Operators
Name Syntax Types Remarks Example
See also
@addWithOverflow.
Twos-complement
wrapping behavior.
See also
@addWithOverflow.
See also
@subWithOverflow.
Twos-complement
wrapping behavior.
Wrapping a -% b Integers
Invokes Peer Type @as(u8, 0) -% 1 ==
a -%= b
Subtraction
Resolution for the
operands.
Name Syntax Types Remarks Example
See also
@subWithOverflow.
Wrapping Twos-complement
-%a Integers -%@as(i8, -128) == -
wrapping behavior.
Negation
See also
@mulWithOverflow.
Twos-complement
wrapping behavior.
See also
@mulWithOverflow.
Name Syntax Types Remarks Example
b must be
comptime-known or
a << b Integers
Bit Shift Left a <<= b have a type with log2 0b1 << 8 == 0b100000000
number of bits as a .
See also
@shlWithOverflow.
Integers
Bitwise Not ~a ~@as(u8, 0b10101111
If is null , returns
a
b ("default value"),
Equivalent to:
Optional Optionals const value: ?u32 =
a.?
value.? == 5678
Unwrap a orelse unreachable
Name Syntax Types Remarks Example
If a is an error ,
returns b ("default
value"), otherwise
returns the
Defaulting Error unwrapped value of const value: anyerror
a catch b
Unions const unwrapped = value
a catch |err| b
Error Unwrap a . Note that b may unwrapped == 1234
be a value of type
noreturn. err is the
error and is in scope
of the expression b .
If a is , returns
false
false without
bool
Logical And a and b evaluating b . (false and true) ==
Otherwise, returns
b.
If is true , returns
a
true without
bool
Logical Or a or b evaluating b . (false or true) ==
Otherwise, returns
b.
bool
Boolean Not !a !false == true
Returns true if a is
Optionals const value: ?u32 =
Null Check a == null null , otherwise (value == null) ==
returns false .
Returns false if a
Integers
Inequality a != b and b are equal, (1 != 1) == false
otherwise returns
Name Syntax Types Remarks Example
Floats
true . Invokes Peer
bool
Type Resolution for
type the operands.
Returns false if a is
Non-Null Optionals const value: ?u32 =
a != null null , otherwise (value != null) ==
Check
returns true .
Returns true if a is
greater than b,
Integers
otherwise returns
Greater Than a > b (2 > 1) == true
Floats false . Invokes Peer
Type Resolution for
the operands.
Returns true if a is
greater than or equal
Integers to b, otherwise
Greater or
a >= b returns false . (2 >= 1) == true
Equal Floats
Invokes Peer Type
Resolution for the
operands.
Returns true if a is
less than b,
Integers
otherwise returns
Less Than a < b (1 < 2) == true
Floats false . Invokes Peer
Returns true if a is
less than or equal to
Integers
Lesser or b, otherwise returns
a <= b (1 <= 2) == true
Equal Floats false . Invokes Peer
Precedence
Arrays
test_arrays.zig
const expect = @import("std").testing.expect;
const assert = @import("std").debug.assert;
const mem = @import("std").mem;
// array literal
const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
comptime {
assert(mem.eql(u8, &message, same_message));
}
// modifiable array
var some_integers: [100]i32 = undefined;
comptime {
assert(all_zero.len == 10);
assert(all_zero[5] == 0);
}
Shell
$ zig test test_arrays.zig
1/4 test_arrays.test.iterate over an array... OK
2/4 test_arrays.test.modify an array... OK
3/4 test_arrays.test.compile-time array initialization... OK
4/4 test_arrays.test.array initialization with function calls... OK
All 4 tests passed.
See also:
• for
• Slices
Multidimensional Arrays
test_multidimensional_arrays.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_multidimensional_arrays.zig
1/1 test_multidimensional_arrays.test.multidimensional arrays... OK
All 1 tests passed.
Sentinel-Terminated Arrays
The syntax [N:x]T describes an array which has a sentinel element of value x at the index
corresponding to the length N .
test_null_terminated_array.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_null_terminated_array.zig
1/2 test_null_terminated_array.test.0-terminated sentinel array... OK
2/2 test_null_terminated_array.test.extra 0s in 0-terminated sentinel array... OK
All 2 tests passed.
See also:
• Sentinel-Terminated Pointers
• Sentinel-Terminated Slices
Vectors
A vector is a group of booleans, Integers, Floats, or Pointers which are operated on in
parallel, using SIMD instructions if possible. Vector types are created with the builtin
function @Vector.
Vectors support the same builtin operators as their underlying base types. These operations
are performed element-wise, and return a vector of the same length as the input vectors.
This includes:
For rearranging elements within and between vectors, Zig provides the @shuffle and
@select functions.
Operations on vectors shorter than the target machine's native SIMD size will typically
compile to single SIMD instructions, while vectors longer than the target machine's native
SIMD size will compile to multiple SIMD instructions. If a given operation doesn't have SIMD
support on the target architecture, the compiler will default to operating on each vector
element one at a time. Zig supports any comptime-known vector length up to 2^32-1,
although small powers of two (2-64) are most typical. Note that excessively long vector
lengths (e.g. 2^20) may result in compiler crashes on current versions of Zig.
test_vector.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
// You can also assign from a slice with comptime-known length to a vector using .*
const vec2: @Vector(2, f32) = arr1[1..3].*;
Shell
$ zig test test_vector.zig
1/2 test_vector.test.Basic vector usage... OK
2/2 test_vector.test.Conversion between vectors, arrays, and slices... OK
All 2 tests passed.
See also:
• @splat
• @shuffle
• @select
• @reduce
Pointers
Zig has two kinds of pointers: single-item and many-item.
◦ Tmust have a known size, which means that it cannot be anyopaque or any other
opaque type.
• []T - is a slice (a fat pointer, which contains a pointer of type [*]T and a length).
◦ Supports index syntax: slice[i]
test_single_item_pointer.zig
const expect = @import("std").testing.expect;
// Dereference a pointer:
try expect(x_ptr.* == 1234);
// When you get the address of a const variable, you get a const single-item pointer.
try expect(@TypeOf(x_ptr) == *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;
try expect(@TypeOf(y_ptr) == *i32);
y_ptr.* += 1;
try expect(y_ptr.* == 5679);
}
Shell
$ zig test test_single_item_pointer.zig
1/2 test_single_item_pointer.test.address of syntax... OK
2/2 test_single_item_pointer.test.pointer array access... OK
All 2 tests passed.
Zig supports pointer arithmetic. It's better to assign the pointer to [*]T and increment that
variable. For example, directly incrementing the pointer from a slice will corrupt it.
test_pointer_arithmetic.zig
const expect = @import("std").testing.expect;
slice.ptr += 1;
// now the slice is in an bad state since len has not been updated
Shell
$ zig test test_pointer_arithmetic.zig
1/2 test_pointer_arithmetic.test.pointer arithmetic with many-item pointer... OK
2/2 test_pointer_arithmetic.test.pointer arithmetic with slices... OK
All 2 tests passed.
In Zig, we generally prefer Slices rather than Sentinel-Terminated Pointers. You can turn an
array or pointer into a slice using slice syntax.
Slices have bounds checking and are therefore protected against this kind of undefined
behavior. This is one reason we prefer slices to pointers.
test_slice_bounds.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_slice_bounds.zig
1/1 test_slice_bounds.test.pointer slicing... OK
All 1 tests passed.
Pointers work at compile-time too, as long as the code does not depend on an undefined
memory layout:
test_comptime_pointers.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_comptime_pointers.zig
1/1 test_comptime_pointers.test.comptime pointers... OK
All 1 tests passed.
test_integer_pointer_conversion.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_integer_pointer_conversion.zig
1/1 test_integer_pointer_conversion.test.@intFromPtr and @ptrFromInt... OK
All 1 tests passed.
Zig is able to preserve memory addresses in comptime code, as long as the pointer is never
dereferenced:
test_comptime_pointer_conversion.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_comptime_pointer_conversion.zig
1/1 test_comptime_pointer_conversion.test.comptime @ptrFromInt... OK
All 1 tests passed.
See also:
• Optional Pointers
• @ptrFromInt
• @intFromPtr
• C Pointers
volatile
Loads and stores are assumed to not have side effects. If a given load or store should have
side effects, such as Memory Mapped Input/Output (MMIO), use volatile . In the following
code, loads and stores with mmio_ptr are guaranteed to all happen and in the same order as
in source code:
test_volatile.zig
const expect = @import("std").testing.expect;
test "volatile" {
const mmio_ptr: *volatile u8 = @ptrFromInt(0x12345678);
try expect(@TypeOf(mmio_ptr) == *volatile u8);
}
Shell
$ zig test test_volatile.zig
1/1 test_volatile.test.volatile... OK
All 1 tests passed.
Note that volatile is unrelated to concurrency and Atomics. If you see code that is using
volatile for something other than Memory Mapped Input/Output, it is probably a bug.
@ptrCast converts a pointer's element type to another. This creates a new pointer that can
cause undetectable illegal behavior depending on the loads and stores that pass through it.
Generally, other kinds of type conversions are preferable to @ptrCast if possible.
test_pointer_casting.zig
const std = @import("std");
const expect = std.testing.expect;
// Even this example is contrived - there are better ways to do the above than
// pointer casting. For example, using a slice narrowing cast:
const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];
try expect(u32_value == 0x12121212);
Shell
$ zig test test_pointer_casting.zig
1/2 test_pointer_casting.test.pointer casting... OK
2/2 test_pointer_casting.test.pointer child type... OK
All 2 tests passed.
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 @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:
test_variable_alignment.zig
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
Shell
$ zig test test_variable_alignment.zig
1/1 test_variable_alignment.test.variable alignment... OK
All 1 tests passed.
In the same way that a *i32 can be coerced 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:
test_variable_func_alignment.zig
const expect = @import("std").testing.expect;
noop1();
try expect(@TypeOf(noop1) == fn () void);
try expect(@TypeOf(&noop1) == *align(1) const fn () void);
noop4();
try expect(@TypeOf(noop4) == fn () void);
try expect(@TypeOf(&noop4) == *align(4) const fn () void);
}
Shell
$ zig test test_variable_func_alignment.zig
1/2 test_variable_func_alignment.test.global variable alignment... OK
2/2 test_variable_func_alignment.test.function alignment... OK
All 2 tests passed.
If you have a pointer or a slice that has a small alignment, but you know that it actually has a
bigger alignment, use @alignCast to change the pointer into a more aligned pointer. This is
a no-op at runtime, but inserts a safety check:
test_incorrect_pointer_alignment.zig
const std = @import("std");
Shell
$ zig test test_incorrect_pointer_alignment.zig
1/1 test_incorrect_pointer_alignment.test.pointer alignment safety... thread 986387 panic: incorrect
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_incorrect_pointer_alignment.zig:10:68
const int_slice = std.mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_incorrect_pointer_alignment.zig:6:31:
try std.testing.expect(foo(bytes) == 0x11111111);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x1043f
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103a31
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x10396a9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1039211 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/840cc356c59e7bf37874f94a9b7c7cdf/te
allowzero
This pointer attribute allows a pointer to have address zero. This is only ever needed on the
freestanding OS target, where the address zero is mappable. If you want to represent null
pointers, use Optional Pointers instead. Optional Pointers with allowzero are not the same
size as pointers. In this code example, if the pointer did not have the allowzero attribute,
this would be a Pointer Cast Invalid Null panic:
test_allowzero.zig
const std = @import("std");
const expect = std.testing.expect;
test "allowzero" {
var zero: usize = 0; // var to make to runtime-known
_ = &zero; // suppress 'var is never mutated' error
const ptr: *allowzero i32 = @ptrFromInt(zero);
try expect(@intFromPtr(ptr) == 0);
}
Shell
$ zig test test_allowzero.zig
1/1 test_allowzero.test.allowzero... OK
All 1 tests passed.
Sentinel-Terminated Pointers
The syntax [*:x]T describes a pointer that has a length determined by a sentinel value. This
provides protection against buffer overflow and overreads.
sentinel-terminated_pointer.zig
const std = @import("std");
Shell
$ zig build-exe sentinel-terminated_pointer.zig -lc
sentinel-terminated_pointer.zig:11:16: error: expected type '[*:0]const u8', found '*const [14]u8'
_ = printf(&non_null_terminated_msg);
^~~~~~~~~~~~~~~~~~~~~~~~
sentinel-terminated_pointer.zig:11:16: note: destination pointer requires '0' sentinel
sentinel-terminated_pointer.zig:4:35: note: parameter type declared here
pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
~^~~~~~~~~~~~
referenced by:
callMain: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:511:32
callMainWithArgs: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:469:
remaining reference traces hidden; use '-freference-trace' to see all reference traces
See also:
• Sentinel-Terminated Slices
• Sentinel-Terminated Arrays
Slices
A slice is a pointer and a length. The difference between an array and a slice is that the
array's length is part of the type and known at compile-time, whereas the slice's length is
known at runtime. Both can be accessed with the `len` field.
test_basic_slices.zig
const expect = @import("std").testing.expect;
// If you slice with comptime-known start and end positions, the result is
// a pointer to an array, rather than a slice.
const array_ptr = array[0..array.len];
try expect(@TypeOf(array_ptr) == *[array.len]i32);
// You can perform a slice-by-length by slicing twice. This allows the compiler
// to perform some optimisations like recognising a comptime-known length when
// the start position is only known at runtime.
var runtime_start: usize = 1;
_ = &runtime_start;
const length = 2;
const array_ptr_len = array[runtime_start..][0..length];
try expect(@TypeOf(array_ptr_len) == *[length]i32);
// Slices have array bounds checking. If you try to access something out
// of bounds, you'll get a safety check failure:
slice[10] += 1;
// Note that `slice.ptr` does not invoke safety checking, while `&slice[0]`
// asserts that the slice has len > 0.
}
Shell
$ zig test test_basic_slices.zig
1/1 test_basic_slices.test.basic slices... thread 986458 panic: index out of bounds: index 10, len 4
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_basic_slices.zig:34:10: 0x1039439 in
slice[10] += 1;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x1044a
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103ad3
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1039a19 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1039581 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/5a09a80de513b3991a6774a68dfe69af/te
test_slices.zig
const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
const fmt = std.fmt;
// Generally, you can use UTF-8 and not worry about whether something is a
// string. If you don't need to deal with individual characters, no need
// to decode.
try expect(mem.eql(u8, hello_world, "hello 世界"));
}
Shell
$ zig test test_slices.zig
1/2 test_slices.test.using slices for strings... OK
2/2 test_slices.test.slice pointer... OK
All 2 tests passed.
See also:
• Pointers
• for
• Arrays
Sentinel-Terminated Slices
The syntax [:x]T is a slice which has a runtime-known length and also guarantees a sentinel
value at the element indexed by the length. The type does not guarantee that there are no
sentinel elements before that. Sentinel-terminated slices allow element access to the len
index.
test_null_terminated_slice.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_null_terminated_slice.zig
1/1 test_null_terminated_slice.test.0-terminated slice... OK
All 1 tests passed.
Sentinel-terminated slices can also be created using a variation of the slice syntax
data[start..end :x] , where data is a many-item pointer, array or slice and x is the sentinel
value.
test_null_terminated_slicing.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_null_terminated_slicing.zig
1/1 test_null_terminated_slicing.test.0-terminated slicing... OK
All 1 tests passed.
Sentinel-terminated slicing asserts that the element in the sentinel position of the backing
data is actually the sentinel value. If this is not the case, safety-protected Undefined
Behavior results.
test_sentinel_mismatch.zig
const std = @import("std");
const expect = std.testing.expect;
_ = slice;
}
Shell
$ zig test test_sentinel_mismatch.zig
1/1 test_sentinel_mismatch.test.sentinel mismatch... thread 986566 panic: sentinel mismatch: expected
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_sentinel_mismatch.zig:13:24: 0x1038f5
const slice = array[0..runtime_length :0];
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x1044a
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103aac
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1039539 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10390a1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/50fee1d1f56022c590a3e33bbeb31a11/te
See also:
• Sentinel-Terminated Pointers
• Sentinel-Terminated Arrays
struct
test_structs.zig
// Declare a struct.
// Zig gives no guarantees about the order of fields and the size of
// the struct but the fields are guaranteed to be ABI-aligned.
const Point = struct {
x: f32,
y: f32,
};
// Other than being available to call with dot syntax, struct methods are
// not special. You can reference them as any other declaration inside
// the struct:
try expect(Vec3.dot(v1, v2) == 0.0);
}
_ = does_nothing;
}
first: ?*Node,
last: ?*Node,
len: usize,
};
}
// Since types are first class values you can instantiate the type
// by assigning it to a variable:
const ListOfInts = LinkedList(i32);
try expect(ListOfInts == LinkedList(i32));
Shell
$ zig test test_structs.zig
1/4 test_structs.test.dot product... OK
2/4 test_structs.test.struct namespaced variable... OK
3/4 test_structs.test.field parent pointer... OK
4/4 test_structs.test.linked list... OK
All 4 tests passed.
Each struct field may have an expression indicating the default field value. Such expressions
are executed at comptime, and allow the field to be omitted in a struct literal expression:
struct_default_field_values.zig
const Foo = struct {
a: i32 = 1234,
b: i32,
};
Shell
$ zig test struct_default_field_values.zig
1/1 struct_default_field_values.test.default struct initialization fields... OK
All 1 tests passed.
Default field values are only appropriate when the data invariants of a struct cannot be
violated by omitting that field from an initialization.
bad_default_value.zig
const Threshold = struct {
minimum: f32 = 0.25,
maximum: f32 = 0.75,
Shell
$ zig build-exe bad_default_value.zig
$ ./bad_default_value
thread 986647 panic: reached unreachable code
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/debug.zig:403:14: 0x103657d in asser
if (!ok) unreachable; // assertion failure
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/bad_default_value.zig:8:15: 0x1033a89 in c
assert(t.maximum >= t.minimum);
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/bad_default_value.zig:19:42: 0x10339ba in
const category = threshold.categorize(0.90);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:511:37: 0x10338d5 in posix
const result = root.main() catch |err| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10333f1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
Above you can see the danger of ignoring this principle. The default field values caused the
data invariant to be violated, causing illegal behavior.
To fix this, remove the default values from all the struct fields, and provide a named default
value:
struct_default_value.zig
const Threshold = struct {
minimum: f32,
maximum: f32,
extern struct
An extern struct has in-memory layout matching the C ABI for the target.
If well-defined in-memory layout is not required, struct is a better choice because it places
fewer restrictions on the compiler.
See packed struct for a struct that has the ABI of its backing integer, which can be useful for
modeling flags.
See also:
• extern union
• extern enum
packed struct
This means that a packed struct can participate in a @bitCast or a @ptrCast to reinterpret
memory. This even works at comptime:
test_packed_structs.zig
const std = @import("std");
const native_endian = @import("builtin").target.cpu.arch.endian();
const expect = std.testing.expect;
fn doTheTest() !void {
try expect(@sizeOf(Full) == 2);
try expect(@sizeOf(Divided) == 2);
const full = Full{ .number = 0x1234 };
const divided: Divided = @bitCast(full);
try expect(divided.half1 == 0x34);
try expect(divided.quarter3 == 0x2);
try expect(divided.quarter4 == 0x1);
Shell
$ zig test test_packed_structs.zig
1/1 test_packed_structs.test.@bitCast between packed structs... OK
All 1 tests passed.
The backing integer is inferred from the fields' total bit width. Optionally, it can be explicitly
provided and enforced at compile time:
test_missized_packed_struct.zig
test "missized packed struct" {
const S = packed struct(u32) { a: u16, b: u8 };
_ = S{ .a = 4, .b = 2 };
}
Shell
$ zig test test_missized_packed_struct.zig
docgen_tmp/test_missized_packed_struct.zig:2:29: error: backing integer type 'u32' has bit size 32 bu
const S = packed struct(u32) { a: u16, b: u8 };
^~~
test_pointer_to_non-byte_aligned_field.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_pointer_to_non-byte_aligned_field.zig
1/1 test_pointer_to_non-byte_aligned_field.test.pointer to non-byte-aligned field... OK
All 1 tests passed.
However, the pointer to a non-byte-aligned field has special properties and cannot be
passed when a normal pointer is expected:
test_misaligned_pointer.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_misaligned_pointer.zig
docgen_tmp/test_misaligned_pointer.zig:17:20: error: expected type '*const u3', found '*align(1:3:1)
try expect(bar(&bit_field.b) == 2);
^~~~~~~~~~~~
docgen_tmp/test_misaligned_pointer.zig:17:20: note: pointer host size '1' cannot cast into pointer ho
docgen_tmp/test_misaligned_pointer.zig:17:20: note: pointer bit offset '3' cannot cast into pointer b
docgen_tmp/test_misaligned_pointer.zig:20:11: note: parameter type declared here
fn bar(x: *const u3) u3 {
^~~~~~~~~
In this case, the function bar cannot be called because the pointer to the non-ABI-aligned
field mentions the bit offset, but the function expects an ABI-aligned pointer.
Pointers to non-ABI-aligned fields share the same address as the other fields within their
host integer:
test_packed_struct_field_address.zig
const std = @import("std");
const expect = std.testing.expect;
test_bitOffsetOf_offsetOf.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_bitOffsetOf_offsetOf.zig
1/1 test_bitOffsetOf_offsetOf.test.offsets of non-byte-aligned fields... OK
All 1 tests passed.
Packed structs have the same alignment as their backing integer, however, overaligned
pointers to packed structs can override this:
test_overaligned_packed_struct.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_overaligned_packed_struct.zig
1/1 test_overaligned_packed_struct.test.overaligned pointer to packed struct... OK
All 1 tests passed.
test_aligned_struct_fields.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
Shell
$ zig test test_aligned_struct_fields.zig
1/1 test_aligned_struct_fields.test.aligned struct fields... OK
All 1 tests passed.
Using packed structs with volatile is problematic, and may be a compile error in the future.
For details on this subscribe to this issue. TODO update these docs with a recommendation
on how to use packed structs with MMIO (the use case for volatile packed structs) once this
issue is resolved. Don't worry, there will be a good solution for this use case in zig.
Struct Naming
Since all structs are anonymous, Zig infers the type name based on a few rules.
• If the struct is in the initialization expression of a variable, it gets named after that
variable.
• If the struct is in the expression, it gets named after the function it is returning
return
struct_name.zig
const std = @import("std");
Shell
$ zig build-exe struct_name.zig
$ ./struct_name
variable: struct_name.main.Foo
anonymous: struct_name.main__struct_3389
function: struct_name.List(i32)
Zig allows omitting the struct type of a literal. When the result is coerced, the struct literal
will directly instantiate the result location, with no copy:
test_struct_result.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_struct_result.zig
1/1 test_struct_result.test.anonymous struct literal... OK
All 1 tests passed.
The struct type can be inferred. Here the result location does not include a type, and so Zig
infers the type:
test_anonymous_struct.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_anonymous_struct.zig
1/1 test_anonymous_struct.test.fully anonymous struct... OK
All 1 tests passed.
Tuples
Anonymous structs can be created without specifying field names, and are referred to as
"tuples".
The fields are implicitly named using numbers starting from 0. Because their names are
integers, they cannot be accessed with . syntax without also wrapping them in @"" . Names
inside @"" are always recognised as identifiers.
Like arrays, tuples have a .len field, can be indexed (provided the index is comptime-known)
and work with the ++ and ** operators. They can also be iterated over with inline for.
test_tuples.zig
const std = @import("std");
const expect = std.testing.expect;
test "tuple" {
const values = .{
@as(u32, 1234),
@as(f64, 12.34),
true,
"hi",
} ++ .{false} ** 2;
try expect(values[0] == 1234);
try expect(values[4] == false);
inline for (values, 0..) |v, i| {
if (i != 2) continue;
try expect(v);
}
try expect(values.len == 6);
try expect(values.@"3"[0] == 'h');
}
Shell
$ zig test test_tuples.zig
1/1 test_tuples.test.tuple... OK
All 1 tests passed.
See also:
• comptime
• @fieldParentPtr
enum
test_enums.zig
const expect = @import("std").testing.expect;
const mem = @import("std").mem;
// Declare an enum.
const Type = enum {
ok,
not_ok,
};
Shell
$ zig test test_enums.zig
1/8 test_enums.test.enum ordinal value... OK
2/8 test_enums.test.set enum ordinal value... OK
3/8 test_enums.test.enum implicit ordinal values and overridden values... OK
4/8 test_enums.test.enum method... OK
5/8 test_enums.test.enum switch... OK
6/8 test_enums.test.std.meta.Tag... OK
7/8 test_enums.test.@typeInfo... OK
8/8 test_enums.test.@tagName... OK
All 8 tests passed.
See also:
• @typeInfo
• @tagName
• @sizeOf
extern enum
enum_export_error.zig
const Foo = enum { a, b, c };
export fn entry(foo: Foo) void { _ = foo; }
Shell
$ zig build-obj enum_export_error.zig
docgen_tmp/enum_export_error.zig:2:17: error: parameter of type 'enum_export_error.Foo' not allowed i
export fn entry(foo: Foo) void { _ = foo; }
^~~~~~~~
docgen_tmp/enum_export_error.zig:2:17: note: enum tag type 'u2' is not extern compatible
docgen_tmp/enum_export_error.zig:2:17: note: only integers with 0, 8, 16, 32, 64 and 128 bits are ext
docgen_tmp/enum_export_error.zig:1:13: note: enum declared here
const Foo = enum { a, b, c };
^~~~~~~~~~~~~~~~
enum_export.zig
const Foo = enum(c_int) { a, b, c };
export fn entry(foo: Foo) void { _ = foo; }
Shell
$ zig build-obj enum_export.zig
Enum Literals
Enum literals allow specifying the name of an enum field without specifying the enum type:
test_enum_literals.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_enum_literals.zig
1/2 test_enum_literals.test.enum literals... OK
2/2 test_enum_literals.test.switch using enum literals... OK
All 2 tests passed.
Non-exhaustive enum
A non-exhaustive enum can be created by adding a trailing _ field. The enum must specify a
tag type and cannot consume every enumeration value.
test_switch_non-exhaustive.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_switch_non-exhaustive.zig
1/1 test_switch_non-exhaustive.test.switch on non-exhaustive enum... OK
All 1 tests passed.
union
A bare union defines a set of possible types that a value can be as a list of fields. Only one
field can be active at a time. The in-memory representation of bare unions is not
guaranteed. Bare unions cannot be used to reinterpret memory. For that, use @ptrCast, or
use an extern union or a packed union which have guaranteed in-memory layout. Accessing
the non-active field is safety-checked Undefined Behavior:
test_wrong_union_access.zig
const Payload = union {
int: i64,
float: f64,
boolean: bool,
};
test "simple union" {
var payload = Payload{ .int = 1234 };
payload.float = 12.34;
}
Shell
$ zig test test_wrong_union_access.zig
1/1 test_wrong_union_access.test.simple union... thread 987122 panic: access of union field 'float' w
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_wrong_union_access.zig:8:12: 0x1038f2
payload.float = 12.34;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x10440
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103a11
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x10394b9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1039021 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/5285249bc32c82ff3a07356dfb83fe43/te
test_simple_union.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_simple_union.zig
1/1 test_simple_union.test.simple union... OK
All 1 tests passed.
Tagged union
Unions can be declared with an enum tag type. This turns the union into a tagged union,
which makes it eligible to use with switch expressions. Tagged unions coerce to their tag
type: Type Coercion: Unions and Enums.
test_tagged_union.zig
const std = @import("std");
const expect = std.testing.expect;
switch (c) {
ComplexTypeTag.ok => |value| try expect(value == 42),
ComplexTypeTag.not_ok => unreachable,
}
}
Shell
$ zig test test_tagged_union.zig
1/2 test_tagged_union.test.switch on tagged union... OK
2/2 test_tagged_union.test.get tag type... OK
All 2 tests passed.
In order to modify the payload of a tagged union in a switch expression, place a * before
the variable name to make it a pointer:
test_switch_modify_tagged_union.zig
const std = @import("std");
const expect = std.testing.expect;
switch (c) {
ComplexTypeTag.ok => |*value| value.* += 1,
ComplexTypeTag.not_ok => unreachable,
}
Shell
$ zig test test_switch_modify_tagged_union.zig
1/1 test_switch_modify_tagged_union.test.modify tagged union in switch... OK
All 1 tests passed.
Unions can be made to infer the enum tag type. Further, unions can have methods just like
structs and enums.
test_union_method.zig
const std = @import("std");
const expect = std.testing.expect;
try expect(v1.truthy());
try expect(!v2.truthy());
}
Shell
$ zig test test_union_method.zig
1/1 test_union_method.test.union method... OK
All 1 tests passed.
@tagName can be used to return a comptime [:0]const u8 value representing the field
name:
test_tagName.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_tagName.zig
1/1 test_tagName.test.@tagName... OK
All 1 tests passed.
extern union
An extern union has memory layout guaranteed to be compatible with the target C ABI.
See also:
• extern struct
packed union
A packed union has well-defined in-memory layout and is eligible to be in a packed struct.
Anonymous Struct Literals syntax can be used to initialize unions without specifying the
type:
test_anonymous_union.zig
const std = @import("std");
const expect = std.testing.expect;
fn makeNumber() Number {
return .{ .float = 12.34 };
}
Shell
$ zig test test_anonymous_union.zig
1/1 test_anonymous_union.test.anonymous union literal syntax... OK
All 1 tests passed.
opaque
opaque {} declares a new type with an unknown (but non-zero) size and alignment. It can
contain declarations the same as structs, unions, and enums.
This is typically used for type safety when interacting with C code that does not expose
struct details. Example:
test_opaque.zig
const Derp = opaque {};
const Wat = opaque {};
Shell
$ zig test test_opaque.zig
docgen_tmp/test_opaque.zig:6:9: error: expected type '*test_opaque.Derp', found '*test_opaque.Wat'
bar(w);
^
docgen_tmp/test_opaque.zig:6:9: note: pointer type child 'test_opaque.Wat' cannot cast into pointer t
docgen_tmp/test_opaque.zig:2:13: note: opaque declared here
const Wat = opaque {};
^~~~~~~~~
docgen_tmp/test_opaque.zig:1:14: note: opaque declared here
const Derp = opaque {};
^~~~~~~~~
docgen_tmp/test_opaque.zig:4:18: note: parameter type declared here
extern fn bar(d: *Derp) void;
^~~~~
referenced by:
test.call foo: docgen_tmp/test_opaque.zig:10:5
remaining reference traces hidden; use '-freference-trace' to see all reference traces
Blocks
Blocks are used to limit the scope of variable declarations:
test_blocks.zig
test "access variable after block scope" {
{
var x: i32 = 1;
_ = &x;
}
x += 1;
}
Shell
$ zig test test_blocks.zig
docgen_tmp/test_blocks.zig:6:5: error: use of undeclared identifier 'x'
x += 1;
^
Blocks are expressions. When labeled, break can be used to return a value from the block:
test_labeled_break.zig
const std = @import("std");
const expect = std.testing.expect;
const x = blk: {
y += 1;
break :blk y;
};
try expect(x == 124);
try expect(y == 124);
}
Shell
$ zig test test_labeled_break.zig
1/1 test_labeled_break.test.labeled break from labeled block expression... OK
All 1 tests passed.
Here, blk can be any name.
See also:
• Labeled while
• Labeled for
Shadowing
Identifiers are never allowed to "hide" other identifiers by using the same name:
test_shadowing.zig
const pi = 3.14;
Shell
$ zig test test_shadowing.zig
docgen_tmp/test_shadowing.zig:6:13: error: local variable shadows declaration of 'pi'
var pi: i32 = 1234;
^~
docgen_tmp/test_shadowing.zig:1:1: note: declared here
const pi = 3.14;
^~~~~~~~~~~~~~~
Because of this, when you read Zig code you can always rely on an identifier to consistently
mean the same thing within the scope it is defined. Note that you can, however, use the
same name if the scopes are separate:
test_scopes.zig
test "separate scopes" {
{
const pi = 3.14;
_ = pi;
}
{
var pi: bool = true;
_ = π
}
}
Shell
$ zig test test_scopes.zig
1/1 test_scopes.test.separate scopes... OK
All 1 tests passed.
Empty Blocks
test {
const a = {};
const b = void{};
try expect(@TypeOf(a) == void);
try expect(@TypeOf(b) == void);
try expect(a == b);
}
Shell
$ zig test test_empty_block.zig
1/1 test_empty_block.test_0... OK
All 1 tests passed.
switch
test_switch.zig
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
// Ranges can be specified using the ... syntax. These are inclusive
// of both ends.
5...100 => 1,
Shell
$ zig test test_switch.zig
1/2 test_switch.test.switch simple... OK
2/2 test_switch.test.switch inside function... OK
All 2 tests passed.
switch can be used to capture the field values of a Tagged union. Modifications to the field
values can be done by placing a * before the capture variable name, turning it into a
pointer.
test_switch_tagged_union.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_switch_tagged_union.zig
1/1 test_switch_tagged_union.test.switch on tagged union... OK
All 1 tests passed.
See also:
• comptime
• enum
• @compileError
• Compile Variables
Exhaustive Switching
When a switch expression does not have an else clause, it must exhaustively list all the
possible values. Failure to do so is a compile error:
test_unhandled_enumeration_value.zig
const Color = enum {
auto,
off,
on,
};
Shell
$ zig test test_unhandled_enumeration_value.zig
docgen_tmp/test_unhandled_enumeration_value.zig:9:5: error: switch must handle all possibilities
switch (color) {
^~~~~~
docgen_tmp/test_unhandled_enumeration_value.zig:3:5: note: unhandled enumeration value: 'off'
off,
^~~
docgen_tmp/test_unhandled_enumeration_value.zig:1:15: note: enum 'test_unhandled_enumeration_value.Co
const Color = enum {
^~~~
Enum Literals can be useful to use with switch to avoid repetitively specifying enum or
union types:
test_exhaustive_switch.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_exhaustive_switch.zig
1/1 test_exhaustive_switch.test.enum literals with switch... OK
All 1 tests passed.
Switch prongs can be marked as inline to generate the prong's body for each possible
value it could have, making the captured value comptime.
test_inline_switch.zig
const std = @import("std");
const expect = std.testing.expect;
const expectError = std.testing.expectError;
inline_prong_range.zig
fn isFieldOptional(comptime T: type, field_index: usize) !bool {
const fields = @typeInfo(T).Struct.fields;
return switch (field_index) {
inline 0...fields.len - 1 => |idx| @typeInfo(fields[idx].type) == .Optional,
else => return error.IndexOutOfBounds,
};
}
inline else prongs can be used as a type safe alternative to inline for loops:
test_inline_else.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_inline_else.zig
1/1 test_inline_else.test.inline for and inline else similarity... OK
All 1 tests passed.
When using an inline prong switching on an union an additional capture can be used to
obtain the union's enum tag value.
test_inline_switch_union_tag.zig
const std = @import("std");
const expect = std.testing.expect;
const U = union(enum) {
a: u32,
b: f32,
};
fn getNum(u: U) u32 {
switch (u) {
// Here `num` is a runtime-known value that is either
// `u.a` or `u.b` and `tag` is `u`'s comptime-known tag value.
inline else => |num, tag| {
if (tag == .b) {
return @intFromFloat(num);
}
return num;
}
}
}
test "test" {
const u = U{ .b = 42 };
try expect(getNum(u) == 42);
}
Shell
$ zig test test_inline_switch_union_tag.zig
1/1 test_inline_switch_union_tag.test.test... OK
All 1 tests passed.
See also:
• inline while
• inline for
while
A while loop is used to repeatedly execute an expression until some condition is no longer
true.
test_while.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_while.zig
1/1 test_while.test.while basic... OK
All 1 tests passed.
test_while_break.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_while_break.zig
1/1 test_while_break.test.while break... OK
All 1 tests passed.
test_while_continue.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_while_continue.zig
1/1 test_while_continue.test.while continue... OK
All 1 tests passed.
While loops support a continue expression which is executed when the loop is continued.
The continue keyword respects this expression.
test_while_continue_expression.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_while_continue_expression.zig
1/2 test_while_continue_expression.test.while loop continue expression... OK
2/2 test_while_continue_expression.test.while loop continue expression, more complicated... OK
All 2 tests passed.
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.
test_while_else.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_while_else.zig
1/1 test_while_else.test.while else... OK
All 1 tests passed.
Labeled while
When a while loop is labeled, it can be referenced from a break or continue from within a
nested loop:
test_while_nested_break.zig
test "nested break" {
outer: while (true) {
while (true) {
break :outer;
}
}
}
test "nested continue" {
var i: usize = 0;
outer: while (i < 10) : (i += 1) {
while (true) {
continue :outer;
}
}
}
Shell
$ zig test test_while_nested_break.zig
1/2 test_while_nested_break.test.nested break... OK
2/2 test_while_nested_break.test.nested continue... OK
All 2 tests passed.
Just like if expressions, while loops can take an optional as the condition and capture the
payload. When null is encountered the loop exits.
When the |x| syntax is present on a while expression, the while condition must have an
Optional Type.
The else branch is allowed on optional iteration. In this case, it will be executed on the first
null value encountered.
test_while_null_capture.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_while_null_capture.zig
1/1 test_while_null_capture.test.while null capture... OK
All 1 tests passed.
Just like 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.
test_while_error_capture.zig
const expect = @import("std").testing.expect;
fn eventuallyErrorSequence() anyerror!u32 {
return if (numbers_left == 0) error.ReachedZero else blk: {
numbers_left -= 1;
break :blk numbers_left;
};
}
Shell
$ zig test test_while_error_capture.zig
1/1 test_while_error_capture.test.while error union capture... OK
All 1 tests passed.
inline while
While loops can be inlined. This causes the loop to be unrolled, which allows the code to do
some things which only work at compile time, such as use types as first class values.
test_inline_while.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_inline_while.zig
1/1 test_inline_while.test.inline while loop... OK
All 1 tests passed.
• You need the loop to execute at comptime for the semantics to work.
• You have a benchmark to prove that forcibly unrolling the loop in this way is
measurably faster.
See also:
• if
• Optionals
• Errors
• comptime
• unreachable
for
test_for.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_for.zig
1/4 test_for.test.for basics... OK
2/4 test_for.test.multi object for... OK
3/4 test_for.test.for reference... OK
4/4 test_for.test.for else... OK
All 4 tests passed.
Labeled for
When a for loop is labeled, it can be referenced from a break or continue from within a
nested loop:
test_for_nested_break.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_for_nested_break.zig
1/2 test_for_nested_break.test.nested break... OK
2/2 test_for_nested_break.test.nested continue... OK
All 2 tests passed.
inline for
For loops can be inlined. This causes the loop to be unrolled, which allows the code to do
some things which only work at compile time, such as use types as first class values. The
capture value and iterator value of inlined for loops are compile-time known.
test_inline_for.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_inline_for.zig
1/1 test_inline_for.test.inline for loop... OK
All 1 tests passed.
• You need the loop to execute at comptime for the semantics to work.
• You have a benchmark to prove that forcibly unrolling the loop in this way is
measurably faster.
See also:
• while
• comptime
• Arrays
• Slices
if
test_if.zig
// If expressions have three uses, corresponding to the three types:
// * bool
// * ?T
// * anyerror!T
const a: anyerror!u32 = 0;
if (a) |value| {
try expect(value == 0);
} else |err| {
_ = err;
unreachable;
}
if (c) |value| {
try expect(value == 9);
} else |_| {
unreachable;
}
}
Shell
$ zig test test_if.zig
1/3 test_if.test.if expression... OK
2/3 test_if.test.if boolean... OK
3/3 test_if.test.if error union... OK
All 3 tests passed.
if with Optionals
test_if_optionals.zig
const expect = @import("std").testing.expect;
const a: ?u32 = 0;
if (a) |value| {
try expect(value == 0);
} else {
unreachable;
}
if (c) |value| {
try expect(value == 2);
} else {
unreachable;
}
}
const a: anyerror!?u32 = 0;
if (a) |optional_value| {
try expect(optional_value.? == 0);
} else |err| {
_ = err;
unreachable;
}
if (d) |optional_value| {
try expect(optional_value.? == 9);
} else |_| {
unreachable;
}
}
Shell
$ zig test test_if_optionals.zig
1/2 test_if_optionals.test.if optional... OK
2/2 test_if_optionals.test.if error union with optional... OK
All 2 tests passed.
See also:
• Optionals
• Errors
defer
Executes an expression unconditionally at scope exit.
test_defer.zig
const std = @import("std");
const expect = std.testing.expect;
const print = std.debug.print;
fn deferExample() !usize {
var a: usize = 1;
{
defer a = 2;
a = 1;
}
try expect(a == 2);
a = 5;
return a;
}
Shell
$ zig test test_defer.zig
1/1 test_defer.test.defer basics... OK
All 1 tests passed.
defer_unwind.zig
const std = @import("std");
const expect = std.testing.expect;
const print = std.debug.print;
defer {
print("1 ", .{});
}
defer {
print("2 ", .{});
}
if (false) {
// defers are not run if they are never executed.
defer {
print("3 ", .{});
}
}
}
Shell
$ zig test defer_unwind.zig
1/1 defer_unwind.test.defer unwinding...
2 1 OK
All 1 tests passed.
test_invalid_defer.zig
fn deferInvalidExample() !void {
defer {
return error.DeferError;
}
return error.DeferError;
}
Shell
$ zig test test_invalid_defer.zig
docgen_tmp/test_invalid_defer.zig:3:9: error: cannot return from defer expression
return error.DeferError;
^~~~~~~~~~~~~~~~~~~~~~~
docgen_tmp/test_invalid_defer.zig:2:5: note: defer expression here
defer {
^~~~~
See also:
• Errors
unreachable
In Debug and ReleaseSafe mode unreachable emits a call to panic with the message
reached unreachable code .
In ReleaseFast and ReleaseSmall mode, the optimizer uses the assumption that unreachable
Basics
test_unreachable.zig
// unreachable is used to assert that control flow will never reach a
// particular location:
test "basic math" {
const x = 1;
const y = 2;
if (x + y != 3) {
unreachable;
}
}
Shell
$ zig test test_unreachable.zig
1/1 test_unreachable.test.basic math... OK
All 1 tests passed.
test_assertion_failure.zig
// This is how std.debug.assert is implemented
fn assert(ok: bool) void {
if (!ok) unreachable; // assertion failure
}
Shell
$ zig test test_assertion_failure.zig
1/1 test_assertion_failure.test.this will fail... thread 988108 panic: reached unreachable code
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_assertion_failure.zig:3:14: 0x1038e6d
if (!ok) unreachable; // assertion failure
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_assertion_failure.zig:8:11: 0x1038e2a
assert(false);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x1043a
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x1039c8
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1039319 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1038e81 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/c95e7748bd467945dbe9cc12e10f552c/te
At Compile-Time
test_comptime_unreachable.zig
const assert = @import("std").debug.assert;
Shell
$ zig test test_comptime_unreachable.zig
docgen_tmp/test_comptime_unreachable.zig:10:16: error: unreachable code
assert(@TypeOf(unreachable) == noreturn);
^~~~~~~~~~~~~~~~~~~~
docgen_tmp/test_comptime_unreachable.zig:10:24: note: control flow is diverted here
assert(@TypeOf(unreachable) == noreturn);
^~~~~~~~~~~
See also:
• Zig Test
• Build Mode
• comptime
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:
test_noreturn.zig
fn foo(condition: bool, b: u32) void {
const a = if (condition) b else return;
_ = a;
@panic("do something with a");
}
test "noreturn" {
foo(false, 1);
}
Shell
$ zig test test_noreturn.zig
1/1 test_noreturn.test.noreturn... OK
All 1 tests passed.
test_noreturn_from_exit.zig
const std = @import("std");
const builtin = @import("builtin");
const native_arch = builtin.cpu.arch;
const expect = std.testing.expect;
test "foo" {
const value = bar() catch ExitProcess(1);
try expect(value == 1234);
}
fn bar() anyerror!u32 {
return 1234;
}
Shell
$ zig test test_noreturn_from_exit.zig -target x86_64-windows --test-no-exec
Functions
test_functions.zig
const std = @import("std");
const builtin = @import("builtin");
const native_arch = builtin.cpu.arch;
const expect = std.testing.expect;
return a + b;
}
// The @setCold builtin tells the optimizer that a function is rarely called.
fn abort() noreturn {
@setCold(true);
while (true) {}
}
// The naked calling convention makes a function not have any function prologue or epilogue.
// This can be useful when integrating with assembly.
fn _start() callconv(.Naked) noreturn {
abort();
}
// The inline calling convention forces a function to be inlined at all call sites.
// If the function cannot be inlined, it is a compile-time error.
fn shiftLeftOne(a: u32) callconv(.Inline) u32 {
return a << 1;
}
// The pub specifier allows the function to be visible when importing.
// Another file can use @import and call sub2
pub fn sub2(a: i8, b: i8) i8 { return a - b; }
test "function" {
try expect(doOp(add, 5, 6) == 11);
try expect(doOp(sub2, 5, 6) == -1);
}
Shell
$ zig test test_functions.zig
1/1 test_functions.test.function... OK
All 1 tests passed.
There is a difference between a function body and a function pointer. Function bodies are
comptime-only types while function Pointers may be runtime-known.
Pass-by-value Parameters
Primitive types such as Integers and Floats passed as parameters are copied, and then the
copy is available in the function body. This is called "passing by value". Copying a primitive
type is essentially free and typically involves nothing more than setting a register.
Structs, unions, and arrays can sometimes be more efficiently passed as a reference, since a
copy could be arbitrarily expensive depending on the size. When these types are passed as
parameters, Zig may choose to copy and pass by value, or pass by reference, whichever way
Zig decides will be faster. This is made possible, in part, by the fact that parameters are
immutable.
test_pass_by_reference_or_value.zig
const Point = struct {
x: i32,
y: i32,
};
Shell
$ zig test test_pass_by_reference_or_value.zig
1/1 test_pass_by_reference_or_value.test.pass struct to function... OK
All 1 tests passed.
For extern functions, Zig follows the C ABI for passing structs and unions by value.
Function parameters can be declared with anytype in place of the type. In this case the
parameter types will be inferred when the function is called. Use @TypeOf and @typeInfo
to get information about the inferred type.
test_fn_type_inference.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_fn_type_inference.zig
1/1 test_fn_type_inference.test.fn type inference... OK
All 1 tests passed.
inline fn
Adding the inline keyword to a function definition makes that function become
semantically inlined at the callsite. This is not a hint to be possibly observed by optimization
passes, but has implications on the types and values involved in the function call.
Unlike normal function calls, arguments at an inline function callsite which are compile-time
known are treated as Compile Time Parameters. This can potentially propagate all the way
to the return value:
inline_call.zig
test "inline function call" {
if (foo(1200, 34) != 1234) {
@compileError("bad");
}
}
Shell
$ zig test inline_call.zig
1/1 inline_call.test.inline function call... OK
All 1 tests passed.
If inline is removed, the test fails with the compile error instead of passing.
It is generally better to let the compiler decide when to inline a function, except for these
scenarios:
• To change how many stack frames are in the call stack, for debugging purposes.
• To force comptime-ness of the arguments to propagate to the return value of the
function, as in the above example.
• Real world performance measurements demand it.
Note that actually restricts what the compiler is allowed to do. This can harm binary
inline
Function Reflection
test_fn_reflection.zig
const std = @import("std");
const math = std.math;
const testing = std.testing;
try testing.expect(@typeInfo(@TypeOf(math.Log2Int)).Fn.is_generic);
}
Shell
$ zig test test_fn_reflection.zig
1/1 test_fn_reflection.test.fn reflection... OK
All 1 tests passed.
Errors
An error set is like an 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 error set type defaults to a u16 , though if the maximum number of distinct error values
is provided via the --error-limit [num] command line parameter an integer type with the
minimum number of bits required to represent all of the error values will be used.
test_coerce_error_subset_to_superset.zig
const std = @import("std");
const FileOpenError = error {
AccessDenied,
OutOfMemory,
FileNotFound,
};
Shell
$ zig test test_coerce_error_subset_to_superset.zig
1/1 test_coerce_error_subset_to_superset.test.coerce subset to superset... OK
All 1 tests passed.
test_coerce_error_superset_to_subset.zig
const FileOpenError = error {
AccessDenied,
OutOfMemory,
FileNotFound,
};
Shell
$ zig test test_coerce_error_superset_to_subset.zig
docgen_tmp/test_coerce_error_superset_to_subset.zig:16:12: error: expected type 'error{OutOfMemory}',
return err;
^~~
docgen_tmp/test_coerce_error_superset_to_subset.zig:16:12: note: 'error.AccessDenied' not a member of
docgen_tmp/test_coerce_error_superset_to_subset.zig:16:12: note: 'error.FileNotFound' not a member of
docgen_tmp/test_coerce_error_superset_to_subset.zig:15:28: note: function return type declared here
fn foo(err: FileOpenError) AllocationError {
^~~~~~~~~~~~~~~
referenced by:
test.coerce superset to subset: docgen_tmp/test_coerce_error_superset_to_subset.zig:12:5
remaining reference traces hidden; use '-freference-trace' to see all reference traces
There is a shortcut for declaring an error set with only 1 value, and then getting that value:
single_value_error_set_shortcut.zig
const err = error.FileNotFound;
single_value_error_set.zig
const err = (error {FileNotFound}).FileNotFound;
anyerror refers to the global error set. This is the error set that contains all errors in the
entire compilation unit. It is a superset of all other error sets and a subset of none of them.
You can coerce any error set to the global one, and you can explicitly cast an error of the
global error set to a non-global one. This inserts a language-level assert to make sure the
error value is in fact in the destination error set.
The global error set should generally be avoided because it prevents the compiler from
knowing what errors are possible at compile-time. Knowing the error set at compile-time is
better for generated documentation and helpful error messages, such as forgetting a
possible error value in a switch.
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.
error_union_parsing_u64.zig
const std = @import("std");
const maxInt = std.math.maxInt;
// x *= radix
var ov = @mulWithOverflow(x, radix);
if (ov[1] != 0) return error.OverFlow;
// x += digit
ov = @addWithOverflow(ov[0], digit);
if (ov[1] != 0) return error.OverFlow;
x = ov[0];
}
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 => maxInt(u8),
};
}
Shell
$ zig test error_union_parsing_u64.zig
1/1 error_union_parsing_u64.test.parse u64... OK
All 1 tests passed.
Notice the return type is !u64 . This means that the function either returns an unsigned 64
bit integer, or an error. We left off the error set to the left of the ! , so the error set is
inferred.
Within the function definition, you can see some return statements that return an error, and
at the bottom a return statement that returns a u64 . Both types coerce to anyerror!u64 .
What it looks like to use this function varies depending on what you're trying to do. One of
the following:
catch
If you want to provide a default value, you can use the catch binary operator:
catch.zig
const parseU64 = @import("error_union_parsing_u64.zig").parseU64;
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 .
If you want to provide a default value with catch after performing some logic, you can
combine catch with named Blocks:
handle_error_with_catch_block.zig.zig
const parseU64 = @import("error_union_parsing_u64.zig").parseU64;
try
Let's say you wanted to return the error if you got one, otherwise continue with the
function logic:
catch_err_return.zig
const parseU64 = @import("error_union_parsing_u64.zig").parseU64;
try.zig
const parseU64 = @import("error_union_parsing_u64.zig").parseU64;
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:
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 and ReleaseSmall modes. So, while we're debugging the
application, if there was a surprise error here, the application would crash appropriately.
You may want to take a different action for every situation. For that, we combine the if and
switch expression:
handle_all_error_scenarios.zig
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,
}
}
Finally, you may want to handle only some errors. For that, you can capture the unhandled
errors in the else case, which now contains a narrower error set:
handle_some_error_scenarios.zig
fn doAnotherThing(str: []u8) error{InvalidChar}!void {
if (parseU64(str, 10)) |number| {
doSomethingWithNumber(number);
} else |err| switch (err) {
error.Overflow => {
// handle overflow...
},
else => |leftover_err| return leftover_err,
}
}
You must use the variable capture syntax. If you don't need the variable, you can capture
with _ and avoid the switch .
handle_no_error_scenarios.zig
fn doADifferentThing(str: []u8) void {
if (parseU64(str, 10)) |number| {
doSomethingWithNumber(number);
} else |_| {
// do as you'd like
}
}
errdefer
Example:
errdefer_example.zig
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);
// here the errdefer will not run since we're returning success from the function.
// but the defer will run!
return foo;
}
The neat thing about this is that you get robust error handling without the verbosity and
cognitive overhead of trying to make sure every exit path is covered. The deallocation code
is always directly following the allocation code.
It should be noted that errdefer statements only last until the end of the block they are
written in, and therefore are not run if an error is returned outside of that block:
test_errdefer_slip_ups.zig
const std = @import("std");
const Allocator = std.mem.Allocator;
fn getFooData() !u32 {
return 666;
}
return foo;
}
test "createFoo" {
try std.testing.expectError(error.InvalidParam, createFoo(std.testing.allocator, 2468));
}
Shell
$ zig test test_errdefer_slip_ups.zig
1/1 test_errdefer_slip_ups.test.createFoo... OK
[gpa] (err): memory address 0x7f1f0a428000 leaked:
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_errdefer_slip_ups.zig:9:28: 0x103941f
return allocator.create(Foo);
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_errdefer_slip_ups.zig:22:39: 0x103963
var foo = try tryToAllocateFoo(allocator);
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_errdefer_slip_ups.zig:39:62: 0x103987
try std.testing.expectError(error.InvalidParam, createFoo(std.testing.allocator, 2468));
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x10492
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103f4b
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x103ba89 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x103b5f1 in _start
asm volatile (switch (native_arch) {
^
To ensure that deallocateFoo is properly called when returning an error, you must add an
errdefer outside of the block:
test_errdefer_block.zig
const std = @import("std");
const Allocator = std.mem.Allocator;
fn getFooData() !u32 {
return 666;
}
return foo;
}
test "createFoo" {
try std.testing.expectError(error.InvalidParam, createFoo(std.testing.allocator, 2468));
}
Shell
$ zig test test_errdefer_block.zig
1/1 test_errdefer_block.test.createFoo... OK
All 1 tests passed.
The fact that errdefers only last for the block they are declared in is especially important
when using loops:
test_errdefer_loop_leak.zig
const std = @import("std");
const Allocator = std.mem.Allocator;
fn getData() !u32 {
return 666;
}
return foos;
}
test "genFoos" {
try std.testing.expectError(error.TooManyFoos, genFoos(std.testing.allocator, 5));
}
Shell
$ zig test test_errdefer_loop_leak.zig
1/1 test_errdefer_loop_leak.test.genFoos... OK
[gpa] (err): memory address 0x7faf65592000 leaked:
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_errdefer_loop_leak.zig:17:40: 0x10397
foo.data = try allocator.create(u32);
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_errdefer_loop_leak.zig:31:59: 0x103a1
try std.testing.expectError(error.TooManyFoos, genFoos(std.testing.allocator, 5));
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x104a0
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103ff5
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x103c339 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x103bea1 in _start
asm volatile (switch (native_arch) {
^
Special care must be taken with code that allocates in a loop to make sure that no memory
is leaked when returning an error:
test_errdefer_loop.zig
const std = @import("std");
const Allocator = std.mem.Allocator;
return foos;
}
test "genFoos" {
try std.testing.expectError(error.TooManyFoos, genFoos(std.testing.allocator, 5));
}
Shell
$ zig test test_errdefer_loop.zig
1/1 test_errdefer_loop.test.genFoos... OK
All 1 tests passed.
• These primitives give enough expressiveness that it's completely practical to have
failing to check for an error be a compile error. If you really want to ignore the error,
you can add catch unreachable and get the added benefit of crashing in Debug and
ReleaseSafe modes if your assumption was wrong.
• Since Zig understands error types, it can pre-weight branches in favor of errors not
occurring. Just a small optimization benefit that is not available in other languages.
See also:
• defer
• if
• switch
An error union is created with the ! binary operator. You can use compile-time reflection to
access the child type of an error union:
test_error_union.zig
const expect = @import("std").testing.expect;
// Use compile-time reflection to access the error set type of an error union:
try comptime expect(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set == anyerror);
}
Shell
$ zig test test_error_union.zig
1/1 test_error_union.test.error union... OK
All 1 tests passed.
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
comptime branches. For example, the Zig standard library uses LinuxFileOpenError ||
WindowsFileOpenError for the error set of opening files.
test_merging_error_sets.zig
const A = error{
NotDir,
const C = A || B;
fn foo() C!void {
return error.NotDir;
}
Because many functions in Zig return a possible error, Zig supports inferring the error set.
To infer the error set for a function, prepend the ! operator to the function’s return type,
like !T :
test_inferred_error_sets.zig
// With an inferred error set
pub fn add_inferred(comptime T: type, a: T, b: T) !T {
const ov = @addWithOverflow(a, b);
if (ov[1] != 0) return error.Overflow;
return ov[0];
}
Shell
$ zig test test_inferred_error_sets.zig
1/1 test_inferred_error_sets.test.inferred error set... OK
All 1 tests passed.
When a function has an inferred error set, that function becomes generic and thus it
becomes trickier to do certain things with it, such as obtain a function pointer, or have an
error set that is consistent across different build targets. Additionally, inferred error sets
are incompatible with recursion.
In these situations, it is recommended to use an explicit error set. You can generally start
with an empty error set and let compile errors guide you toward completing the set.
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 try everywhere and then still be able to know what
happened if an error ends up bubbling all the way out of your application.
error_return_trace.zig
pub fn main() !void {
try foo(12);
}
fn bar() !void {
if (baz()) {
try quux();
} else |err| switch (err) {
error.FileNotFound => try hello(),
}
}
fn baz() !void {
try bang1();
}
fn quux() !void {
try bang2();
}
fn hello() !void {
try bang2();
}
fn bang1() !void {
return error.FileNotFound;
}
fn bang2() !void {
return error.PermissionDenied;
}
Shell
$ zig build-exe error_return_trace.zig
$ ./error_return_trace
error: PermissionDenied
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:34:5: 0x1033968 in
return error.FileNotFound;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:22:5: 0x1033a73 in
try bang1();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:38:5: 0x1033a98 in
return error.PermissionDenied;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:30:5: 0x1033b03 in
try bang2();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:17:31: 0x1033bba in
error.FileNotFound => try hello(),
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:7:9: 0x1033ca0 in f
try bar();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/error_return_trace.zig:2:5: 0x1033cf8 in m
try foo(12);
^
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:
stack_trace.zig
pub fn main() void {
foo(12);
}
fn bar() void {
if (baz()) {
quux();
} else {
hello();
}
}
fn baz() bool {
return bang1();
}
fn quux() void {
bang2();
}
fn hello() void {
bang2();
}
fn bang1() bool {
return false;
}
fn bang2() void {
@panic("PermissionDenied");
}
Shell
$ zig build-exe stack_trace.zig
$ ./stack_trace
thread 988779 panic: PermissionDenied
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/stack_trace.zig:38:5: 0x1037e30 in bang2 (
@panic("PermissionDenied");
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/stack_trace.zig:30:10: 0x10684d8 in hello
bang2();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/stack_trace.zig:17:14: 0x1037e0c in bar (s
hello();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/stack_trace.zig:7:12: 0x1035d2c in foo (st
bar();
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/stack_trace.zig:2:8: 0x1033ccd in main (st
foo(12);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033579 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10330e1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
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 Debug and ReleaseSafe builds and disabled by
default in ReleaseFast and ReleaseSmall builds.
There are a few ways to activate this error return tracing feature:
Implementation Details
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:
stack_trace_struct.zig
pub const StackTrace = struct {
index: usize,
instruction_addresses: [N]usize,
};
Here, N is the maximum function call depth as determined by call graph analysis. Recursion
is ignored and counts for 2.
A pointer 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:
zig_return_error_fn.zig
// marked as "no-inline" in LLVM IR
fn __zig_return_error(stack_trace: *StackTrace) void {
stack_trace.instruction_addresses[stack_trace.index] = @returnAddress();
stack_trace.index = (stack_trace.index + 1) % N;
}
The cost is 2 math operations plus some memory reads and writes. The memory accessed is
constrained and should remain cached for the duration of the error return bubbling.
As for code size cost, 1 function call before a return statement is no big deal. Even so, I 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.
Optionals
One area that Zig provides safety without compromising efficiency or readability is with the
optional type.
The question mark symbolizes the optional type. You can convert a type to an optional type
by putting a question mark in front of it, like this:
optional_integer.zig
// normal integer
const normal_int: i32 = 1234;
// optional integer
const optional_int: ?i32 = 5678;
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.
C code
call_malloc_in_c.c
// malloc prototype included for reference
void *malloc(size_t size);
Zig code
call_malloc_from_zig.zig
// malloc prototype included for reference
extern fn malloc(size: usize) ?[*]u8;
fn doAThing() ?*Foo {
const ptr = malloc(1234) orelse return null;
_ = ptr; // ...
}
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
The other form of checking against NULL you might see looks like this:
checking_null_in_c.c
void do_a_thing(struct Foo *foo) {
// do some stuff
if (foo) {
do_something_with_foo(foo);
}
// do some stuff
}
checking_null_in_zig.zig
const Foo = struct{};
fn doSomethingWithFoo(foo: *Foo) void { _ = foo; }
if (optional_foo) |foo| {
doSomethingWithFoo(foo);
}
// do some stuff
}
Once again, the notable thing here is that inside the if block, foo is no longer an optional
pointer, it is a pointer, which cannot be null.
One benefit to this is that functions which take pointers as arguments can be annotated
with the "nonnull" attribute - __attribute__((nonnull)) in GCC. The optimizer can sometimes
make better decisions knowing that pointer arguments cannot be null.
Optional Type
An optional is created by putting ? in front of a type. You can use compile-time reflection
to access the child type of an optional:
test_optional_type.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_optional_type.zig
1/1 test_optional_type.test.optional type... OK
All 1 tests passed.
null
Just like undefined, null has its own type, and the only way to use it is to cast it to a
different type:
null.zig
const optional_value: ?i32 = null;
Optional Pointers
An optional pointer is guaranteed to be the same size as a pointer. The null of the optional
is guaranteed to be address 0.
test_optional_pointer.zig
const expect = @import("std").testing.expect;
var x: i32 = 1;
ptr = &x;
// Optional pointers are the same size as normal pointers, because pointer
// value 0 is used as the null value.
try expect(@sizeOf(?*i32) == @sizeOf(*i32));
}
Shell
$ zig test test_optional_pointer.zig
1/1 test_optional_pointer.test.optional pointers... OK
All 1 tests passed.
See also:
Casting
A type cast converts a value of one type to another. Zig has Type Coercion for conversions
that are known to be completely safe and unambiguous, and Explicit Casts for conversions
that one would not want to happen on accident. There is also a third kind of type conversion
called Peer Type Resolution for the case when a result type must be decided given multiple
operand types.
Type Coercion
Type coercion occurs when one type is expected, but different type is provided:
test_type_coercion.zig
test "type coercion - variable declaration" {
const a: u8 = 1;
const b: u16 = a;
_ = b;
}
Shell
$ zig test test_type_coercion.zig
1/3 test_type_coercion.test.type coercion - variable declaration... OK
2/3 test_type_coercion.test.type coercion - function call... OK
3/3 test_type_coercion.test.type coercion - @as builtin... OK
All 3 tests passed.
Type coercions are only allowed when it is completely unambiguous how to get from one
type to another, and the transformation is guaranteed to be safe. There is one exception,
which is C Pointers.
Values which have the same representation at runtime can be cast to increase the strictness
of the qualifiers, no matter how nested the qualifiers are:
These casts are no-ops at runtime since the value representation does not change.
test_no_op_casts.zig
test "type coercion - const qualification" {
var a: i32 = 1;
const b: *i32 = &a;
foo(b);
}
Shell
$ zig test test_no_op_casts.zig
1/1 test_no_op_casts.test.type coercion - const qualification... OK
All 1 tests passed.
test_pointer_coerce_const_optional.zig
const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
Shell
$ zig test test_pointer_coerce_const_optional.zig
1/1 test_pointer_coerce_const_optional.test.cast *[1][*]const u8 to [*]const ?[*]const u8... OK
All 1 tests passed.
Integers coerce to integer types which can represent every value of the old type, and
likewise Floats coerce to float types which can represent every value of the old type.
test_integer_widening.zig
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const mem = std.mem;
Shell
$ zig test test_integer_widening.zig
1/3 test_integer_widening.test.integer widening... OK
2/3 test_integer_widening.test.implicit unsigned integer to signed integer... OK
3/3 test_integer_widening.test.float widening... OK
All 3 tests passed.
A compiler error is appropriate because this ambiguous expression leaves the compiler two
choices about the coercion.
test_ambiguous_coercion.zig
// Compile time coercion of float to int
test "implicit cast to comptime_int" {
const f: f32 = 54.0 / 5;
_ = f;
}
Shell
$ zig test test_ambiguous_coercion.zig
docgen_tmp/test_ambiguous_coercion.zig:3:25: error: ambiguous coercion of division operands 'comptime
const f: f32 = 54.0 / 5;
~~~~~^~~
test_coerce_slices_arrays_and_pointers.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_coerce_slices_arrays_and_pointers.zig
1/7 test_coerce_slices_arrays_and_pointers.test.*const [N]T to []const T... OK
2/7 test_coerce_slices_arrays_and_pointers.test.*const [N]T to E![]const T... OK
3/7 test_coerce_slices_arrays_and_pointers.test.*const [N]T to ?[]const T... OK
4/7 test_coerce_slices_arrays_and_pointers.test.*[N]T to []T... OK
5/7 test_coerce_slices_arrays_and_pointers.test.*[N]T to [*]T... OK
6/7 test_coerce_slices_arrays_and_pointers.test.*[N]T to ?[*]T... OK
7/7 test_coerce_slices_arrays_and_pointers.test.*T to *[1]T... OK
All 7 tests passed.
See also:
• C Pointers
The payload type of Optionals, as well as null, coerce to the optional type.
test_coerce_optionals.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_coerce_optionals.zig
1/1 test_coerce_optionals.test.coerce to optionals... OK
All 1 tests passed.
Shell
$ zig test test_coerce_optional_wrapped_error_union.zig
1/1 test_coerce_optional_wrapped_error_union.test.coerce to optionals wrapped in error union... OK
All 1 tests passed.
The payload type of an Error Union Type as well as the Error Set Type coerce to the error
union type:
test_coerce_to_error_union.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_coerce_to_error_union.zig
1/1 test_coerce_to_error_union.test.coercion to error unions... OK
All 1 tests passed.
test_coerce_large_to_small.zig
const std = @import("std");
const expect = std.testing.expect;
test "coercing large integer type to smaller one when value is comptime-known to fit" {
const x: u64 = 255;
const y: u8 = x;
try expect(y == 255);
}
Shell
$ zig test test_coerce_large_to_small.zig
1/1 test_coerce_large_to_small.test.coercing large integer type to smaller one when value is comptime
All 1 tests passed.
Tagged unions can be coerced to enums, and enums can be coerced to tagged unions when
they are comptime-known to be a field of the union that has only one possible value, such as
void:
test_coerce_unions_enums.zig
const std = @import("std");
const expect = std.testing.expect;
const E = enum {
one,
two,
three,
};
const U = union(E) {
one: i32,
two: f32,
three,
};
const U2 = union(enum) {
a: void,
b: f32,
const u_4: U2 = .a; // coerce enum literal to union with inferred enum tag type.
try expect(u_4.tag() == 1);
Shell
$ zig test test_coerce_unions_enums.zig
1/1 test_coerce_unions_enums.test.coercion between unions and enums... OK
All 1 tests passed.
See also:
• union
• enum
Tuples can be coerced to arrays, if all of the fields have the same type.
test_coerce_tuples_arrays.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_coerce_tuples_arrays.zig
1/1 test_coerce_tuples_arrays.test.coercion from homogenous tuple to array... OK
All 1 tests passed.
Explicit Casts
Explicit casts are performed via 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.
• switch expressions
• if expressions
• while expressions
• for expressions
• Multiple break statements in a block
• Some binary operations
This kind of type resolution chooses a type that all peer types can coerce into. Here are
some examples:
test_peer_type_resolution.zig
const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
return slice[0..1];
}
test "peer type resolution: *[0]u8, []const u8, and anyerror![]u8" {
{
var data = "hi".*;
const slice = data[0..];
try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
}
comptime {
var data = "hi".*;
const slice = data[0..];
try expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
try expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
}
}
fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
if (a) {
return &[_]u8{};
}
return slice[0..1];
}
// The non-error and error cases are only peers if the error case is just a switch expression;
// the pattern `x catch |err| blk: { switch (err) {...} }` does not consider the unwrapped `x`
// and error case to be peers.
const c = a catch |err| switch (err) {
error.A => 0,
error.B => 1,
error.C => null,
};
try expect(@TypeOf(c) == ?u32);
}
Shell
$ zig test test_peer_type_resolution.zig
1/8 test_peer_type_resolution.test.peer resolve int widening... OK
2/8 test_peer_type_resolution.test.peer resolve arrays of different size to const slice... OK
3/8 test_peer_type_resolution.test.peer resolve array and const slice... OK
4/8 test_peer_type_resolution.test.peer type resolution: ?T and T... OK
5/8 test_peer_type_resolution.test.peer type resolution: *[0]u8 and []const u8... OK
6/8 test_peer_type_resolution.test.peer type resolution: *[0]u8, []const u8, and anyerror![]u8... OK
7/8 test_peer_type_resolution.test.peer type resolution: *const T and ?*T... OK
8/8 test_peer_type_resolution.test.peer type resolution: error union switch... OK
All 8 tests passed.
• void
• The Integers u0 and i0 .
• Arrays and Vectors with len 0, or with an element type that is a zero bit type.
• An enum with only 1 tag.
• A struct with all fields being zero bit types.
• A union with only 1 field which is a zero bit type.
These types can only ever have one possible value, and thus require 0 bits to represent.
Code that makes use of these types is not included in the final generated code:
zero_bit_types.zig
export fn entry() void {
var x: void = {};
var y: void = {};
x = y;
y = x;
}
When this turns into machine code, 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 epilogue.
void
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 :
test_void_in_hashmap.zig
const std = @import("std");
const expect = std.testing.expect;
try expect(map.contains(2));
try expect(!map.contains(3));
_ = map.remove(2);
try expect(!map.contains(2));
}
Shell
$ zig test test_void_in_hashmap.zig
1/1 test_void_in_hashmap.test.turn HashMap into a set with void... OK
All 1 tests passed.
Note that this is different from 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 anyopaque . void has a known size of 0 bytes, and anyopaque has an
unknown, but non-zero, size.
Expressions of type voidare the only ones whose value can be ignored. For example,
ignoring a non- void expression is a compile error:
test_expression_ignored.zig
test "ignoring expression value" {
foo();
}
fn foo() i32 {
return 1234;
}
Shell
$ zig test test_expression_ignored.zig
docgen_tmp/test_expression_ignored.zig:2:8: error: value of type 'i32' ignored
foo();
~~~^~
docgen_tmp/test_expression_ignored.zig:2:8: note: all non-void values must be used
docgen_tmp/test_expression_ignored.zig:2:8: note: this error can be suppressed by assigning the value
However, if the expression has type void , there will be no error. Expression results can be
explicitly ignored by assigning them to _ .
test_void_ignored.zig
test "void is ignored" {
returnsVoid();
}
fn returnsVoid() void {}
fn foo() i32 {
return 1234;
}
Shell
$ zig test test_void_ignored.zig
1/2 test_void_ignored.test.void is ignored... OK
2/2 test_void_ignored.test.explicitly ignoring expression value... OK
All 2 tests passed.
As a motivating example, consider the statement const x: u32 = 42; . The type annotation
here provides a result type of u32 to the initialization expression 42 , instructing the
compiler to coerce this integer (initally of type comptime_int ) to this type. We will see more
examples shortly.
This is not an implementation detail: the logic outlined above is codified into the Zig
language specification, and is the primary mechanism of type inference in the language.
This system is collectively referred to as "Result Location Semantics".
Result Types
Result types are propagated recursively through expressions where possible. For instance, if
the expression &e has result type *u32 , then e is given a result type of u32 , allowing the
language to perform this coercion before taking a reference.
The result type mechanism is utilized by casting builtins such as @intCast . Rather than
taking as an argument the type to cast to, these builtins use their result type to determine
this information. The result type is often known from context; where it is not, the @as
We can break down the result types for each component of a simple expression as follows:
result_type_propagation.zig
const expectEqual = @import("std").testing.expectEqual;
test "result type propagates through struct initializer" {
const S = struct { x: u32 };
const val: u64 = 123;
const s: S = .{ .x = @intCast(val) };
// .{ .x = @intCast(val) } has result type `S` due to the type annotation
// @intCast(val) has result type `u32` due to the type of the field `S.x`
// val has no result type, as it is permitted to be any integer type
try expectEqual(@as(u32, 123), s.x);
}
Shell
$ zig test result_type_propagation.zig
1/1 result_type_propagation.test.result type propagates through struct initializer... OK
All 1 tests passed.
This result type information is useful for the aforementioned cast builtins, as well as to
avoid the construction of pre-coercion values, and to avoid the need for explicit type
coercions in some cases. The following table details how some common expressions
propagate result types, where x and y are arbitrary sub-expressions.
const val: T = x - x is a T
var val: T = x - x is a T
val = x - x is a @TypeOf(val)
@as(T, x) - x is a T
&x *T x is a T
.{ .a = x } T x is a std.meta.FieldType(T, .a)
T{ .a = x } - x is a std.meta.FieldType(T, .a)
Expression Parent Result Type Sub-expression Result Type
@Type(x) - x is a std.builtin.Type
@typeInfo(x) - x is a type
x << y - y is a std.math.Log2IntCeil(@TypeOf(x))
Result Locations
In addition to result type information, every expression may be optionally assigned a result
location: a pointer to which the value must be directly written. This system can be used to
prevent intermediate copies when initializing data structures, which can be important for
types which must have a fixed memory address ("pinned" types).
When compiling the simple assignment expression , many languages would create the
x = e
temporary value e on the stack, and then assign it to x , potentially performing a type
coercion in the process. Zig approaches this differently. The expression e is given a result
type matching the type of x , and a result location of &x . For many syntactic forms of e ,
this has no practical impact. However, it can have important semantic effects when working
with more complex syntax forms.
This can sometimes be important when assigning an aggregate value where the
initialization expression depends on the previous value of the aggregate. The easiest way to
demonstrate this is by attempting to swap fields of a struct or array - the following logic
looks sound, but in fact is not:
result_location_interfering_with_swap.zig
const expect = @import("std").testing.expect;
test "attempt to swap array elements with array initializer" {
var arr: [2]u32 = .{ 1, 2 };
arr = .{ arr[1], arr[0] };
// The previous line is equivalent to the following two lines:
// arr[0] = arr[1];
// arr[1] = arr[0];
// So this fails!
try expect(arr[0] == 2); // succeeds
try expect(arr[1] == 1); // fails
}
Shell
$ zig test result_location_interfering_with_swap.zig
1/1 result_location_interfering_with_swap.test.attempt to swap array elements with array initializer.
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/testing.zig:540:14: 0x1038e9f in exp
if (!ok) return error.TestUnexpectedResult;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/result_location_interfering_with_swap.zig:
try expect(arr[1] == 1); // fails
^
0 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/76a3150e40ec585405478691460e89fc/te
The following table details how some common expressions propagate result locations,
where x and y are arbitrary sub-expressions. Note that some expressions cannot provide
meaningful result locations to sub-expressions, even if they themselves have a result
location.
Result
Expression Sub-expression Result Locations
Location
const val: T =
- x has result location &val
x
usingnamespace
usingnamespace is a declaration that mixes all the public declarations of the operand, which
must be a struct, union, enum, or opaque, into the namespace:
test_usingnamespace.zig
test "using std namespace" {
const S = struct {
usingnamespace @import("std");
};
try S.testing.expect(true);
}
Shell
$ zig test test_usingnamespace.zig
1/1 test_usingnamespace.test.using std namespace... OK
All 1 tests passed.
usingnamespace has an important use case when organizing the public API of a file or
package. For example, one might have c.zig with all of the C imports:
c.zig
pub usingnamespace @cImport({
@cInclude("epoxy/gl.h");
@cInclude("GLFW/glfw3.h");
@cDefine("STBI_ONLY_PNG", "");
@cDefine("STBI_NO_STDIO", "");
@cInclude("stb_image.h");
});
The above example demonstrates using pub to qualify the usingnamespace additionally
makes the imported declarations pub . This can be used to forward declarations, giving
precise control over what declarations a given file exposes.
comptime
Zig places importance on the concept of whether an expression is known at compile-time.
There are a few different places this concept is used, and these building blocks are used to
keep the language small, readable, and powerful.
Compile-Time Parameters
compile-time_duck_typing.zig
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);
}
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 .
test_unresolved_comptime_value.zig
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);
_ = result;
}
Shell
$ zig test test_unresolved_comptime_value.zig
docgen_tmp/test_unresolved_comptime_value.zig:9:13: error: unable to resolve comptime value
if (condition) f32 else u64,
^~~~~~~~~
docgen_tmp/test_unresolved_comptime_value.zig:9:13: note: condition in comptime branch must be compti
referenced by:
test.try to pass a runtime type: docgen_tmp/test_unresolved_comptime_value.zig:5:5
remaining reference traces hidden; use '-freference-trace' to see all reference traces
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:
test_comptime_mismatched_type.zig
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);
}
Shell
$ zig test test_comptime_mismatched_type.zig
docgen_tmp/test_comptime_mismatched_type.zig:2:18: error: operator > not allowed for type 'bool'
return if (a > b) a else b;
~~^~~
referenced by:
test.try to compare bools: docgen_tmp/test_comptime_mismatched_type.zig:5:12
remaining reference traces hidden; use '-freference-trace' to see all reference traces
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:
test_comptime_max_with_bool.zig
fn max(comptime T: type, a: T, b: T) T {
if (T == bool) {
return a or b;
} else if (a > b) {
return a;
} else {
return b;
}
}
test "try to compare bools" {
try @import("std").testing.expect(max(bool, false, true) == true);
}
Shell
$ zig test test_comptime_max_with_bool.zig
1/1 test_comptime_max_with_bool.test.try to compare bools... OK
All 1 tests passed.
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:
compiler_generated_function.zig
fn max(a: bool, b: bool) bool {
{
return a or b;
}
}
All the code that dealt with compile-time known values is eliminated and we are left with
only the necessary run-time code to accomplish the task.
This works the same way for switch expressions - they are implicitly inlined when the target
expression is compile-time known.
Compile-Time Variables
In Zig, the programmer can label variables as comptime . This guarantees to the compiler that
every load and store of the variable is performed at compile-time. Any violation of this
results in a compile error.
This combined with the fact that we can inline loops allows us to write a function which is
partially evaluated at compile-time and partially at run-time.
For example:
test_comptime_evaluation.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_comptime_evaluation.zig
1/1 test_comptime_evaluation.test.perform fn... OK
All 1 tests passed.
performFn_1
// From the line:
// expect(performFn('t', 1) == 6);
fn performFn(start_value: i32) i32 {
var result: i32 = start_value;
result = two(result);
result = three(result);
return result;
}
performFn_2
// From the line:
// expect(performFn('o', 0) == 1);
fn performFn(start_value: i32) i32 {
var result: i32 = start_value;
result = one(result);
return result;
}
performFn_3
// From the line:
// expect(performFn('w', 99) == 99);
fn performFn(start_value: i32) i32 {
var result: i32 = start_value;
_ = &result;
return result;
}
Note that this happens even in a debug build. This is not a way to write more optimized
code, but it is a way to make sure that what should happen at compile-time, does happen at
compile-time. This catches more errors and allows expressiveness that in other languages
requires using macros, generated code, or a preprocessor to accomplish.
Compile-Time Expressions
test_comptime_call_extern_function.zig
extern fn exit() noreturn;
test "foo" {
comptime {
exit();
}
}
Shell
$ zig test test_comptime_call_extern_function.zig
docgen_tmp/test_comptime_call_extern_function.zig:5:13: error: comptime call of extern function
exit();
~~~~^~
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.
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.
test_fibonacci_recursion.zig
const expect = @import("std").testing.expect;
test "fibonacci" {
// test fibonacci at run-time
try expect(fibonacci(7) == 13);
Shell
$ zig test test_fibonacci_recursion.zig
1/1 test_fibonacci_recursion.test.fibonacci... OK
All 1 tests passed.
Imagine if we had forgotten the base case of the recursive function and tried to run the
tests:
test_fibonacci_comptime_overflow.zig
const expect = @import("std").testing.expect;
test "fibonacci" {
try comptime expect(fibonacci(7) == 13);
}
Shell
$ zig test test_fibonacci_comptime_overflow.zig
docgen_tmp/test_fibonacci_comptime_overflow.zig:5:28: error: overflow of integer type 'u32' with valu
return fibonacci(index - 1) + fibonacci(index - 2);
~~~~~~^~~
docgen_tmp/test_fibonacci_comptime_overflow.zig:5:21: note: called from here (7 times)
return fibonacci(index - 1) + fibonacci(index - 2);
~~~~~~~~~^~~~~~~~~~~
docgen_tmp/test_fibonacci_comptime_overflow.zig:9:34: note: called from here
try comptime expect(fibonacci(7) == 13);
~~~~~~~~~^~~
The compiler produces an error which is a stack trace from trying to evaluate the function at
compile-time.
Luckily, we used an unsigned integer, and so when we tried to subtract 1 from 0, it triggered
undefined behavior, which is always a compile error if the compiler knows it happened. But
what would have happened if we used a signed integer?
fibonacci_comptime_infinite_recursion.zig
const assert = @import("std").debug.assert;
test "fibonacci" {
try comptime assert(fibonacci(7) == 13);
}
The compiler is supposed to notice that evaluating this function at compile-time took more
than 1000 branches, and thus emits an error and gives up. If the programmer wants to
increase the budget for compile-time computation, they can use a built-in function called
@setEvalBranchQuota to change the default number 1000 to something else.
However, there is a design flaw in the compiler causing it to stack overflow instead of having
the proper behavior here. I'm terribly sorry about that. I hope to get this resolved before
the next release.
What if we fix the base case, but put the wrong value in the expect line?
test_fibonacci_comptime_unreachable.zig
const assert = @import("std").debug.assert;
test "fibonacci" {
try comptime assert(fibonacci(7) == 99999);
}
Shell
$ zig test test_fibonacci_comptime_unreachable.zig
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/debug.zig:403:14: error: reached unr
if (!ok) unreachable; // assertion failure
^~~~~~~~~~~
docgen_tmp/test_fibonacci_comptime_unreachable.zig:9:24: note: called from here
try comptime assert(fibonacci(7) == 99999);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
At container level (outside of any function), all expressions are implicitly comptime
expressions. This means that we can use functions to initialize complex static data. For
example:
test_container-level_comptime_expressions.zig
const first_25_primes = firstNPrimes(25);
const sum_of_first_25_primes = sum(&first_25_primes);
Shell
$ zig test test_container-level_comptime_expressions.zig
1/1 test_container-level_comptime_expressions.test.variable values... OK
All 1 tests passed.
When we compile this program, Zig generates the constants with the answer pre-computed.
Here are the lines from the generated LLVM IR:
@0 = internal unnamed_addr constant [25 x i32] [i32 2, i32 3, i32 5, i32 7, i32 11, i32 13, i32 17, i
@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 comptime capabilities to implement generic data structures without introducing
any special-case syntax.
generic_data_structure.zig
fn List(comptime T: type) type {
return struct {
items: []T,
len: usize,
};
}
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.
anonymous_struct_name.zig
const Node = struct {
next: ?*Node,
name: []const u8,
};
In this example, the Node struct refers to itself. This works because all top level declarations
are order-independent. As long as the compiler can determine the size of the struct, it is
free to refer to itself. In this case, Node refers to itself as a pointer, which has a well-defined
size at compile time, so it works fine.
Putting all of this together, let's see how print works in Zig.
print.zig
const print = @import("std").debug.print;
Shell
$ zig build-exe print.zig
$ ./print
here is a string: 'foobar' here is a number: 1234
Let's crack open the implementation of this and see how it works:
poc_print_fn.zig
const Writer = struct {
/// Calls print and then flushes the buffer.
pub fn print(self: *Writer, comptime format: []const u8, args: anytype) anyerror!void {
const State = enum {
start,
open_brace,
close_brace,
};
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:
printValueis a function that takes a parameter of any type, and does different things
depending on the type:
poc_printValue_fn.zig
const Writer = struct {
pub fn printValue(self: *Writer, value: anytype) !void {
switch (@typeInfo(@TypeOf(value))) {
.Int => {
return self.writeInt(value);
},
.Float => {
return self.writeFloat(value);
},
.Pointer => {
return self.write(value);
},
else => {
@compileError("Unable to print type '" ++ @typeName(@TypeOf(value)) ++ "'");
},
}
}
test_print_too_many_args.zig
const print = @import("std").debug.print;
Shell
$ zig test test_print_too_many_args.zig
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/fmt.zig:203:18: error: unused argume
'
1 => @compileError("unused argument in '" ++ fmt ++ "'"),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
print__anon_2564: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/io/Writer.zig:
print: /home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/io.zig:324:47
remaining reference traces hidden; use '-freference-trace' to see all reference traces
Zig gives programmers the tools needed to protect themselves against their own mistakes.
Zig doesn't care whether the format argument is a string literal, only that it is a compile-
time known value that can be coerced to a []const u8 :
print_comptime-known_format.zig
const print = @import("std").debug.print;
Shell
$ zig build-exe print_comptime-known_format.zig
$ ./print_comptime-known_format
here is a string: 'foobar' here is a number: 1234
Zig does not special case string formatting in the compiler and instead exposes enough
power to accomplish this task in userland. It does so without introducing another language
on top of Zig, such as a macro language or a preprocessor language. It's Zig all the way
down.
See also:
• inline while
• inline for
Assembly
For some use cases, it may be necessary to directly control the machine code generated by
Zig programs, rather than relying on Zig's code generation. For these cases, one can use
inline assembly. Here is an example of implementing Hello, World on x86_64 Linux using
inline assembly:
inline_assembly.zig
pub fn main() noreturn {
const msg = "hello world\n";
_ = syscall3(SYS_write, STDOUT_FILENO, @intFromPtr(msg), msg.len);
_ = syscall1(SYS_exit, 0);
unreachable;
}
pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize),
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
: "rcx", "r11"
);
}
Shell
$ zig build-exe inline_assembly.zig -target x86_64-linux
$ ./inline_assembly
hello world
Some day Zig may have its own assembler. This would allow it to integrate more seamlessly
into the language, as well as be compatible with the popular NASM syntax. This
documentation section will be updated before 1.0.0 is released, with a conclusive statement
about the status of AT&T vs Intel/NASM syntax.
Output Constraints
Output constraints are still considered to be unstable in Zig, and so LLVM documentation
and GCC documentation must be used to understand the semantics.
Note that some breaking changes to output constraints are planned with issue #215.
Input Constraints
Input constraints are still considered to be unstable in Zig, and so LLVM documentation and
GCC documentation must be used to understand the semantics.
Note that some breaking changes to input constraints are planned with issue #215.
Clobbers
Clobbers are the set of registers whose values will not be preserved by the execution of the
assembly code. These do not include output or input registers. The special clobber value of
"memory" means that the assembly causes writes to arbitrary undeclared memory locations -
not only the memory pointed to by a declared indirect output.
Failure to declare the full set of clobbers for a given inline assembly expression is unchecked
Undefined Behavior.
Global Assembly
When an assembly expression occurs in a container level comptime block, this is global
assembly.
This kind of assembly has different rules than inline assembly. First, volatile is not valid
because all global assembly is unconditionally included. Second, there are no inputs,
outputs, or clobbers. All global assembly is concatenated verbatim into one long string and
assembled together. There are no template substitution rules regarding % as there are in
inline assembly expressions.
test_global_assembly.zig
const std = @import("std");
const expect = std.testing.expect;
comptime {
asm (
\\.global my_func;
\\.type my_func, @function;
\\my_func:
\\ lea (%rdi,%rsi,1),%eax
\\ retq
);
}
Shell
$ zig test test_global_assembly.zig -target x86_64-linux
1/1 test_global_assembly.test.global assembly... OK
All 1 tests passed.
Atomics
TODO: @fence()
See also:
• @atomicLoad
• @atomicStore
• @atomicRmw
• @fence
• @cmpxchgWeak
• @cmpxchgStrong
Async Functions
Async functions regressed with the release of 0.11.0. Their future in the Zig language is
unclear due to multiple unsolved problems:
These problems are surmountable, but it will take time. The Zig team is currently focused on
other priorities.
Builtin Functions
Builtin functions are provided by the compiler and are prefixed with @ . The comptime
keyword on a parameter means that the parameter must be known at compile time.
@addrSpaceCast
Converts a pointer from one address space to another. The new address space is inferred
based on the result type. Depending on the current target and address spaces, this cast may
be a no-op, a complex operation, or illegal. If the cast is legal, then the resulting pointer
points to the same memory location as the pointer operand. It is always valid to cast a
pointer between the same address spaces.
@addWithOverflow
Performs a + b and returns a tuple with the result and a possible overflow bit.
@alignCast
ptr can be *T , ?*T , or []T . Changes the alignment of a pointer. The alignment to use is
inferred based on the result type.
A pointer alignment safety check is added to the generated code to make sure the pointer is
aligned as promised.
@alignOf
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.
See also:
• Alignment
@as
Performs Type Coercion. This cast is allowed when the conversion is unambiguous and safe,
and is the preferred way to convert between types, whenever possible.
@atomicLoad
This builtin function atomically dereferences a pointer to a T and returns the value.
See also:
• @atomicStore
• @atomicRmw
• @fence
• @cmpxchgWeak
• @cmpxchgStrong
@atomicRmw
@atomicRmw(comptime T: type, ptr: *T, comptime op: AtomicRmwOp, operand: T, comptime ordering: Atomic
This builtin function dereferences a pointer to a T and atomically modifies the value and
returns the previous value.
See also:
• @atomicStore
• @atomicLoad
• @fence
• @cmpxchgWeak
• @cmpxchgStrong
@atomicStore
This builtin function dereferences a pointer to a T and atomically stores the given value.
See also:
• @atomicLoad
• @atomicRmw
• @fence
• @cmpxchgWeak
• @cmpxchgStrong
@bitCast
Converts a value of one type to another type. The return type is the inferred result type.
Asserts that @typeInfo(DestType) != .Pointer . Use @ptrCast or @ptrFromInt if you need this.
Works at compile-time if value is known at compile time. It's a compile error to bitcast a
value of undefined layout; this means that, besides the restriction from types which possess
dedicated casting builtins (enums, pointers, error sets), bare structs, error unions, slices,
optionals, and any other type without a well-defined memory layout, also cannot be used in
this operation.
@bitOffsetOf
@bitOffsetOf(comptime T: type, comptime field_name: []const u8) comptime_int
For non packed structs, this will always be divisible by 8 . For packed structs, non-byte-
aligned fields will share a byte offset, but they will have different bit offsets.
See also:
• @offsetOf
@bitSizeOf
This function returns the number of bits it takes to store T in memory if the type were a
field in a packed struct/union. The result is a target-specific compile time constant.
This function measures the size at runtime. For types that are disallowed at runtime, such as
comptime_int and type , the result is 0 .
See also:
• @sizeOf
• @typeInfo
@breakpoint
@breakpoint() void
This function inserts a platform-specific debug trap instruction which causes debuggers to
break there. Unlike for @trap() , execution may continue after this point if the program is
resumed.
See also:
• @trap
@mulAdd
@mulAdd(comptime T: type, a: T, b: T, c: T) T
Fused multiply-add, similar to (a * b) + c , except only rounds once, and is thus more
accurate.
Supports Floats and Vectors of floats.
@byteSwap
@byteSwap(operand: anytype) T
@TypeOf(operand) must be an integer type or an integer vector type with bit count evenly
divisible by 8.
Swaps the byte order of the integer. This converts a big endian integer to a little endian
integer, and converts a little endian integer to a big endian integer.
Note that for the purposes of memory layout with respect to endianness, the integer type
should be related to the number of bytes reported by @sizeOf bytes. This is demonstrated
with u24 . @sizeOf(u24) == 4 , which means that a u24 stored in memory takes 4 bytes, and
those 4 bytes are what are swapped on a little vs big endian system. On the other hand, if T
is specified to be u24 , then only 3 bytes are reversed.
@bitReverse
@bitReverse(integer: anytype) T
Reverses the bitpattern of an integer value, including the sign bit if applicable.
@offsetOf
See also:
• @bitOffsetOf
@call
Calls a function, in the same way that invoking an expression with parentheses does:
test_call_builtin.zig
const expect = @import("std").testing.expect;
Shell
$ zig test test_call_builtin.zig
1/1 test_call_builtin.test.noinline function call... OK
All 1 tests passed.
@call allows more flexibility than normal function call syntax does. The CallModifier enum
is reproduced here:
builtin.CallModifier struct.zig
pub const CallModifier = enum {
/// Equivalent to function call syntax.
auto,
/// Prevents tail call optimization. This guarantees that the return
/// address will point to the callsite, as opposed to the callsite's
/// callsite. If the call is otherwise required to be tail-called
/// or inlined, a compile error is emitted instead.
never_tail,
/// Guarantees that the call will not be inlined. If the call is
/// otherwise required to be inlined, a compile error is emitted instead.
never_inline,
/// Asserts that the function call will not suspend. This allows a
/// non-async function to call an async function.
no_async,
/// Guarantees that the call will be generated with tail call optimization.
/// If this is not possible, a compile error is emitted instead.
always_tail,
@cDefine
#define _GNU_SOURCE
@cDefine("_GNU_SOURCE", {})
See also:
@cImport
@cImport(expression) type
This function parses C code and imports the functions, types, variables, and compatible
macro definitions into a new empty struct type, and then returns that type.
expression is interpreted at compile time. The builtin functions @cInclude , @cDefine , and
@cUndef work within this expression, appending to a temporary buffer which is then parsed
as C code.
Usually you should only have one @cImport in your entire application, because it saves the
compiler from invoking clang multiple times, and prevents inline functions from being
duplicated.
• To avoid a symbol collision, for example if foo.h and bar.h both #define
CONNECTION_COUNT
See also:
@cInclude
@cInclude(comptime path: []const u8) void
See also:
@clz
See also:
• @ctz
• @popCount
@cmpxchgStrong
not_atomic_cmpxchgStrong.zig
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;
}
}
If you are using cmpxchg in a retry loop, @cmpxchgWeak is the better choice, because it can
be implemented more efficiently in machine instructions.
See also:
• @atomicStore
• @atomicLoad
• @atomicRmw
• @fence
• @cmpxchgWeak
@cmpxchgWeak
cmpxchgWeakButNotAtomic
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;
}
}
If you are using cmpxchg in a retry loop, the sporadic failure will be no problem, and
cmpxchgWeak is the better choice, because it can be implemented more efficiently in machine
• @atomicStore
• @atomicLoad
• @atomicRmw
• @fence
• @cmpxchgStrong
@compileError
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
test_compileLog_builtin.zig
const print = @import("std").debug.print;
test "main" {
@compileLog("comptime in main");
Shell
$ zig test test_compileLog_builtin.zig
docgen_tmp/test_compileLog_builtin.zig:11:5: error: found compile log statement
@compileLog("comptime in main");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
docgen_tmp/test_compileLog_builtin.zig:5:5: note: also here
@compileLog("comptime val1 = ", val1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compile Log Output:
@as(*const [16:0]u8, "comptime in main")
@as(*const [16:0]u8, "comptime val1 = "), @as(i32, 99)
@constCast
@ctz
See also:
• @clz
• @popCount
@cUndef
See also:
See also:
• @cVaCopy
• @cVaEnd
• @cVaStart
@cVaCopy
See also:
• @cVaArg
• @cVaEnd
• @cVaStart
@cVaEnd
See also:
• @cVaArg
• @cVaCopy
• @cVaStart
@cVaStart
@cVaStart() std.builtin.VaList
See also:
• @cVaArg
• @cVaCopy
• @cVaEnd
@divExact
@divExact(numerator: T, denominator: T) T
denominator == numerator .
• @divExact(6, 3) == 2
• @divExact(a, b) * b == a
See also:
• @divTrunc
• @divFloor
@divFloor
@divFloor(numerator: T, denominator: T) T
Floored division. Rounds toward negative infinity. For unsigned integers it is the same as
numerator / denominator . Caller guarantees denominator != 0 and !(@typeInfo(T) == .Int
• @divFloor(-5, 3) == -2
• (@divFloor(a, b) * b) + @mod(a, b) == a
See also:
• @divTrunc
• @divExact
@divTrunc
@divTrunc(numerator: T, denominator: T) T
Truncated division. Rounds toward zero. For unsigned integers it is the same as numerator /
denominator . Caller guarantees denominator != 0 and !(@typeInfo(T) == .Int and T.is_signed
• @divTrunc(-5, 3) == -1
• (@divTrunc(a, b) * b) + @rem(a, b) == a
See also:
• @divFloor
• @divExact
@embedFile
This function returns a compile time constant pointer to null-terminated, fixed-size array
with length equal to the byte count of the file given by path . The contents of the array are
the contents of the file. This is equivalent to a string literal with the file contents.
See also:
• @import
@enumFromInt
Converts an integer into an enum value. The return type is the inferred result type.
Attempting to convert an integer which represents no value in the chosen enum type
invokes safety-checked Undefined Behavior.
See also:
• @intFromEnum
@errorFromInt
Converts from the integer representation of an error into The Global Error Set type.
Attempting to convert an integer that does not correspond to any error results in safety-
protected Undefined Behavior.
See also:
• @intFromError
@errorName
This function returns the string representation of an error. The string representation of
error.OutOfMem 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
@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.
@errorCast
Converts an error set or error union value from one error set to another error set. The
return type is the inferred result type. Attempting to convert an error which is not in the
destination error set results in safety-protected Undefined Behavior.
@export
This builtin can be called from a comptime block to conditionally export symbols. When
declaration is a function with the C calling convention and options.linkage is Strong , this is
export_builtin.zig
comptime {
@export(internalName, .{ .name = "foo", .linkage = .strong });
}
Shell
$ zig build-obj export_builtin.zig
export_builtin_equivalent_code.zig
export fn foo() void {}
Shell
$ zig build-obj export_builtin_equivalent_code.zig
Note that even when using export , the @"foo" syntax for identifiers can be used to choose
any string for the symbol name:
export_any_symbol_name.zig
export fn @"A function name that is a complete sentence."() void {}
Shell
$ zig build-obj export_any_symbol_name.zig
When looking at the resulting object, you can see the symbol is used verbatim:
See also:
• Exporting a C Library
@extern
Creates a reference to an external symbol in the output object file. T must be a pointer
type.
See also:
• @export
@fence
See also:
• @atomicStore
• @atomicLoad
• @atomicRmw
• @cmpxchgWeak
• @cmpxchgStrong
@field
Performs field access by a compile-time string. Works on both fields and declarations.
test_field_builtin.zig
const std = @import("std");
@field(p, "x") = 4;
@field(p, "y") = @field(p, "x") + 1;
@field(Point, "z") = 2;
try expect(@field(Point, "z") == 2);
}
Shell
$ zig test test_field_builtin.zig
1/2 test_field_builtin.test.field access by string... OK
2/2 test_field_builtin.test.decl access by string... OK
All 2 tests passed.
@fieldParentPtr
@fieldParentPtr(comptime field_name: []const u8, field_ptr: *T) anytype
@floatCast
Convert from one float type to another. This cast is safe, but may cause the numeric value to
lose precision. The return type is the inferred result type.
@floatFromInt
Converts an integer to the closest floating point representation. The return type is the
inferred result type. To convert the other way, use @intFromFloat. This operation is legal
for all values of all integer types.
@frameAddress
@frameAddress() usize
This function returns the base pointer of the current stack frame.
The implications of this are target-specific and not consistent across all platforms. The
frame address may not be available in release mode due to aggressive optimizations.
@hasDecl
test_hasDecl_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
test "@hasDecl" {
try expect(@hasDecl(Foo, "blah"));
// Even though `hi` is private, @hasDecl returns true because this test is
// in the same file scope as Foo. It would return false if Foo was declared
// in a different file.
try expect(@hasDecl(Foo, "hi"));
Shell
$ zig test test_hasDecl_builtin.zig
1/1 test_hasDecl_builtin.test.@hasDecl... OK
All 1 tests passed.
See also:
• @hasField
@hasField
See also:
• @hasDecl
@import
This function finds a zig file corresponding to path and adds it to the build, if it is not
already added.
Zig source files are implicitly structs, with a name equal to the file's basename with the
extension truncated. @import returns the struct type corresponding to the file.
Declarations which have the pub keyword may be referenced from a different source file
than the one they are declared in.
path can be a relative path or it can be the name of a package. If it is a relative path, it is
relative to the file that contains the @import function call.
See also:
• Compile Variables
• @embedFile
@inComptime
@inComptime() bool
Returns whether the builtin was run in a comptime context. The result is a compile-time
constant.
See also:
• comptime
@intCast
Converts an integer to another integer while keeping the same numerical value. The return
type is the inferred result type. Attempting to convert a number which is out of range of the
destination type results in safety-protected Undefined Behavior.
test_intCast_builtin.zig
test "integer cast panic" {
var a: u16 = 0xabcd; // runtime-known
_ = &a;
const b: u8 = @intCast(a);
_ = b;
}
Shell
$ zig test test_intCast_builtin.zig
1/1 test_intCast_builtin.test.integer cast panic... thread 990176 panic: integer cast truncated bits
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_intCast_builtin.zig:4:19: 0x1038e82 i
const b: u8 = @intCast(a);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x10442
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103a26
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x10393a9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1038f11 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/2692afe223c7b62e0d699882f8c4b15a/te
To truncate the significant bits of a number out of range of the destination type, use
@truncate.
@intFromBool
@intFromBool(value: bool) u1
@intFromEnum
Converts an enumeration value into its integer tag type. When a tagged union is passed, the
tag value is used as the enumeration value.
If there is only one possible enum value, the result is a comptime_int known at comptime.
See also:
• @enumFromInt
@intFromError
• @errorFromInt
@intFromFloat
Converts the integer part of a floating point number to the inferred result type.
If the integer part of the floating point number cannot fit in the destination type, it invokes
safety-checked Undefined Behavior.
See also:
• @floatFromInt
@intFromPtr
Converts value to a usize which is the address of the pointer. value can be *T or ?*T .
@max
@max(a: T, b: T) T
Returns the maximum value of a and b . This builtin accepts integers, floats, and vectors of
either. In the latter case, the operation is performed element wise.
NaNs are handled as follows: if one of the operands of a (pairwise) operation is NaN, the
other operand is returned. If both operands are NaN, NaN is returned.
See also:
• @min
• Vectors
@memcpy
source must be a slice, a pointer to an array, or a many-item pointer. It may have any
alignment, and it may have any element type.
The source element type must support Type Coercion into the dest element type. The
element types may have different ABI size, however, that may incur a performance penalty.
Similar to for loops, at least one of source and dest must provide a length, and if two
lengths are provided, they must be equal.
@memset
dest must be a mutable slice or a mutable pointer to an array. It may have any alignment,
and it may have any element type.
For securely zeroing out sensitive contents from memory, you should use
std.crypto.utils.secureZero
@min
@min(a: T, b: T) T
Returns the minimum value of a and b . This builtin accepts integers, floats, and vectors of
either. In the latter case, the operation is performed element wise.
NaNs are handled as follows: if one of the operands of a (pairwise) operation is NaN, the
other operand is returned. If both operands are NaN, NaN is returned.
See also:
• @max
• Vectors
@wasmMemorySize
This function returns the size of the Wasm memory identified by index as an unsigned value
in units of Wasm pages. Note that each Wasm page is 64KB in size.
This function is a low level intrinsic with no safety mechanisms usually useful for allocator
designers targeting Wasm. So unless you are writing a new allocator from scratch, you
should use something like @import("std").heap.WasmPageAllocator .
See also:
• @wasmMemoryGrow
@wasmMemoryGrow
This function increases the size of the Wasm memory identified by index by delta in units
of unsigned number of Wasm pages. Note that each Wasm page is 64KB in size. On success,
returns previous memory size; on failure, if the allocation fails, returns -1.
This function is a low level intrinsic with no safety mechanisms usually useful for allocator
designers targeting Wasm. So unless you are writing a new allocator from scratch, you
should use something like @import("std").heap.WasmPageAllocator .
test_wasmMemoryGrow_builtin.zig
const std = @import("std");
const native_arch = @import("builtin").target.cpu.arch;
const expect = std.testing.expect;
test "@wasmMemoryGrow" {
if (native_arch != .wasm32) return error.SkipZigTest;
Shell
$ zig test test_wasmMemoryGrow_builtin.zig
1/1 test_wasmMemoryGrow_builtin.test.@wasmMemoryGrow... SKIP
0 passed; 1 skipped; 0 failed.
See also:
• @wasmMemorySize
@mod
@mod(numerator: T, denominator: T) T
Modulus division. For unsigned integers this is the same as numerator % denominator . Caller
guarantees denominator > 0 , otherwise the operation will result in a Remainder Division by
Zero when runtime safety checks are enabled.
• @mod(-5, 3) == 1
• (@divFloor(a, b) * b) + @mod(a, b) == a
See also:
• @rem
@mulWithOverflow
Performs a * b and returns a tuple with the result and a possible overflow bit.
@panic
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, the
• From library code, calling the programmer's panic function if they exposed one in the
root source file.
• When mixing C and Zig code, calling the canonical panic implementation across
multiple .o files.
See also:
@popCount
See also:
• @ctz
• @clz
@prefetch
This builtin tells the compiler to emit a prefetch instruction if supported by the target CPU.
If the target CPU does not support the requested prefetch instruction, this builtin is a no-
op. This function has no effect on the behavior of the program, only on the performance
characteristics.
The ptrargument may be any pointer type and determines the memory address to
prefetch. This function does not dereference the pointer, it is perfectly legal to pass a
pointer to invalid memory to this function and no illegal behavior will result.
@ptrCast
Converts a pointer of one type to a pointer of another type. The return type is the inferred
result type.
Optional Pointers are allowed. Casting an optional pointer which is null to a non-optional
pointer invokes safety-checked Undefined Behavior.
@ptrFromInt
Converts an integer to a pointer. The return type is the inferred result type. To convert the
other way, use @intFromPtr. Casting an address of 0 to a destination type which in not
optional and does not have the allowzero attribute will result in a Pointer Cast Invalid Null
panic when runtime safety checks are enabled.
If the destination pointer type does not allow address zero and address is zero, this invokes
safety-checked Undefined Behavior.
@rem
@rem(numerator: T, denominator: T) T
Remainder division. For unsigned integers this is the same as numerator % denominator . Caller
guarantees denominator > 0 , otherwise the operation will result in a Remainder Division by
Zero when runtime safety checks are enabled.
• @rem(-5, 3) == -2
• (@divTrunc(a, b) * b) + @rem(a, b) == a
See also:
• @mod
@returnAddress
@returnAddress() usize
This function returns the address of the next machine code instruction that will be executed
when the current function returns.
The implications of this are target-specific and not consistent across all platforms.
This function is only valid within function scope. If the function gets inlined into a calling
function, the returned address will apply to the calling function.
@select
See also:
• Vectors
@setAlignStack
@setAlignStack(comptime alignment: u29) void
Ensures that a function will have a stack alignment of at least alignment bytes.
@setCold
Tells the optimizer that the current function is (or is not) rarely called. This function is only
valid within function scope.
@setEvalBranchQuota
Increase 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:
test_without_setEvalBranchQuota_builtin.zig
test "foo" {
comptime {
var i = 0;
while (i < 1001) : (i += 1) {}
}
}
Shell
$ zig test test_without_setEvalBranchQuota_builtin.zig
docgen_tmp/test_without_setEvalBranchQuota_builtin.zig:4:9: error: evaluation exceeded 1000 backwards
while (i < 1001) : (i += 1) {}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
docgen_tmp/test_without_setEvalBranchQuota_builtin.zig:4:9: note: use @setEvalBranchQuota() to raise
test_setEvalBranchQuota_builtin.zig
test "foo" {
comptime {
@setEvalBranchQuota(1001);
var i = 0;
while (i < 1001) : (i += 1) {}
}
}
Shell
$ zig test test_setEvalBranchQuota_builtin.zig
1/1 test_setEvalBranchQuota_builtin.test.foo... OK
All 1 tests passed.
See also:
• comptime
@setFloatMode
Changes the current scope's rules about how floating point operations are defined.
The floating point mode is inherited by child scopes, and can be overridden in any scope.
You can set the floating point mode in a struct or module scope by using a comptime block.
See also:
@setRuntimeSafety
Sets whether runtime safety checks are enabled for the scope that contains the function
call.
test_setRuntimeSafety_builtin.zig
test "@setRuntimeSafety" {
// The builtin applies to the scope that it is called in. So here, integer overflow
// will not be caught in ReleaseFast and ReleaseSmall modes:
// var x: u8 = 255;
// x += 1; // undefined behavior in ReleaseFast/ReleaseSmall modes.
{
// However this block has safety enabled, so safety checks happen here,
// even in ReleaseFast and ReleaseSmall modes.
@setRuntimeSafety(true);
var x: u8 = 255;
x += 1;
{
// The value can be overridden at any scope. So here integer overflow
// would not be caught in any build mode.
@setRuntimeSafety(false);
// var x: u8 = 255;
// x += 1; // undefined behavior in all build modes.
}
}
}
Shell
$ zig test test_setRuntimeSafety_builtin.zig -OReleaseFast
1/1 test_setRuntimeSafety_builtin.test.@setRuntimeSafety... thread 990272 panic: integer overflow
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_setRuntimeSafety_builtin.zig:11:11: 0
x += 1;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x100a8
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x10099a6 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10098e1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/7bf3eca22a4fdd72525ef07184c18dc9/te
@shlExact
Performs the left shift operation ( << ). For unsigned integers, the result is undefined if any 1
bits are shifted out. For signed integers, the result is undefined if any bits that disagree with
the resultant sign bit are shifted out.
comptime_int is modeled as an integer with an infinite number of bits, meaning that in such
case, @shlExact always produces a result and cannot produce a compile error.
See also:
• @shrExact
• @shlWithOverflow
@shlWithOverflow
Performs a << b and returns a tuple with the result and a possible overflow bit.
See also:
• @shlExact
• @shrExact
@shrExact
Performs the right shift operation ( >> ). Caller guarantees that the shift will not shift any 1
bits out.
See also:
• @shlExact
• @shlWithOverflow
@shuffle
Each element in mask selects an element from either a or b . Positive numbers select from
a starting at 0. Negative values select from b , starting at -1 and going down. It is
recommended to use the ~ operator for indexes from b so that both indexes can start
from 0 (i.e. ~@as(i32, 0) is -1 ).
For each element of mask , if it or the selected value from a or b is undefined , then the
resulting element is undefined .
a_len and b_len may differ in length. Out-of-bounds element indexes in mask result in
compile errors.
E must be an integer, float, pointer, or bool . The mask may be any vector length, and its
length determines the result length.
test_shuffle_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_shuffle_builtin.zig
1/1 test_shuffle_builtin.test.vector @shuffle... OK
All 1 tests passed.
See also:
• Vectors
@sizeOf
This function returns the number of bytes it takes to store T in memory. The result is a
target-specific compile time constant.
This size may contain padding bytes. If there were two consecutive T in memory, the
padding would be the offset in bytes between element at index 0 and the element at index
1. For integer, consider whether you want to use @sizeOf(T) or @typeInfo(T).Int.bits .
This function measures the size at runtime. For types that are disallowed at runtime, such as
comptime_int and type , the result is 0 .
See also:
• @bitSizeOf
• @typeInfo
@splat
Produces a vector where each element is the value scalar . The return type and thus the
length of the vector is inferred.
test_splat_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_splat_builtin.zig
1/1 test_splat_builtin.test.vector @splat... OK
All 1 tests passed.
See also:
• Vectors
• @shuffle
@reduce
Transforms a vector into a scalar value (of type E ) by performing a sequential horizontal
reduction of its elements using the specified operator op .
Note that .Add and .Mul reductions on integral types are wrapping; when applied on
floating point types the operation associativity is preserved, unless the float mode is set to
Optimized .
test_reduce_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_reduce_builtin.zig
1/1 test_reduce_builtin.test.vector @reduce... OK
All 1 tests passed.
See also:
• Vectors
• @setFloatMode
@src
@src() std.builtin.SourceLocation
Returns a SourceLocation struct representing the function's name and location in the source
code. This must be called in a function.
test_src_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
test "@src" {
try doTheTest();
}
fn doTheTest() !void {
const src = @src();
Shell
$ zig test test_src_builtin.zig
1/1 test_src_builtin.test.@src... OK
All 1 tests passed.
@sqrt
Performs the square root of a floating point number. Uses a dedicated hardware instruction
when available.
Supports Floats and Vectors of floats.
@sin
@cos
@tan
@exp
@exp2
Returns the natural logarithm of a floating point number. Uses a dedicated hardware
instruction when available.
@log2
Returns the logarithm to the base 2 of a floating point number. Uses a dedicated hardware
instruction when available.
@log10
Returns the logarithm to the base 10 of a floating point number. Uses a dedicated hardware
instruction when available.
@abs
Returns the absolute value of an integer or a floating point number. Uses a dedicated
hardware instruction when available. The return type is always an unsigned integer of the
same bit width as the operand if the operand is an integer. Unsigned integer operands are
supported. The builtin cannot overflow for signed integer operands.
@floor
Returns the largest integral value not greater than the given floating point number. Uses a
dedicated hardware instruction when available.
Returns the smallest integral value not less than the given floating point number. Uses a
dedicated hardware instruction when available.
@trunc
Rounds the given floating point number to an integer, towards zero. Uses a dedicated
hardware instruction when available.
@round
Rounds the given floating point number to an integer, away from zero. Uses a dedicated
hardware instruction when available.
@subWithOverflow
Performs a - b and returns a tuple with the result and a possible overflow bit.
@tagName
Converts an enum value or union value to a string literal representing the name.
If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-
checked Undefined Behavior.
@This
@This() type
Returns the innermost struct, enum, or union that this function call is inside. This can be
useful for an anonymous struct that needs to refer to itself:
test_this_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
test "@This()" {
var items = [_]i32{ 1, 2, 3, 4 };
const list = List(i32){ .items = items[0..] };
try expect(list.length() == 4);
}
items: []T,
Shell
$ zig test test_this_builtin.zig
1/1 test_this_builtin.test.@This()... OK
All 1 tests passed.
When @This() is used at file scope, it returns a reference to the struct that corresponds to
the current file.
@trap
@trap() noreturn
This function inserts a platform-specific trap/jam instruction which can be used to exit the
program abnormally. This may be implemented by explicitly emitting an invalid instruction
which may cause an illegal instruction exception of some sort. Unlike for @breakpoint() ,
execution does not continue after this point.
See also:
• @breakpoint
@truncate
This function truncates bits from an integer type, resulting in a smaller or same-sized
integer type. The return type is the inferred result type.
This function always truncates the significant bits of the integer, regardless of endianness
on the target platform.
Calling @truncate on a number out of range of the destination type is well defined and
working code:
test_truncate_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_truncate_builtin.zig
1/1 test_truncate_builtin.test.integer truncation... OK
All 1 tests passed.
@Type
This function is the inverse of @typeInfo. It reifies type information into a type .
• type
• noreturn
• void
• bool
• comptime_float
• @TypeOf(undefined)
• @TypeOf(null)
• Arrays
• Optionals
• Error Set Type
• Error Union Type
• Vectors
• opaque
• anyframe
• struct
• enum
• Enum Literals
• union
• Functions
@typeInfo
Type information of structs, unions, enums, and error sets has fields which are guaranteed
to be in the same order as appearance in the source file.
Type information of structs, unions, enums, and opaques has declarations, which are also
guaranteed to be in the same order as appearance in the source file.
@typeName
@TypeOf
@TypeOf(...) type
@TypeOfis a special builtin function that takes any (nonzero) number of expressions as
parameters and returns the type of the result, using Peer Type Resolution.
The expressions are evaluated, however they are guaranteed to have no runtime side-
effects:
test_TypeOf_builtin.zig
const std = @import("std");
const expect = std.testing.expect;
Shell
$ zig test test_TypeOf_builtin.zig
1/1 test_TypeOf_builtin.test.no runtime side effects... OK
All 1 tests passed.
@unionInit
This is the same thing as union initialization syntax, except that the field name is a
comptime-known value rather than an identifier token.
@Vector
Creates Vectors.
@volatileCast
@workGroupId
Returns the index of the work group in the current kernel invocation in dimension
dimension .
@workGroupSize
Returns the number of work items that a work group has in dimension dimension .
@workItemId
Build Mode
Zig has four build modes:
• Debug (default)
• ReleaseFast
• ReleaseSafe
• ReleaseSmall
build.zig
const std = @import("std");
-Doptimize=Debug
Debug
Shell
$ zig build-exe example.zig
Shell
$ zig build-exe example.zig -O ReleaseFast
ReleaseSafe
Shell
$ zig build-exe example.zig -O ReleaseSafe
ReleaseSmall
Shell
$ zig build-exe example.zig -O ReleaseSmall
See also:
• Compile Variables
• Zig Build System
• Undefined Behavior
• All Thread Local Variables are treated as regular Container Level Variables.
• The overhead of Async Functions becomes equivalent to function call overhead.
• The @import("builtin").single_threaded becomes true and therefore various userland
APIs which read this variable become more efficient. For example std.Mutex becomes
an empty data structure and all of its functions become no-ops.
Undefined Behavior
Zig has many instances of undefined behavior. If undefined behavior is detected at compile-
time, Zig emits a compile error and refuses to continue. Most undefined behavior that
cannot be detected at compile-time can be detected at runtime. In these cases, Zig has
safety checks. Safety checks can be disabled on a per-block basis with @setRuntimeSafety.
The ReleaseFast and ReleaseSmall build modes disable all safety checks (except where
overridden by @setRuntimeSafety) in order to facilitate optimizations.
When a safety check fails, Zig crashes with a stack trace, like this:
test_undefined_behavior.zig
test "safety check" {
unreachable;
}
Shell
$ zig test test_undefined_behavior.zig
1/1 test_undefined_behavior.test.safety check... thread 990514 panic: reached unreachable code
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/test_undefined_behavior.zig:2:5: 0x1038e50
unreachable;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:158:25: 0x10442
if (test_fn.func()) |_| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/compiler/test_runner.zig:35:28: 0x103a23
return mainTerminal();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1039379 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1038ee1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
/home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o/2b91ce43fc94ee5453af2c9e105375fc/te
At compile-time:
test_comptime_reaching_unreachable.zig
comptime {
assert(false);
}
fn assert(ok: bool) void {
if (!ok) unreachable; // assertion failure
}
Shell
$ zig test test_comptime_reaching_unreachable.zig
docgen_tmp/test_comptime_reaching_unreachable.zig:5:14: error: reached unreachable code
if (!ok) unreachable; // assertion failure
^~~~~~~~~~~
docgen_tmp/test_comptime_reaching_unreachable.zig:2:11: note: called from here
assert(false);
~~~~~~^~~~~~~
At runtime:
runtime_reaching_unreachable.zig
const std = @import("std");
Shell
$ zig build-exe runtime_reaching_unreachable.zig
$ ./runtime_reaching_unreachable
thread 990554 panic: reached unreachable code
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/debug.zig:403:14: 0x10357fd in asser
if (!ok) unreachable; // assertion failure
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_reaching_unreachable.zig:4:21: 0x1
std.debug.assert(false);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033469 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1032fd1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_index_out_of_bounds.zig
comptime {
const array: [5]u8 = "hello".*;
const garbage = array[5];
_ = garbage;
}
Shell
$ zig test test_comptime_index_out_of_bounds.zig
docgen_tmp/test_comptime_index_out_of_bounds.zig:3:27: error: index 5 outside array of length 5
const garbage = array[5];
^
At runtime:
runtime_index_out_of_bounds.zig
pub fn main() void {
const x = foo("hello");
_ = x;
}
Shell
$ zig build-exe runtime_index_out_of_bounds.zig
$ ./runtime_index_out_of_bounds
thread 990594 panic: index out of bounds: index 5, len 5
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_index_out_of_bounds.zig:7:13: 0x10
return x[5];
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_index_out_of_bounds.zig:2:18: 0x10
const x = foo("hello");
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033489 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1032ff1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_invalid_cast.zig
comptime {
const value: i32 = -1;
const unsigned: u32 = @intCast(value);
_ = unsigned;
}
Shell
$ zig test test_comptime_invalid_cast.zig
docgen_tmp/test_comptime_invalid_cast.zig:3:36: error: type 'u32' cannot represent integer value '-1'
const unsigned: u32 = @intCast(value);
^~~~~
At runtime:
runtime_invalid_cast.zig
const std = @import("std");
Shell
$ zig build-exe runtime_invalid_cast.zig
$ ./runtime_invalid_cast
thread 990635 panic: attempt to cast negative value to unsigned integer
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_cast.zig:6:27: 0x1033d22 i
const unsigned: u32 = @intCast(value);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033589 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10330f1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_invalid_cast_truncate.zig
comptime {
const spartan_count: u16 = 300;
const byte: u8 = @intCast(spartan_count);
_ = byte;
}
Shell
$ zig test test_comptime_invalid_cast_truncate.zig
docgen_tmp/test_comptime_invalid_cast_truncate.zig:3:31: error: type 'u8' cannot represent integer va
const byte: u8 = @intCast(spartan_count);
^~~~~~~~~~~~~
At runtime:
runtime_invalid_cast_truncate.zig
const std = @import("std");
Shell
$ zig build-exe runtime_invalid_cast_truncate.zig
$ ./runtime_invalid_cast_truncate
thread 990675 panic: integer cast truncated bits
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_cast_truncate.zig:6:22: 0x
const byte: u8 = @intCast(spartan_count);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033619 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033181 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
To truncate bits, use @truncate.
Integer Overflow
Default Operations
• + (addition)
• - (subtraction)
• - (negation)
• * (multiplication)
• / (division)
• @divTrunc (division)
• @divFloor (division)
• @divExact (division)
test_comptime_overflow.zig
comptime {
var byte: u8 = 255;
byte += 1;
}
Shell
$ zig test test_comptime_overflow.zig
docgen_tmp/test_comptime_overflow.zig:3:10: error: overflow of integer type 'u8' with value '256'
byte += 1;
~~~~~^~~~
At runtime:
runtime_overflow.zig
const std = @import("std");
Shell
$ zig build-exe runtime_overflow.zig
$ ./runtime_overflow
thread 990715 panic: integer overflow
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_overflow.zig:5:10: 0x1033d9e in ma
byte += 1;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033619 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033181 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
• @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
math_add.zig
const math = @import("std").math;
const print = @import("std").debug.print;
pub fn main() !void {
var byte: u8 = 255;
Shell
$ zig build-exe math_add.zig
$ ./math_add
unable to add one: Overflow
error: Overflow
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/math.zig:619:21: 0x1033df5 in add__a
if (ov[1] != 0) return error.Overflow;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/math_add.zig:8:9: 0x1033d93 in main (math_
return err;
^
These builtins return a tuple containing whether there was an overflow (as a u1 ) and the
possibly overflowed bits of the operation:
• @addWithOverflow
• @subWithOverflow
• @mulWithOverflow
• @shlWithOverflow
Example of @addWithOverflow:
addWithOverflow_builtin.zig
const print = @import("std").debug.print;
pub fn main() void {
const byte: u8 = 255;
Shell
$ zig build-exe addWithOverflow_builtin.zig
$ ./addWithOverflow_builtin
overflowed result: 9
Wrapping Operations
• +% (wraparound addition)
• -% (wraparound subtraction)
• -% (wraparound negation)
• *% (wraparound multiplication)
test_wraparound_semantics.zig
const std = @import("std");
const expect = std.testing.expect;
const minInt = std.math.minInt;
const maxInt = std.math.maxInt;
Shell
$ zig test test_wraparound_semantics.zig
1/1 test_wraparound_semantics.test.wraparound addition and subtraction... OK
All 1 tests passed.
At compile-time:
test_comptime_shlExact_overwlow.zig
comptime {
const x = @shlExact(@as(u8, 0b01010101), 2);
_ = x;
}
Shell
$ zig test test_comptime_shlExact_overwlow.zig
docgen_tmp/test_comptime_shlExact_overwlow.zig:2:15: error: operation caused overflow
const x = @shlExact(@as(u8, 0b01010101), 2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At runtime:
runtime_shlExact_overflow.zig
const std = @import("std");
Shell
$ zig build-exe runtime_shlExact_overflow.zig
$ ./runtime_shlExact_overflow
thread 990836 panic: left shift overflowed bits
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_shlExact_overflow.zig:6:5: 0x1033d
const y = @shlExact(x, 2);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033639 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10331a1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_shrExact_overflow.zig
comptime {
const x = @shrExact(@as(u8, 0b10101010), 2);
_ = x;
}
Shell
$ zig test test_comptime_shrExact_overflow.zig
docgen_tmp/test_comptime_shrExact_overflow.zig:2:15: error: exact shift shifted out 1 bits
const x = @shrExact(@as(u8, 0b10101010), 2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At runtime:
runtime_shrExact_overflow.zig
const std = @import("std");
pub fn main() void {
var x: u8 = 0b10101010; // runtime-known
_ = &x;
const y = @shrExact(x, 2);
std.debug.print("value: {}\n", .{y});
}
Shell
$ zig build-exe runtime_shrExact_overflow.zig
$ ./runtime_shrExact_overflow
thread 990876 panic: right shift overflowed bits
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_shrExact_overflow.zig:6:5: 0x1033d
const y = @shrExact(x, 2);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033639 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10331a1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
Division by Zero
At compile-time:
test_comptime_division_by_zero.zig
comptime {
const a: i32 = 1;
const b: i32 = 0;
const c = a / b;
_ = c;
}
Shell
$ zig test test_comptime_division_by_zero.zig
docgen_tmp/test_comptime_division_by_zero.zig:4:19: error: division by zero here causes undefined beh
const c = a / b;
^
At runtime:
runtime_division_by_zero.zig
const std = @import("std");
Shell
$ zig build-exe runtime_division_by_zero.zig
$ ./runtime_division_by_zero
thread 990916 panic: division by zero
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_division_by_zero.zig:7:17: 0x1033d
const c = a / b;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033589 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10330f1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_remainder_division_by_zero.zig
comptime {
const a: i32 = 10;
const b: i32 = 0;
const c = a % b;
_ = c;
}
Shell
$ zig test test_comptime_remainder_division_by_zero.zig
docgen_tmp/test_comptime_remainder_division_by_zero.zig:4:19: error: division by zero here causes und
const c = a % b;
^
At runtime:
runtime_remainder_division_by_zero.zig
const std = @import("std");
Shell
$ zig build-exe runtime_remainder_division_by_zero.zig
$ ./runtime_remainder_division_by_zero
thread 990956 panic: division by zero
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_remainder_division_by_zero.zig:7:1
const c = a % b;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033589 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10330f1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
test_comptime_divExact_remainder.zig
comptime {
const a: u32 = 10;
const b: u32 = 3;
const c = @divExact(a, b);
_ = c;
}
Shell
$ zig test test_comptime_divExact_remainder.zig
docgen_tmp/test_comptime_divExact_remainder.zig:4:15: error: exact division produced remainder
const c = @divExact(a, b);
^~~~~~~~~~~~~~~
At runtime:
runtime_divExact_remainder.zig
const std = @import("std");
Shell
$ zig build-exe runtime_divExact_remainder.zig
$ ./runtime_divExact_remainder
thread 990996 panic: exact division produced remainder
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_divExact_remainder.zig:7:15: 0x103
const c = @divExact(a, b);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x10335a9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033111 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_unwrap_null.zig
comptime {
const optional_number: ?i32 = null;
const number = optional_number.?;
_ = number;
}
Shell
$ zig test test_comptime_unwrap_null.zig
docgen_tmp/test_comptime_unwrap_null.zig:3:35: error: unable to unwrap null
const number = optional_number.?;
~~~~~~~~~~~~~~~^~
At runtime:
runtime_unwrap_null.zig
const std = @import("std");
Shell
$ zig build-exe runtime_unwrap_null.zig
$ ./runtime_unwrap_null
thread 991036 panic: attempt to use null value
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_unwrap_null.zig:6:35: 0x1033db2 in
const number = optional_number.?;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033619 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033181 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
One way to avoid this crash is to test for null instead of assuming non-null, with the if
expression:
testing_null_with_if.zig
const print = @import("std").debug.print;
pub fn main() void {
const optional_number: ?i32 = null;
if (optional_number) |number| {
print("got number: {}\n", .{number});
} else {
print("it's null\n", .{});
}
}
Shell
$ zig build-exe testing_null_with_if.zig
$ ./testing_null_with_if
it's null
See also:
• Optionals
test_comptime_unwrap_error.zig
comptime {
const number = getNumberOrFail() catch unreachable;
_ = number;
}
fn getNumberOrFail() !i32 {
return error.UnableToReturnNumber;
}
Shell
$ zig test test_comptime_unwrap_error.zig
docgen_tmp/test_comptime_unwrap_error.zig:2:44: error: caught unexpected error 'UnableToReturnNumber'
const number = getNumberOrFail() catch unreachable;
^~~~~~~~~~~
docgen_tmp/test_comptime_unwrap_error.zig:7:18: note: error returned here
return error.UnableToReturnNumber;
^~~~~~~~~~~~~~~~~~~~
At runtime:
runtime_unwrap_error.zig
const std = @import("std");
fn getNumberOrFail() !i32 {
return error.UnableToReturnNumber;
}
Shell
$ zig build-exe runtime_unwrap_error.zig
$ ./runtime_unwrap_error
thread 991103 panic: attempt to unwrap error: UnableToReturnNumber
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_unwrap_error.zig:9:5: 0x1035e9f in
return error.UnableToReturnNumber;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_unwrap_error.zig:4:44: 0x1033e31 i
const number = getNumberOrFail() catch unreachable;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033679 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x10331e1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
One way to avoid this crash is to test for an error instead of assuming a successful result,
with the if expression:
testing_error_with_if.zig
const print = @import("std").debug.print;
pub fn main() void {
const result = getNumberOrFail();
if (result) |number| {
print("got number: {}\n", .{number});
} else |err| {
print("got error: {s}\n", .{@errorName(err)});
}
}
fn getNumberOrFail() !i32 {
return error.UnableToReturnNumber;
}
Shell
$ zig build-exe testing_error_with_if.zig
$ ./testing_error_with_if
got error: UnableToReturnNumber
See also:
• Errors
At compile-time:
test_comptime_invalid_error_code.zig
comptime {
const err = error.AnError;
const number = @intFromError(err) + 10;
const invalid_err = @errorFromInt(number);
_ = invalid_err;
}
Shell
$ zig test test_comptime_invalid_error_code.zig
docgen_tmp/test_comptime_invalid_error_code.zig:4:39: error: integer value '11' represents no error
const invalid_err = @errorFromInt(number);
^~~~~~
At runtime:
runtime_invalid_error_code.zig
const std = @import("std");
Shell
$ zig build-exe runtime_invalid_error_code.zig
$ ./runtime_invalid_error_code
thread 991170 panic: invalid error code
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_error_code.zig:7:5: 0x1033
const invalid_err = @errorFromInt(number);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033609 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033171 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_invalid_enum_cast.zig
const Foo = enum {
a,
b,
c,
};
comptime {
const a: u2 = 3;
const b: Foo = @enumFromInt(a);
_ = b;
}
Shell
$ zig test test_comptime_invalid_enum_cast.zig
docgen_tmp/test_comptime_invalid_enum_cast.zig:8:20: error: enum 'test_comptime_invalid_enum_cast.Foo
const b: Foo = @enumFromInt(a);
^~~~~~~~~~~~~~~
docgen_tmp/test_comptime_invalid_enum_cast.zig:1:13: note: enum declared here
const Foo = enum {
^~~~
At runtime:
runtime_invalid_enum_cast.zig
const std = @import("std");
Shell
$ zig build-exe runtime_invalid_enum_cast.zig
$ ./runtime_invalid_enum_cast
thread 991210 panic: invalid enum value
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_enum_cast.zig:12:20: 0x103
const b: Foo = @enumFromInt(a);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x10335f9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033161 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_invalid_error_set_cast.zig
const Set1 = error{
A,
B,
};
const Set2 = error{
A,
C,
};
comptime {
_ = @as(Set2, @errorCast(Set1.B));
}
Shell
$ zig test test_comptime_invalid_error_set_cast.zig
docgen_tmp/test_comptime_invalid_error_set_cast.zig:10:19: error: 'error.B' not a member of error set
_ = @as(Set2, @errorCast(Set1.B));
^~~~~~~~~~~~~~~~~~
At runtime:
runtime_invalid_error_set_cast.zig
const std = @import("std");
Shell
$ zig build-exe runtime_invalid_error_set_cast.zig
$ ./runtime_invalid_error_set_cast
thread 991250 panic: invalid error code
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_error_set_cast.zig:15:21:
const x: Set2 = @errorCast(set1);
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_error_set_cast.zig:12:8: 0
foo(Set1.B);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033629 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033191 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_incorrect_pointer_alignment.zig
comptime {
const ptr: *align(1) i32 = @ptrFromInt(0x1);
const aligned: *align(4) i32 = @alignCast(ptr);
_ = aligned;
}
Shell
$ zig test test_comptime_incorrect_pointer_alignment.zig
docgen_tmp/test_comptime_incorrect_pointer_alignment.zig:3:47: error: pointer address 0x1 is not alig
const aligned: *align(4) i32 = @alignCast(ptr);
^~~
At runtime:
runtime_incorrect_pointer_alignment.zig
const mem = @import("std").mem;
pub fn main() !void {
var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
const bytes = mem.sliceAsBytes(array[0..]);
if (foo(bytes) != 0x11111111) return error.Wrong;
}
fn foo(bytes: []u8) u32 {
const slice4 = bytes[1..5];
const int_slice = mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
return int_slice[0];
}
Shell
$ zig build-exe runtime_incorrect_pointer_alignment.zig
$ ./runtime_incorrect_pointer_alignment
thread 991290 panic: incorrect alignment
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_incorrect_pointer_alignment.zig:9:
const int_slice = mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_incorrect_pointer_alignment.zig:5:
if (foo(bytes) != 0x11111111) return error.Wrong;
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:511:37: 0x1033835 in posix
const result = root.main() catch |err| {
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1033351 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
At compile-time:
test_comptime_wrong_union_field_access.zig
comptime {
var f = Foo{ .int = 42 };
f.float = 12.34;
}
Shell
$ zig test test_comptime_wrong_union_field_access.zig
docgen_tmp/test_comptime_wrong_union_field_access.zig:3:6: error: access of union field 'float' while
f.float = 12.34;
~^~~~~~
docgen_tmp/test_comptime_wrong_union_field_access.zig:6:13: note: union declared here
const Foo = union {
^~~~~
At runtime:
runtime_wrong_union_field_access.zig
const std = @import("std");
Shell
$ zig build-exe runtime_wrong_union_field_access.zig
$ ./runtime_wrong_union_field_access
thread 991330 panic: access of union field 'float' while field 'int' is active
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_wrong_union_field_access.zig:14:6:
f.float = 12.34;
^
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_wrong_union_field_access.zig:10:8:
bar(&f);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1038fb9 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1038b21 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
To change the active field of a union, assign the entire union, like this:
change_active_union_field.zig
const std = @import("std");
Shell
$ zig build-exe change_active_union_field.zig
$ ./change_active_union_field
value: 1.234e1
To change the active field of a union when a meaningful value for the field is not known, use
undefined, like this:
undefined_active_union_field.zig
const std = @import("std");
Shell
$ zig build-exe undefined_active_union_field.zig
$ ./undefined_active_union_field
value: 1.234e1
See also:
• union
• extern union
This happens when casting a float to an integer where the float has a value outside the
integer type's range.
At compile-time:
test_comptime_out_of_bounds_float_to_integer_cast.zig
comptime {
const float: f32 = 4294967296;
const int: i32 = @intFromFloat(float);
_ = int;
}
Shell
$ zig test test_comptime_out_of_bounds_float_to_integer_cast.zig
docgen_tmp/test_comptime_out_of_bounds_float_to_integer_cast.zig:3:36: error: float value '4294967296
const int: i32 = @intFromFloat(float);
^~~~~
At runtime:
runtime_out_of_bounds_float_to_integer_cast.zig
pub fn main() void {
var float: f32 = 4294967296; // runtime-known
_ = &float;
const int: i32 = @intFromFloat(float);
_ = int;
}
Shell
$ zig build-exe runtime_out_of_bounds_float_to_integer_cast.zig
$ ./runtime_out_of_bounds_float_to_integer_cast
thread 991424 panic: integer part of floating point value out of bounds
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_out_of_bounds_float_to_integer_cas
const int: i32 = @intFromFloat(float);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033489 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1032ff1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
This happens when casting a pointer with the address 0 to a pointer which may not have the
address 0. For example, C Pointers, Optional Pointers, and allowzero pointers allow address
zero, but normal Pointers do not.
At compile-time:
test_comptime_invalid_null_pointer_cast.zig
comptime {
const opt_ptr: ?*i32 = null;
const ptr: *i32 = @ptrCast(opt_ptr);
_ = ptr;
}
Shell
$ zig test test_comptime_invalid_null_pointer_cast.zig
docgen_tmp/test_comptime_invalid_null_pointer_cast.zig:3:32: error: null pointer casted to type '*i32
const ptr: *i32 = @ptrCast(opt_ptr);
^~~~~~~
At runtime:
runtime_invalid_null_pointer_cast.zig
pub fn main() void {
var opt_ptr: ?*i32 = null;
_ = &opt_ptr;
const ptr: *i32 = @ptrCast(opt_ptr);
_ = ptr;
}
Shell
$ zig build-exe runtime_invalid_null_pointer_cast.zig
$ ./runtime_invalid_null_pointer_cast
thread 991464 panic: cast causes pointer to be null
/home/ci/actions-runner/_work/zig-bootstrap/zig/docgen_tmp/runtime_invalid_null_pointer_cast.zig:4:23
const ptr: *i32 = @ptrCast(opt_ptr);
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:501:22: 0x1033469 in posix
root.main();
^
/home/ci/actions-runner/_work/zig-bootstrap/out/host/lib/zig/std/start.zig:253:5: 0x1032fd1 in _start
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
(process terminated by signal)
Memory
The Zig language performs no memory management on behalf of the programmer. This is
why Zig has no runtime, and why Zig code works seamlessly in so many environments,
including real-time software, operating system kernels, embedded devices, and low latency
servers. As a consequence, Zig programmers must always be able to answer the question:
Like Zig, the C programming language has manual memory management. However, unlike
Zig, C has a default allocator - malloc , realloc , and free . When linking against libc, Zig
exposes this allocator with std.heap.c_allocator . However, by convention, there is no
default allocator in Zig. Instead, functions which need to allocate accept an Allocator
parameter. Likewise, data structures such as std.ArrayList accept an Allocator parameter
in their initialization functions:
test_allocator.zig
const std = @import("std");
const Allocator = std.mem.Allocator;
const expect = std.testing.expect;
Shell
$ zig test test_allocator.zig
1/1 test_allocator.test.using an allocator... OK
All 1 tests passed.
In the above example, 100 bytes of stack memory are used to initialize a
FixedBufferAllocator , which is then passed to a function. As a convenience there is a global
an Allocator guide.
Choosing an Allocator
What allocator to use depends on a number of factors. Here is a flow chart to help you
decide:
1. Are you making a library? In this case, best to accept an Allocator as a parameter and
allow your library's users to decide what allocator to use.
2. Are you linking libc? In this case, std.heap.c_allocator is likely the right choice, at least
for your main allocator.
3. Is the maximum number of bytes that you will need bounded by a number known at
comptime? In this case, use std.heap.FixedBufferAllocator or
std.heap.ThreadSafeFixedBufferAllocator depending on whether you need thread-
safety or not.
4. Is your program a command line application which runs from start to end without any
fundamental cyclical pattern (such as a video game main loop, or a web server request
handler), such that it would make sense to free everything at once at the end? In this
case, it is recommended to follow this pattern:
cli_allocation.zig
const std = @import("std");
Shell
$ zig build-exe cli_allocation.zig
$ ./cli_allocation
ptr=i32@7fb0ffb52010
When using this kind of allocator, there is no need to free anything manually.
Everything gets freed at once with the call to arena.deinit() .
5. Are the allocations part of a cyclical pattern such as a video game main loop, or a web
server request handler? If the allocations can all be freed at once, at the end of the
cycle, for example once the video game frame has been fully rendered, or the web
server request has been served, then std.heap.ArenaAllocator is a great candidate. As
demonstrated in the previous bullet point, this allows you to free entire arenas at
once. Note also that if an upper bound of memory can be established, then
std.heap.FixedBufferAllocator can be used as a further optimization.
6. Are you writing a test, and you want to make sure error.OutOfMemory is handled
correctly? In this case, use std.testing.FailingAllocator .
7. Are you writing a test? In this case, use std.testing.allocator .
8. Finally, if none of the above apply, you need a general purpose allocator. Zig's general
purpose allocator is available as a function that takes a comptime struct of
configuration options and returns a type. Generally, you will set up one
std.heap.GeneralPurposeAllocator in your main function, and then pass it or sub-
String literals such as "hello" are in the global constant data section. This is why it is an
error to pass a string literal to a mutable slice, like this:
test_string_literal_to_slice.zig
fn foo(s: []u8) void {
_ = s;
}
Shell
$ zig test test_string_literal_to_slice.zig
docgen_tmp/test_string_literal_to_slice.zig:6:9: error: expected type '[]u8', found '*const [5:0]u8'
foo("hello");
^~~~~~~
docgen_tmp/test_string_literal_to_slice.zig:6:9: note: cast discards const qualifier
docgen_tmp/test_string_literal_to_slice.zig:1:11: note: parameter type declared here
fn foo(s: []u8) void {
^~~~
test_string_literal_to_const_slice.zig
fn foo(s: []const u8) void {
_ = s;
}
Shell
$ zig test test_string_literal_to_const_slice.zig
1/1 test_string_literal_to_const_slice.test.string literal to constant slice... OK
All 1 tests passed.
Just like string literals, const declarations, when the value is known at comptime, are
stored in the global constant data section. Also Compile Time Variables are stored in the
global constant data section.
var declarations inside functions are stored in the function's stack frame. Once a function
returns, any Pointers to variables in the function's stack frame become invalid references,
and dereferencing them becomes unchecked Undefined Behavior.
var declarations at the top level or in struct declarations are stored in the global data
section.
Implementing an Allocator
Zig programmers can implement their own allocators by fulfilling the Allocator interface. In
order to do this one must read carefully the documentation comments in std/mem.zig and
then supply a allocFn and a resizeFn .
There are many example allocators to look at for inspiration. Look at std/heap.zig and
std.heap.GeneralPurposeAllocator .
Heap Allocation Failure
Many programming languages choose to handle the possibility of heap allocation failure by
unconditionally crashing. By convention, Zig programmers do not consider this to be a
satisfactory solution. Instead, error.OutOfMemory represents heap allocation failure, and Zig
libraries return this error code whenever heap allocation failure prevented an operation
from completing successfully.
Some have argued that because some operating systems such as Linux have memory
overcommit enabled by default, it is pointless to handle heap allocation failure. There are
many problems with this reasoning:
Recursion
Recursion is an area of active experimentation in Zig and so the documentation here is not
final. You can read a summary of recursion status in the 0.3.0 release notes.
The short summary is that currently recursion works normally as you would expect.
Although Zig code is not yet protected from stack overflow, it is planned that a future
version of Zig will provide such protection, with some degree of cooperation from Zig code
required.
In order to prevent bugs, there are some helpful conventions to follow when dealing with
pointers. In general, when a function returns a pointer, the documentation for the function
should explain who "owns" the pointer. This concept helps the programmer decide when it
is appropriate, if ever, to free the pointer.
For example, the function's documentation may say "caller owns the returned memory", in
which case the code that calls the function must have a plan for when to free that memory.
Probably in this situation, the function will accept an Allocator parameter.
Sometimes the lifetime of a pointer may be more complicated. For example, the
std.ArrayList(T).items slice has a lifetime that remains valid until the next time the list is
The API documentation for functions and data structures should take great care to explain
the ownership and lifetime semantics of pointers. Ownership determines whose
responsibility it is to free the memory referenced by the pointer, and lifetime determines
the point at which the memory becomes inaccessible (lest Undefined Behavior occur).
Compile Variables
Compile variables are accessible by importing the "builtin" package, which the compiler
makes available to every Zig source file. It contains compile-time constants such as the
current target, endianness, and release mode.
compile_variables.zig
const builtin = @import("builtin");
const separator = if (builtin.os.tag == .windows) '\\' else '/';
@import("builtin")
const std = @import("std");
/// Zig version. When writing code that supports multiple versions of Zig, prefer
/// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
pub const zig_version = std.SemanticVersion.parse(zig_version_string) catch unreachable;
pub const zig_version_string = "0.12.0";
pub const zig_backend = std.builtin.CompilerBackend.stage2_llvm;
See also:
• Build Mode
To use the build system, run zig to see a command-line usage help menu. This
build --help
will include project-specific options that were declared in the build.zig script.
For the time being, the build system documentation is hosted externally: Build System
Documentation
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.
C Type Primitives
These have guaranteed C ABI compatibility and can be used like any other type.
• c_char
• c_short
• c_ushort
• c_int
• c_uint
• c_long
• c_ulong
• c_longlong
• c_ulonglong
• c_longdouble
See also:
• Primitive Types
The @cImport builtin function can be used to directly import symbols from .h files:
cImport_builtin.zig
const c = @cImport({
// See https://github.com/ziglang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("stdio.h");
});
pub fn main() void {
_ = c.printf("hello\n");
}
Shell
$ zig build-exe cImport_builtin.zig -lc
$ ./cImport_builtin
hello
@cImport Expression
const builtin = @import("builtin");
const c = @cImport({
@cDefine("NDEBUG", builtin.mode == .ReleaseFast);
if (something) {
@cDefine("_GNU_SOURCE", {});
}
@cInclude("stdlib.h");
if (something) {
@cUndef("_GNU_SOURCE");
}
@cInclude("soundio.h");
});
See also:
• @cImport
• @cInclude
• @cDefine
• @cUndef
• @import
C Translation CLI
Zig's C translation capability is available as a CLI tool via zig translate-c. It requires a single
filename as an argument. It may also take a set of optional flags that are forwarded to
clang. It writes the translated file to stdout.
• -I:Specify a search directory for include files. May be used multiple times. Equivalent
to clang's -I flag. The current directory is not included by default; use -I. to include it.
• -D: Define a preprocessor macro. Equivalent to clang's -D flag.
• -cflags [flags] --:
Pass arbitrary additional command line flags to clang. Note: the list
of flags must end with --
• -target:The target triple for the translated Zig code. If no target is specified, the
current host target will be used.
Important! When translating C code with zig translate-c, you must use the same -target
triple that you will use when compiling the translated code. In addition, you must ensure
that the -cflags used, if any, match the cflags used by code on the target system. Using the
incorrect -target or -cflags could result in clang or Zig parse failures, or subtle ABI
incompatibilities when linking with C code.
varytarget.h
long FOO = __LONG_MAX__;
Shell
$ zig translate-c -target thumb-freestanding-gnueabihf varytarget.h|grep FOO
pub export var FOO: c_long = 2147483647;
$ zig translate-c -target x86_64-macos-gnu varytarget.h|grep FOO
pub export var FOO: c_long = 9223372036854775807;
varycflags.h
enum FOO { BAR };
int do_something(enum FOO foo);
Shell
$ zig translate-c varycflags.h|grep -B1 do_something
pub const enum_FOO = c_uint;
pub extern fn do_something(foo: enum_FOO) c_int;
$ zig translate-c -cflags -fshort-enums -- varycflags.h|grep -B1 do_something
pub const enum_FOO = u8;
pub extern fn do_something(foo: enum_FOO) c_int;
@cImport vs translate-c
@cImport and zig translate-c use the same underlying C translation functionality, so on a
technical level they are equivalent. In practice, @cImport is useful as a way to quickly and
easily access numeric constants, typedefs, and record types without needing any extra
setup. If you need to pass cflags to clang, or if you would like to edit the translated code, it
is recommended to use zig translate-c and save the results to a file. Common reasons for
editing the generated code include: changing anytype parameters in function-like macros to
more specific types; changing [*c]T pointers to [*]T or *T pointers for improved type
safety; and enabling or disabling runtime safety within specific functions.
See also:
• Targets
• C Type Primitives
• Pointers
• C Pointers
• Import from C Header File
• @cInclude
• @cImport
• @setRuntimeSafety
C Translation Caching
The C translation feature (whether used via zig translate-c or @cImport ) integrates with the
Zig caching system. Subsequent runs with the same source file, target, and cflags will use
the cache instead of repeatedly translating the same code.
To see where the cached files are stored when compiling code that uses @cImport , use the
--verbose-cimport flag:
verbose_cimport_flag.zig
const c = @cImport({
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("stdio.h");
});
pub fn main() void {
_ = c;
}
Shell
$ zig build-exe verbose_cimport_flag.zig -lc --verbose-cimport
info(compilation): C import source: /home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o
info(compilation): C import .d file: /home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/
info(compilation): C import output: /home/ci/actions-runner/_work/zig-bootstrap/out/zig-local-cache/o
LLVM Emit Object... [20D[0KLLVM Emit Object... [20D[0KLLD Link... [12D[0K$ ./verbose_cimport_flag
cimport.h contains the file to translate (constructed from calls to @cInclude , @cDefine , and
@cUndef ), cimport.h.d is the list of file dependencies, and cimport.zig contains the
translated output.
See also:
• Import from C Header File
• C Translation CLI
• @cInclude
• @cImport
Translation failures
Some C constructs cannot be translated to Zig - for example, goto, structs with bitfields, and
token-pasting macros. Zig employs demotion to allow translation to continue in the face of
non-translatable entities.
Demotion comes in three varieties - opaque, extern, and @compileError . C structs and unions
that cannot be translated correctly will be translated as opaque{} . Functions that contain
opaque types or code constructs that cannot be translated will be demoted to extern
declarations. Thus, non-translatable types can still be used as pointers, and non-translatable
functions can be called so long as the linker is aware of the compiled function.
See also:
• opaque
• extern
• @compileError
C Macros
macro.c
#define MAKELOCAL(NAME, INIT) int NAME = INIT
int foo(void) {
MAKELOCAL(a, 1);
MAKELOCAL(b, 2);
return a + b;
}
Shell
$ zig translate-c macro.c > macro.zig
macro.zig
pub export fn foo() c_int {
var a: c_int = 1;
_ = &a;
var b: c_int = 2;
_ = &b;
return a + b;
}
pub const MAKELOCAL = @compileError("unable to translate C expr: unexpected token .Equal");
Note that foo was translated correctly despite using a non-translatable macro. MAKELOCAL
was demoted to @compileError since it cannot be expressed as a Zig function; this simply
means that you cannot directly use MAKELOCAL from Zig.
See also:
• @compileError
C Pointers
This type is to be avoided whenever possible. The only valid reason for using a C pointer is in
auto-generated code from translating C code.
[*c]T - C pointer.
• Supports all the syntax of the other two pointer types ( *T ) and ( [*]T ).
• Coerces to other pointer types, as well as Optional Pointers. When a C pointer is
coerced to a non-optional pointer, safety-checked Undefined Behavior occurs if the
address is 0.
• Allows address 0. On non-freestanding targets, dereferencing address 0 is safety-
checked Undefined Behavior. Optional C pointers introduce another bit to keep track
of null, just like ?usize . Note that creating an optional C pointer is unnecessary as one
can use normal Optional Pointers.
• Supports Type Coercion to and from integers.
• Supports comparison with integers.
• Does not support Zig-only pointer attributes such as alignment. Use normal Pointers
please!
When a C pointer is pointing to a single struct (not an array), dereference the C pointer to
access the struct's fields or member data. That syntax looks like this:
ptr_to_struct.*.struct_member
ptr_to_struct_array[index].struct_member
C Variadic Functions
test_variadic_function.zig
const std = @import("std");
const testing = std.testing;
Shell
$ zig test test_variadic_function.zig -lc
1/1 test_variadic_function.test.variadic function... OK
All 1 tests passed.
Hello, world!
test_defining_variadic_function.zig
const std = @import("std");
const testing = std.testing;
const builtin = @import("builtin");
Shell
$ zig test test_defining_variadic_function.zig
1/1 test_defining_variadic_function.test.defining a variadic function... OK
All 1 tests passed.
Exporting a C Library
One of the primary use cases for Zig is exporting a library with the C ABI for other
programming languages to call into. The export keyword in front of functions, variables,
and types causes them to be part of the library API:
mathtest.zig
export fn add(a: i32, b: i32) i32 {
return a + b;
}
Shell
$ zig build-lib mathtest.zig
Shell
$ zig build-lib mathtest.zig -dynamic
test.c
// This header is generated by zig from mathtest.zig
#include "mathtest.h"
#include <stdio.h>
build_c.zig
const std = @import("std");
Shell
$ zig build test
1379
See also:
• export
You can mix Zig object files with any other object files that respect the C ABI. Example:
base64.zig
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;
const decoded_size = base64_decoder.calcSizeForSlice(src) catch unreachable;
base64_decoder.decode(dest[0..decoded_size], src) catch unreachable;
return decoded_size;
}
test.c
// This header is generated by zig from base64.zig
#include "base64.h"
#include <string.h>
#include <stdio.h>
return 0;
}
build_object.zig
const std = @import("std");
Shell
$ zig build
$ ./zig-out/bin/test
all your base are belong to us
See also:
• Targets
• Zig Build System
WebAssembly
Zig supports building for WebAssembly out of the box.
Freestanding
For host environments like the web browser and nodejs, build as an executable using the
freestanding OS target. Here's an example of running Zig code compiled to WebAssembly
with nodejs.
math.zig
extern fn print(i32) void;
Shell
$ zig build-exe math.zig -target wasm32-freestanding -fno-entry --export=add
test.js
const fs = require('fs');
const source = fs.readFileSync("./math.wasm");
const typedArray = new Uint8Array(source);
WebAssembly.instantiate(typedArray, {
env: {
print: (result) => { console.log(`The result is ${result}`); }
}}).then(result => {
const add = result.instance.exports.add;
add(1, 2);
});
Shell
$ node test.js
The result is 3
WASI
Zig's support for WebAssembly System Interface (WASI) is under active development.
Example of using the standard library and reading command line arguments:
wasi_args.zig
const std = @import("std");
Shell
$ zig build-exe wasi_args.zig -target wasm32-wasi
Shell
$ wasmtime wasi_args.wasm 123 hello
0: wasi_args.wasm
1: 123
2: hello
A more interesting example would be extracting the list of preopens from the runtime. This
is now supported in the standard library via std.fs.wasi.Preopens :
wasi_preopens.zig
const std = @import("std");
const fs = std.fs;
Shell
$ zig build-exe wasi_preopens.zig -target wasm32-wasi
Shell
$ wasmtime --dir=. wasi_preopens.wasm
0: stdin
1: stdout
2: stderr
3: .
Targets
Target refers to the computer that will be used to run an executable. It is composed of the
CPU architecture, the set of enabled CPU features, operating system, minimum and
maximum operating system version, ABI, and ABI version.
When no target option is provided to the compiler, the default choice is to target the host
computer, meaning that the resulting executable will be unsuitable for copying to a different
computer. In order to copy an executable to another computer, the compiler needs to know
about the target requirements via the -target option.
The Zig Standard Library ( @import("std") ) has cross-platform abstractions, making the same
source code viable on many targets. Some code is more portable than other code. In
general, Zig code is extremely portable compared to other programming languages.
Each platform requires its own implementations to make Zig's cross-platform abstractions
work. These implementations are at various degrees of completion. Each tagged release of
the compiler comes with release notes that provide the full support table for each target.
Style Guide
These coding conventions are not enforced by the compiler, but they are shipped in this
documentation along with the compiler in order to provide a point of reference, should
anyone wish to point to an authority on agreed upon Zig coding style.
• Value
• Data
• Context
• Manager
• utils, misc, or somebody's initials
Everything is a value, all types are data, everything is context, all logic manages state.
Nothing is communicated by using a word that applies to all types.
Every declaration is assigned a fully qualified namespace by the compiler, creating a tree
structure. Choose names based on the fully-qualified namespace, and avoid redundant
name segments.
redundant_fqn.zig
const std = @import("std");
Shell
$ zig build-exe redundant_fqn.zig
$ ./redundant_fqn
redundant_fqn.json.JsonValue
In this example, "json" is repeated in the fully-qualified namespace. The solution is to delete
Json from JsonValue . In this example we have an empty struct named json but remember
This example is an exception to the rule specified in Avoid Redundancy in Names. The
meaning of the type has been reduced to its core: it is a json value. The name cannot be any
more specific without being incorrect.
Whitespace
• 4 space indentation
• Open braces on same line, unless you need to wrap.
• If a list of things is longer than 2, put each item on its own line and exercise the ability
to put an extra comma at the end.
• Line length: aim for 100; use common sense.
Names
Roughly speaking: camelCaseFunctionName , TitleCaseTypeName , snake_case_variable_name .
More precisely:
Acronyms, initialisms, proper nouns, or any other word that has capitalization rules in
written English are subject to naming conventions just like any other word. Even acronyms
that are only 2 letters long are subject to these conventions.
File names fall into two categories: types and namespaces. If the file (implicitly a struct) has
top level fields, it should be named like any other struct with fields using TitleCase .
Otherwise, it should use snake_case . Directory names should be snake_case .
These are general rules of thumb; if it makes sense to do something different, do what
makes sense. For example, if there is an established convention such as ENOENT , follow the
established convention.
Examples
style_example.zig
const namespace_name = @import("dir_name/file_name.zig");
const TypeName = @import("dir_name/TypeName.zig");
var global_var: i32 = undefined;
const const_name = 42;
const primitive_type_alias = f32;
const string_alias = []u8;
// The initials BE (Big Endian) are just another word in Zig identifier names.
fn readU32Be() u32 {}
• Omit any information that is redundant based on the name of the thing being
documented.
• Duplicating information onto multiple similar functions is encouraged because it helps
IDEs and other tools provide better help text.
• Use the word assume to indicate invariants that cause Undefined Behavior when
violated.
• Use the word assert to indicate invariants that cause safety-checked Undefined
Behavior when violated.
Source Encoding
Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile
error.
Throughout all zig source code (including in comments), some code points are never
allowed:
• Ascii control characters, except for U+000a (LF), U+000d (CR), and U+0009 (HT):
U+0000 - U+0008, U+000b - U+000c, U+000e - U+0001f, U+007f.
• Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).
LF (byte value 0x0a, code point U+000a, '\n') is the line terminator in Zig source code. This
byte value terminates every line of zig source code except the last line of the file. It is
recommended that non-empty source files end with an empty line, which means the last
byte would be 0x0a (LF).
Each LF may be immediately preceded by a single CR (byte value 0x0d, code point U+000d,
'\r' ) to form a Windows style line ending, but this is discouraged. Note that in multiline
strings, CRLF sequences will be encoded as LF when compiled into a zig program. A CR in
any other context is not allowed.
HT hard tabs (byte value 0x09, code point U+0009, '\t' ) are interchangeable with SP
spaces (byte value 0x20, code point U+0020, ' ' ) as a token separator, but use of hard tabs
is discouraged. See Grammar.
For compatibility with other tools, the compiler ignores a UTF-8-encoded byte order mark
(U+FEFF) if it is the first Unicode code point in the source text. A byte order mark is not
allowed anywhere else in the source.
Note that running zig fmt on a source file will implement all recommendations mentioned
here.
Note that a tool reading Zig source code can make assumptions if the source code is
assumed to be correct Zig code. For example, when identifying the ends of lines, a tool can
use a naive search such as /\n/ , or an advanced search such as /\r\n?|[\n\u0085\u2028
\u2029]/ , and in either case line endings will be correctly identified. For another example,
when identifying the whitespace before the first token on a line, a tool can either use a
naive search such as /[ \t]/ , or an advanced search such as /\s/ , and in either case
whitespace will be correctly identified.
Keyword Reference
Keyword Description
await can be used to suspend the current function until the frame
provided after the await completes. await copies the value returned
await
from the target function's frame to the caller.
break can be used with a block label to return a value from the block. It
can also be used to exit a loop before iteration completes naturally.
break
See also Blocks, while, for
defer will execute an expression when control flow leaves the current
block.
defer
See also defer
If used after a loop expression, the else branch will be executed if the
loop finishes without breaking.
Keyword Description
See also if, switch, while, for
fn declares a function.
fn See also Functions
See also if
Code inside a nosuspend scope does not cause the enclosing function to
become an async function.
The packed keyword before a struct definition changes the struct's in-
memory layout to the guaranteed packed layout.
packed
See also packed struct
resume will continue execution of a function frame after the point the
resume
function was suspended.
suspend will cause control flow to return to the call site or resumer of
the function. suspend can also be used before a block within a function,
suspend
to allow the function access to its frame before control flow returns to
the call site.
The test keyword can be used to denote a top-level block of code used
to make sure behavior meets expectations.
test
See also Zig Test
unreachable can be used to assert that control flow will never happen
upon a particular location. Depending on the build mode, unreachable
may emit a panic.
unreachable
Emits a panic in Debug and ReleaseSafe mode, or when using zig test.
Appendix
Containers
A container in Zig is any syntactical construct that acts as a namespace to hold variable and
function declarations. Containers are also type definitions which can be instantiated.
Structs, enums, unions, opaques, and even Zig source files themselves are containers.
Although containers (except Zig source files) use curly braces to surround their definition,
they should not be confused with blocks or functions. Containers do not contain
statements.
Grammar
grammar.y
Root <- skip container_doc_comment? ContainerMembers eof
Decl
<- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / KEYWORD_inline / KEYWORD_noinline)? Fn
/ (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? GlobalVarDecl
/ KEYWORD_usingnamespace Expr SEMICOLON
FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? AddrSpace? LinkSection? Call
VarDeclProto <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? AddrSpace? Link
ComptimeStatement
<- BlockExpr
/ VarDeclExprStatement
IfStatement
<- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )?
/ IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )
ForStatement
<- ForPrefix BlockExpr ( KEYWORD_else Statement )?
/ ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement )
WhileStatement
<- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )?
/ WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )
BlockExprStatement
<- BlockExpr
/ AssignExpr SEMICOLON
PrimaryExpr
<- AsmExpr
/ IfExpr
/ KEYWORD_break BreakLabel? Expr?
/ KEYWORD_comptime Expr
/ KEYWORD_nosuspend Expr
/ KEYWORD_continue BreakLabel?
/ KEYWORD_resume Expr
/ KEYWORD_return Expr?
/ BlockLabel? LoopExpr
/ Block
/ CurlySuffixExpr
IfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)?
InitList
<- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE
/ LBRACE Expr (COMMA Expr)* COMMA? RBRACE
/ LBRACE RBRACE
SuffixExpr
<- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments
/ PrimaryTypeExpr (SuffixOp / FnCallArguments)*
PrimaryTypeExpr
<- BUILTINIDENTIFIER FnCallArguments
/ CHAR_LITERAL
/ ContainerDecl
/ DOT IDENTIFIER
/ DOT InitList
/ ErrorSetDecl
/ FLOAT
/ FnProto
/ GroupedExpr
/ LabeledTypeExpr
/ IDENTIFIER
/ IfTypeExpr
/ INTEGER
/ KEYWORD_comptime TypeExpr
/ KEYWORD_error DOT IDENTIFIER
/ KEYWORD_anyframe
/ KEYWORD_unreachable
/ STRINGLITERAL
/ SwitchExpr
LabeledTypeExpr
<- BlockLabel Block
/ BlockLabel? LoopTypeExpr
AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER
# Fn specific
CallConv <- KEYWORD_callconv LPAREN Expr RPAREN
ParamDecl
<- doc_comment? (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType
/ DOT3
ParamType
<- KEYWORD_anytype
/ TypeExpr
# Payloads
Payload <- PIPE IDENTIFIER PIPE
PtrListPayload <- PIPE ASTERISK? IDENTIFIER (COMMA ASTERISK? IDENTIFIER)* COMMA? PIPE
# Switch specific
SwitchProng <- KEYWORD_inline? SwitchCase EQUALRARROW PtrIndexPayload? SingleAssignExpr
SwitchCase
<- SwitchItem (COMMA SwitchItem)* COMMA?
/ KEYWORD_else
# For specific
ForArgumentsList <- ForItem (COMMA ForItem)* COMMA?
# Operators
AssignOp
<- ASTERISKEQUAL
/ ASTERISKPIPEEQUAL
/ SLASHEQUAL
/ PERCENTEQUAL
/ PLUSEQUAL
/ PLUSPIPEEQUAL
/ MINUSEQUAL
/ MINUSPIPEEQUAL
/ LARROW2EQUAL
/ LARROW2PIPEEQUAL
/ RARROW2EQUAL
/ AMPERSANDEQUAL
/ CARETEQUAL
/ PIPEEQUAL
/ ASTERISKPERCENTEQUAL
/ PLUSPERCENTEQUAL
/ MINUSPERCENTEQUAL
/ EQUAL
CompareOp
<- EQUALEQUAL
/ EXCLAMATIONMARKEQUAL
/ LARROW
/ RARROW
/ LARROWEQUAL
/ RARROWEQUAL
BitwiseOp
<- AMPERSAND
/ CARET
/ PIPE
/ KEYWORD_orelse
/ KEYWORD_catch Payload?
BitShiftOp
<- LARROW2
/ RARROW2
/ LARROW2PIPE
AdditionOp
<- PLUS
/ MINUS
/ PLUS2
/ PLUSPERCENT
/ MINUSPERCENT
/ PLUSPIPE
/ MINUSPIPE
MultiplyOp
<- PIPE2
/ ASTERISK
/ SLASH
/ PERCENT
/ ASTERISK2
/ ASTERISKPERCENT
/ ASTERISKPIPE
PrefixOp
<- EXCLAMATIONMARK
/ MINUS
/ TILDE
/ MINUSPERCENT
/ AMPERSAND
/ KEYWORD_try
/ KEYWORD_await
PrefixTypeOp
<- QUESTIONMARK
/ KEYWORD_anyframe MINUSRARROW
/ SliceTypeStart (ByteAlign / AddrSpace / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
/ PtrTypeStart (AddrSpace / KEYWORD_align LPAREN Expr (COLON Expr COLON Expr)? RPAREN / KEYWORD_
/ ArrayTypeStart
SuffixOp
<- LBRACKET Expr (DOT2 (Expr? (COLON Expr)?)?)? RBRACKET
/ DOT IDENTIFIER
/ DOTASTERISK
/ DOTQUESTIONMARK
# Ptr specific
SliceTypeStart <- LBRACKET (COLON Expr)? RBRACKET
PtrTypeStart
<- ASTERISK
/ ASTERISK2
/ LBRACKET ASTERISK (LETTERC / COLON Expr)? RBRACKET
# ContainerDecl specific
ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACE
ContainerDeclType
<- KEYWORD_struct (LPAREN Expr RPAREN)?
/ KEYWORD_opaque
/ KEYWORD_enum (LPAREN Expr RPAREN)?
/ KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?
# Alignment
ByteAlign <- KEYWORD_align LPAREN Expr RPAREN
# Lists
IdentifierList <- (doc_comment? IDENTIFIER COMMA)* (doc_comment? IDENTIFIER)?
# From https://lemire.me/blog/2018/05/09/how-quickly-can-you-check-that-a-string-is-valid-unicode-utf
# First Byte Second Byte Third Byte Fourth Byte
# [0x00,0x7F]
# [0xC2,0xDF] [0x80,0xBF]
# 0xE0 [0xA0,0xBF] [0x80,0xBF]
# [0xE1,0xEC] [0x80,0xBF] [0x80,0xBF]
# 0xED [0x80,0x9F] [0x80,0xBF]
# [0xEE,0xEF] [0x80,0xBF] [0x80,0xBF]
# 0xF0 [0x90,0xBF] [0x80,0xBF] [0x80,0xBF]
# [0xF1,0xF3] [0x80,0xBF] [0x80,0xBF] [0x80,0xBF]
# 0xF4 [0x80,0x8F] [0x80,0xBF] [0x80,0xBF]
mb_utf8_literal <-
oxF4 ox80_ox8F ox80_oxBF ox80_oxBF
/ oxF1_oxF3 ox80_oxBF ox80_oxBF ox80_oxBF
/ oxF0 ox90_0xBF ox80_oxBF ox80_oxBF
/ oxEE_oxEF ox80_oxBF ox80_oxBF
/ oxED ox80_ox9F ox80_oxBF
/ oxE1_oxEC ox80_oxBF ox80_oxBF
/ oxE0 oxA0_oxBF ox80_oxBF
/ oxC2_oxDF ox80_oxBF
char_escape
<- "\\x" hex hex
/ "\\u{" hex+ "}"
/ "\\" [nr\\t'"]
char_char
<- mb_utf8_literal
/ char_escape
/ ascii_char_not_nl_slash_squote
string_char
<- char_escape
/ [^\\"\n]
Zen