Skip to content

Reserved Keywords

A keyword is a word that has special meaning in Zig.

A keyword is a word that has special meaning in Zig.

You cannot use a keyword as your own variable name, function name, struct name, or field name, because the compiler already uses that word for the language itself.

For example, this is invalid:

const if = 10;

if is a keyword. Zig uses it for conditional logic.

This is also invalid:

fn return() void {}

return is a keyword. Zig uses it to return from a function.

Common Keywords

You have already seen several keywords:

const std = @import("std");

pub fn main() void {
    var count: usize = 0;

    if (count == 0) {
        return;
    }
}

In this small program:

KeywordMeaning
constdeclares a name that cannot be reassigned
vardeclares a mutable variable
pubmakes a declaration public
fndefines a function
voidmeans no return value
ifstarts a conditional branch
returnexits a function

These words are part of Zig syntax.

Control Flow Keywords

Control flow decides which code runs.

Important control-flow keywords include:

KeywordMeaning
ifconditional branch
elsealternative branch
switchchoose between many cases
whileloop while a condition is true
forloop over items
breakleave a loop or block
continueskip to the next loop step
returnleave a function
deferrun code when leaving the current scope
errdeferrun code when leaving the scope because of an error

Example:

if (age >= 18) {
    return;
} else {
    // handle minor
}

Here, if, return, and else are keywords.

Type and Declaration Keywords

Some keywords create declarations or describe types.

KeywordMeaning
constimmutable binding
varmutable binding
fnfunction
structstructured data type
enumnamed set of values
unionvalue that can hold one of several forms
errorerror set
opaquetype with hidden representation
anytypegeneric parameter type
comptimecompile-time value or execution
pubpublic declaration
externdeclaration from another language or object file
exportmake a symbol available outside Zig
inlinerequest or require inline behavior

Example:

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

Here, const and struct are keywords.

Error Handling Keywords

Zig has explicit error handling.

Important error-related keywords include:

KeywordMeaning
tryreturn early if an error happens
catchhandle an error
errorcreate or refer to errors
errdeferdefer cleanup only on error

Example:

const file = try openFile();

This means: call openFile; if it returns an error, return that error from the current function.

Example with catch:

const value = parseNumber(text) catch 0;

This means: if parsing fails, use 0.

Compile-Time Keywords

Zig has strong compile-time programming.

The most important keyword here is:

comptime

Example:

fn add(comptime T: type, a: T, b: T) T {
    return a + b;
}

comptime T: type means the type T is known at compile time.

You will use comptime more later. For now, remember that Zig can run normal Zig logic during compilation.

Address Space and Calling Keywords

Some keywords are used in lower-level code, especially when working with hardware, operating systems, or C.

KeywordMeaning
addrspacespecifies an address space
alignspecifies memory alignment
allowzeroallows pointer address zero
callconvspecifies a calling convention
linksectionplaces code or data in a linker section
threadlocalgives each thread its own instance

Beginners do not need these immediately, but they are part of Zig because Zig is a systems language.

Other Important Keywords

Some keywords appear in special contexts.

KeywordMeaning
asminline assembly
asyncasynchronous function call
awaitwait for async result
nosuspendcall async code without suspension
resumeresume suspended async frame
suspendsuspend execution
testdefine a test block
usingnamespacebring declarations into scope

Example test:

test "addition works" {
    try std.testing.expect(1 + 1 == 2);
}

Here, test defines a Zig test block.

true, false, null, and undefined

These are not ordinary names.

They are built-in language values.

WordMeaning
trueboolean true
falseboolean false
nullno value for an optional
undefineduninitialized value
unreachablecode path that must not happen

Examples:

const enabled = true;
const maybe_number: ?i32 = null;
var buffer: [64]u8 = undefined;

true, null, and undefined already have language meaning, so you cannot use them as normal names.

Full Reserved Keyword List

These words are reserved by Zig:

addrspace
align
allowzero
and
anyframe
anytype
asm
async
await
break
callconv
catch
comptime
const
continue
defer
else
enum
errdefer
error
export
extern
fn
for
if
inline
linksection
noalias
nosuspend
opaque
or
orelse
packed
pub
resume
return
struct
suspend
switch
test
threadlocal
try
union
unreachable
usingnamespace
var
volatile
while

Do not use these as identifiers.

What Happens If You Use a Keyword as a Name

This fails:

const while = 10;

The compiler sees while and expects loop syntax, not a variable name.

Use a clearer name:

const loop_count = 10;

This also fails:

const struct = 123;

Use:

const struct_count = 123;

A keyword has grammar meaning. It cannot be reused as an ordinary name.

Builtins Are Different

Zig also has builtins that start with @.

Examples:

@import
@sizeOf
@alignOf
@intCast
@panic

These are not ordinary identifiers either, but they are visually distinct because they begin with @.

Example:

const std = @import("std");

You do not define @import. It is provided by the language.

Operators Are Also Reserved Syntax

Some words are operators too:

and
or
orelse

Example:

if (is_ready and has_permission) {
    // ...
}

and combines two boolean conditions.

or means one condition or the other may be true.

orelse is used with optionals:

const value = maybe_value orelse 0;

These words are part of expressions, so they cannot be used as variable names.

Naming Around Keywords

When a natural name conflicts with a keyword, choose a more specific name.

Instead of:

const error = 1;

Use:

const error_code = 1;

Instead of:

const type = "user";

Use:

const user_type = "user";

Instead of:

const test = true;

Use:

const test_enabled = true;

Specific names are usually better anyway.

The Main Idea

Reserved keywords are words Zig keeps for itself.

They define the grammar of the language: variables, functions, loops, branches, errors, structs, enums, tests, compile-time execution, and low-level system features.

You do not need to memorize every keyword immediately. You will learn them naturally as you use the language. For now, remember the rule: if Zig already uses a word as syntax, do not use it as your own name.