liveBook · Manning

Zig

As explained in Systems Programming with Zig

Zig is a general-purpose systems programming language designed for reliable, maintainable, performant software with minimal dependencies and explicit control over low-level behavior.

When a service must stay reliable without wasting resources, Zig gives its programmer direct control rather than hiding the machinery. Zig is a systems programming language oriented toward simple, maintainable, performant software with minimal dependencies. Its approach favors understanding and careful planning over the fastest possible prototype. section 1.0 section 1.7.3

Zig works alongside C and C++ rather than trying to replace them. C ABI compatibility helps Zig libraries expose interfaces to existing software, while the build system supports cross-compilation and multi-language projects. Its bundled formatter enforces one code style, and its build tooling supports hermetic builds and reproducible package management. section 1.2 section 1.7.1 section 1.2.3 section 1.8

Zig makes several responsibilities explicit:

  • Handle errors and return values, or explicitly discard them. The compiler checks that these decisions are not silently skipped. section 1.3.1
  • Use const for a variable that is not reassigned; declaring an unchanged variable as var is an error. Unused variables and parameters must also be explicitly discarded. section 1.3.1
  • Choose how memory is managed and how long data lives. Manual management offers control and configurability, but demands planning that garbage collection can avoid. section 1.3.3
  • Treat undefined behavior deliberately. Zig includes safety checks and pointer types that help make unsafe memory operations harder to invoke accidentally. section 1.2.3

The type system supplies numeric and logical types, pointers, arrays, and slices. Strings are bytes in memory with an encoding; structs group values of potentially different types. The special types type and comptime_int support comptime features, while interfaces underpin standard-library facilities such as readers, writers, and allocators. section 2.4 section 1.8

Picking a type for 42

The integer 42 can fit in either i32 or u8. The first is a signed 32-bit integer; the second is an unsigned 8-bit integer. Zig also supports integer types with other bit widths, so the choice can reflect the data being represented. section 2.1.2

Table 2.1 Basic Zig types
Name Explanation

bool

Logical type. true or false.

i32, i64, i17 etc. (i followed by a number of bits)

Signed (two’s-complement) integer type. In practice, you will mostly use i32 and i64, but integers can have any number of bits, from 0 to 65535.

u8, u32, u64, u23 etc. (u followed by a number of bits)

Unsigned integer type. In practice, you will mostly use u8 and maybe u32, but unsigned integers can have any number of bits, from 0 to 65535.

usize and isize

Special unsigned and signed integer types intended for usage as indices or offsets. These are equivalent to Rust’s usize and isize, and C/C++'s size_t and isize_t.

f16, f32, f64, f80, f128

Standard floating point types.

void

You can think of this as a type that represents nothing, nada, zilch. More formally, there is only one possible value of void. For the C programmers out there, Zig’s void type is similar to C’s void return type, but has nothing to do with C’s void * type.

type

Types in Zig are actually values, too! More specifically, type annotations in Zig are expressions like any other expression. With comptime, we can even create functions that generate new types, a common pattern for generic data structures.

comptime_int, comptime_float

Special types, mainly for integer and float constants, that can’t exist at runtime. These are arbitrarily precise, so that they can coerce to any integer or float type that can hold them. For now, you can treat this as a curiosity.

Compare Zig’s basic logical, integer, floating-point, and other types. section 2.1.2

Zig fits system libraries, firmware, operating systems, servers, plugins, WebAssembly, and performance-sensitive applications. It can integrate with application frameworks that expose a C interface, but may be overkill for an unoptimized GUI or a quick prototype. Its strictness can frustrate newcomers: read compiler errors closely, and expect to take responsibility for behavior that other languages might hide. section 1.7.1 section 1.7.2 section 1.7.5 section 1.7.6

How Systems Programming with Zig covers it

How deep it goes
Introduces design trade-offs and compiler rules, then surveys basic types and project structure; full implementations are planned through practical projects.
Examples
Uses a numeric-type exercise around 42 and identifies servers, firmware, plugins, and WebAssembly as applications.
What it assumes
Assumes programming experience and familiarity with systems problems, while introducing Zig’s basic types and explicit style.
What you can do after
Evaluate Zig for systems projects by weighing explicit control and reliability against planning effort and prototyping speed.
How it connects
Connects C interoperability, manual memory management, error handling, cross-compilation, and compile-time metaprogramming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.