Saltar al contenido ForoHackers

Zig: start here

Zig
#3057 · · Citar · Denunciar

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

No has iniciado sesión, así que esto se publica como anónimo: no se guarda con ello ninguna cuenta, nombre ni dirección, y no puede llevar enlaces ni imágenes. Inicia sesión o únete para publicar con tu nombre. Los registros de nuestro servidor web guardan la dirección y la hora de cada petición hasta 14 días; para que la tuya no quede en ellos, usa Tor. Para escribir al equipo de forma confidencial, usa el buzón. Firmar como fuente habitual (una clave guardada solo en este navegador)

Enviar tarda unos segundos aquí: tu navegador resuelve antes una prueba de trabajo contra el spam.

Responder