⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.17.0-dev.1503+1f1bee62eOn an older Zig?

Language

The language itself, one runnable page per idea: integer and float rules, pointers and slices, optionals and error unions, structs, unions and enums, switch, the loop forms, comptime, and SIMD vectors. Each page states the rule and then proves it with a program you can edit in place.

  1. There is no null. An optional ?T is its own type, and the compiler will not let you read one without unwrapping it first.
  2. Errors are values in the return type, not exceptions. try is shorthand for returning one to the caller.
  3. Signed overflow is a crash in Debug and ReleaseSafe, not a wrap. If you want wrapping, ask for it with +%.
  4. comptime is not a macro language. It is the same Zig, run earlier, which is why a generic container is a function that returns a type.

31 chapters.

  • Assignmentconst by default, var only when you must mutate.
  • ArraysFixed-length sequences whose length is part of the type.
  • If ExpressionsNo truthiness: if takes a bool.
  • While LoopsConditions, continue expressions, and loops that produce values.
  • For LoopsIterate over sequences and ranges, never a bare counter.
  • FunctionsImmutable parameters and explicit discards.
  • DeferCleanup that runs on every path out of a scope.
  • ErrorsErrors are values in a union, not exceptions.
  • SwitchExhaustive by construction.
  • Runtime SafetyChecked illegal behaviour, and where the checks go.
  • PointersSingle-item pointers that are never null.
  • Pointer Sized Integersusize, isize, and why they are not just u64.
  • Many-item Pointers[*]T: a pointer to an unknown number of items.
  • SlicesA pointer and a length, together.
  • EnumsNamed values, fixed tags, and methods.
  • StructsLayout, defaults, and methods.
  • UnionsOne of several types, sharing one allocation.
  • Integer RulesArbitrary widths, explicit narrowing, chosen overflow behaviour.
  • FloatsIEEE-754 types and explicit conversions.
  • Labelled BlocksBlocks that produce a value.
  • Labelled LoopsBreak or continue an outer loop by name.
  • Loops as Expressionselse supplies the value when nothing breaks.
  • Payload CapturesThe |value| syntax that unwraps everything.
  • OptionalsZig's answer to null, checked by the compiler.
  • ComptimeOrdinary Zig, executed by the compiler.
  • Inline LoopsUnrolled loops where each iteration can differ in type.
  • OpaqueTypes with unknown size, used through pointers only.
  • Anonymous StructsInferred struct literals, and tuples.
  • Sentinel TerminationSequences that end in a known marker.
  • VectorsSIMD with ordinary operators.
  • ImportsFiles are structs, and pub controls the boundary.