⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.18.0-dev.624+b7f2c6d8fWhat's newOn an older Zig?Blograylib-zig

Language

This section teaches the language itself, with one runnable page for each idea. The first topics are integer and float rules, pointers and slices, optionals and error unions, and structs, unions and enums. The other topics are switch, the loop forms, comptime and SIMD vectors. Each page states the rule. Then the page proves the rule with a program that you can edit in place.

  1. There is no null. An optional ?T is a separate type. The compiler does not let you read an optional before you unwrap it.
  2. Errors are values in the return type, not exceptions. try is a short form that returns the error to the caller.
  3. In Debug and ReleaseSafe, integer overflow stops the program. It does not wrap. If you want wraparound arithmetic, use +%.
  4. comptime is not a macro language. comptime is the same Zig, run at an earlier time. Thus a generic container is a function that returns a type.

35 chapters.

  • Assignmentconst by default, and var only when you must change the value.
  • 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 over a bare counter.
  • FunctionsImmutable parameters and explicit discards.
  • DeferCleanup code that runs on each 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 the same as 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, in 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.
  • Labelled SwitchA switch that you can jump back into with a new value.
  • Loops as ExpressionsThe else branch gives the value when no break runs.
  • Payload CapturesThe |value| syntax that unwraps all wrapper types.
  • OptionalsThe Zig answer to null, checked by the compiler.
  • ComptimeUsual Zig code that the compiler runs.
  • InterfacesNo interface keyword. Four shapes instead, and what each shape costs.
  • Inline LoopsUnrolled loops where each iteration can have a different type.
  • OpaqueTypes with unknown size, used through pointers only.
  • Anonymous StructsStruct literals with an inferred type, and tuples.
  • Sentinel TerminationSequences that end in a known marker.
  • VectorsSIMD with ordinary operators.
  • ImportsFiles are structs, and pub controls the boundary.
  • Array RecipesGrids, lookup tables made at compile time, and how to get an array from a slice.
  • Slice RecipesReverse, rotate, shift and search a slice, in place and without an allocator.