Skip to content

Important Builtins in Zig 1.16

This chapter covered the builtins you will see most often as a beginner.

This chapter covered the builtins you will see most often as a beginner.

A builtin is a compiler-provided function. Its name starts with @.

@import("std")
@sizeOf(u32)
@alignOf(u32)
@intCast(x)

These are not standard library functions. They are part of the language.

Type and Module Builtins

Use these to import code or work with types.

BuiltinMeaning
@importLoad a Zig module or source file
@TypeOfGet the type of a value
@typeInfoInspect a type
@TypeBuild a type from type information
@typeNameGet the name of a type

Example:

const std = @import("std");

const x = 123;
const T = @TypeOf(x);

pub fn main() void {
    std.debug.print("{s}\n", .{@typeName(T)});
}

Memory Layout Builtins

Use these when you need to understand how data is stored.

BuiltinMeaning
@sizeOfNumber of bytes used by a type
@alignOfRequired memory alignment of a type
@offsetOfByte offset of a struct field
@bitSizeOfNumber of bits used by a type

Example:

const std = @import("std");

const Point = struct {
    x: i32,
    y: i32,
};

pub fn main() void {
    std.debug.print("size: {}\n", .{@sizeOf(Point)});
    std.debug.print("align: {}\n", .{@alignOf(Point)});
}

Conversion Builtins

Use these when converting values explicitly.

BuiltinMeaning
@intCastConvert between integer types, checking that the value fits
@truncateKeep only the low bits of an integer
@floatCastConvert between floating-point types
@floatFromIntConvert an integer to a float
@intFromFloatConvert a float to an integer
@bitCastReinterpret bits as another type of the same size

The important distinction is this:

@intCast preserves numeric meaning.
@truncate preserves low bits.
@bitCast preserves raw representation.

Example:

const x: u16 = 300;

const a: u8 = @truncate(x); // 44

Do not use @truncate when you mean “safe integer conversion.” Use @intCast with a range check.

Pointer and Memory Builtins

Use these when working close to raw memory.

BuiltinMeaning
@ptrCastReinterpret a pointer as another pointer type
@alignCastAssert stronger pointer alignment
@constCastRemove const from a pointer
@volatileCastChange volatile qualification
@memcpyCopy memory from source to destination
@memsetFill memory with a repeated value

Example:

var buffer = [_]u8{0} ** 5;
const text = "hello";

@memcpy(&buffer, text);

Use pointer casts carefully. They do not move memory, fix alignment, or prove that the pointed-to memory has the new type.

Compile-Time Control Builtins

Use these to guide compilation.

BuiltinMeaning
@compileErrorStop compilation with a message
@compileLogPrint compile-time debugging information
@setEvalBranchQuotaRaise the compile-time branch limit
@panicStop the running program with a message

Example:

comptime {
    if (@sizeOf(usize) != 8) {
        @compileError("this program requires a 64-bit target");
    }
}

@compileError is for invalid code or unsupported targets.

@panic is for runtime states that should not happen.

File Builtins

Use these to include build-time files.

BuiltinMeaning
@embedFileEmbed a file’s bytes into the executable

Example:

const help_text = @embedFile("help.txt");

The file is read during compilation. The final executable contains the bytes.

Beginner Rule

Most Zig programs use only a small set of builtins often:

@import
@sizeOf
@alignOf
@TypeOf
@intCast
@truncate
@floatCast
@bitCast
@ptrCast
@memcpy
@panic
@compileError
@embedFile

Learn these first.

Do not try to memorize every builtin at once. Read each one by asking:

What special thing is the compiler being asked to do?

That question usually explains the code.