Skip to the content ForoHackers
Log inJoin

Zig: start here

Zig
#3057 · · Quote · Report

Zig is a low-level systems language that aims to be a simpler, more explicit C. It has no hidden control flow and no hidden memory allocation: if code allocates, you see an allocator passed in. It also ships a build system and a C-compatible toolchain, so it is often used to build C projects and for cross-compilation.

Key concepts

  • Explicit allocators: functions that need memory take an allocator, so every allocation is visible and swappable.
  • Comptime: ordinary code run at compile time, replacing macros and most generics.
  • Error unions: errors are values in the return type, propagated with try and handled with catch.
  • Optionals: a value that may be absent is a distinct type, not a null pointer.
  • C interoperability: C headers can be imported and C code compiled alongside Zig.
  • Safety modes: checks for overflow and bounds in debug builds that can be removed for speed.

Vocabulary

  • Slice: a pointer paired with a length, the usual way to pass arrays.
  • Defer and errdefer: cleanup that runs at scope exit, or only when an error is returned.
  • Cross-compilation: building for another operating system or CPU from the same machine.
  • Build script: the build is itself a Zig program that describes the steps.

What to study, in order

  1. Basic syntax, types and control flow from the language's own documentation.
  2. Write a small command-line tool, passing allocators and handling errors with try.
  3. Build a dynamic array and a hash map yourself to understand allocators.
  4. Use comptime to write a generic function and a type-level check.
  5. Call a C library from Zig, then compile your program for a different target.

Related tags

#c #rust #memory-management #toolchains

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply