# Reserved Keywords

### Reserved Keywords

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:

```zig
const if = 10;
```

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

This is also invalid:

```zig
fn return() void {}
```

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

#### Common Keywords

You have already seen several keywords:

```zig
const std = @import("std");

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

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

In this small program:

| Keyword | Meaning |
|---|---|
| `const` | declares a name that cannot be reassigned |
| `var` | declares a mutable variable |
| `pub` | makes a declaration public |
| `fn` | defines a function |
| `void` | means no return value |
| `if` | starts a conditional branch |
| `return` | exits a function |

These words are part of Zig syntax.

#### Control Flow Keywords

Control flow decides which code runs.

Important control-flow keywords include:

| Keyword | Meaning |
|---|---|
| `if` | conditional branch |
| `else` | alternative branch |
| `switch` | choose between many cases |
| `while` | loop while a condition is true |
| `for` | loop over items |
| `break` | leave a loop or block |
| `continue` | skip to the next loop step |
| `return` | leave a function |
| `defer` | run code when leaving the current scope |
| `errdefer` | run code when leaving the scope because of an error |

Example:

```zig
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.

| Keyword | Meaning |
|---|---|
| `const` | immutable binding |
| `var` | mutable binding |
| `fn` | function |
| `struct` | structured data type |
| `enum` | named set of values |
| `union` | value that can hold one of several forms |
| `error` | error set |
| `opaque` | type with hidden representation |
| `anytype` | generic parameter type |
| `comptime` | compile-time value or execution |
| `pub` | public declaration |
| `extern` | declaration from another language or object file |
| `export` | make a symbol available outside Zig |
| `inline` | request or require inline behavior |

Example:

```zig
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:

| Keyword | Meaning |
|---|---|
| `try` | return early if an error happens |
| `catch` | handle an error |
| `error` | create or refer to errors |
| `errdefer` | defer cleanup only on error |

Example:

```zig
const file = try openFile();
```

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

Example with `catch`:

```zig
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:

```zig
comptime
```

Example:

```zig
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.

| Keyword | Meaning |
|---|---|
| `addrspace` | specifies an address space |
| `align` | specifies memory alignment |
| `allowzero` | allows pointer address zero |
| `callconv` | specifies a calling convention |
| `linksection` | places code or data in a linker section |
| `threadlocal` | gives 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.

| Keyword | Meaning |
|---|---|
| `asm` | inline assembly |
| `async` | asynchronous function call |
| `await` | wait for async result |
| `nosuspend` | call async code without suspension |
| `resume` | resume suspended async frame |
| `suspend` | suspend execution |
| `test` | define a test block |
| `usingnamespace` | bring declarations into scope |

Example test:

```zig
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.

| Word | Meaning |
|---|---|
| `true` | boolean true |
| `false` | boolean false |
| `null` | no value for an optional |
| `undefined` | uninitialized value |
| `unreachable` | code path that must not happen |

Examples:

```zig
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:

```text
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:

```zig
const while = 10;
```

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

Use a clearer name:

```zig
const loop_count = 10;
```

This also fails:

```zig
const struct = 123;
```

Use:

```zig
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:

```zig
@import
@sizeOf
@alignOf
@intCast
@panic
```

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

Example:

```zig
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:

```zig
and
or
orelse
```

Example:

```zig
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:

```zig
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:

```zig
const error = 1;
```

Use:

```zig
const error_code = 1;
```

Instead of:

```zig
const type = "user";
```

Use:

```zig
const user_type = "user";
```

Instead of:

```zig
const test = true;
```

Use:

```zig
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.

