TExcelTAD
Home › Zig › Zig Syntax

Zig Syntax

Zig's syntax looks familiar if you have seen C, Rust or Go: curly braces for blocks, semicolons after statements and // for comments. This lesson walks through the shape of a Zig program so the following lessons can focus on one feature at a time.

The structure of a program

zig · try it yourself
const std = @import("std");

const greeting = "Welcome";

pub fn main() void {
    const name = "Zig learner";
    std.debug.print("{s}, {s}!\n", .{ greeting, name });
}
Welcome, Zig learner!
Part Meaning
const std = @import("std"); a top-level constant holding the standard library
const greeting = …; another top-level declaration, visible in the whole file
pub fn main() void { … } the entry point; pub = public, void = returns nothing
.{ greeting, name } an anonymous tuple with the values to print

A Zig source file is itself a struct: its top-level declarations are the struct's members. That is why @import("std") returns a value you can store in a constant, and why you reach into it with dots: std.debug.print.

Statements and semicolons

Every statement ends with a semicolon. Blocks { … } group statements. Indentation does not matter to the compiler, but zig fmt uses four spaces and almost all Zig code follows it.

Builtin functions

Names that start with @ are builtins, provided by the compiler itself rather than the standard library. You will meet many of them:

Builtin Purpose
@import load a file or package
@as give a value an explicit type
@intCast, @floatFromInt convert between number types
@TypeOf, @typeName inspect types
@sizeOf size of a type in bytes
zig · try it yourself
const std = @import("std");

pub fn main() void {
    const small = @as(u8, 200);
    std.debug.print("value: {d}\n", .{small});
    std.debug.print("type: {s}\n", .{@typeName(@TypeOf(small))});
    std.debug.print("bytes: {d}\n", .{@sizeOf(u64)});
}
value: 200
type: u8
bytes: 8

Identifiers

Names may contain letters, digits and underscores and must not start with a digit. The conventions used by the standard library are:

Kind Style Example
variables, constants, fields snake_case max_speed
functions camelCase readFile
types (structs, enums, …) TitleCase HashMap

If you really need a name that clashes with a keyword, wrap it as @"name", for example @"type".

Unused things are errors

Zig refuses to compile code with an unused local constant, variable or function parameter. That keeps code tidy, but it surprises newcomers:

zig · try it yourself
const std = @import("std");

pub fn main() void {
    const unused = 42;
    std.debug.print("done\n", .{});
}
./example.zig:4:11: error: unused local constant
    const unused = 42;
          ^~~~~~

To deliberately ignore a value, assign it to the special name _:

zig · try it yourself
const std = @import("std");

pub fn main() void {
    const unused = 42;
    _ = unused; // explicitly discarded
    std.debug.print("done\n", .{});
}
done

Expressions everywhere

Many constructs that are statements in C are expressions in Zig — if, switch and labeled blocks all produce values. You will see this pattern often:

zig
const label = if (score >= 50) "pass" else "fail";

Tip: Run zig fmt on your files. It fixes indentation and spacing, and a trailing comma inside a list tells it to put each item on its own line.

Test yourself

What do names starting with @, such as @import, refer to?

  1. Macros
  2. Compiler builtin functions
  3. Annotations
  4. Private variables

How do you silence the error for an unused local constant x?

  1. // ignore x
  2. _ = x;
  3. #[allow(unused)]
  4. delete x;

Which naming style does the Zig standard library use for types?

  1. snake_case
  2. camelCase
  3. TitleCase
  4. SCREAMING_CASE