Skip to main content

Zig Patterns

Voltaire uses Zig 0.15.1 for performance-critical implementations. This guide covers style, memory management, and common patterns.

Style Guide

Variable Naming

Single-word variables when meaning is clear:
Use descriptive names when ambiguity exists:

Function Structure

Prefer long imperative function bodies over small abstractions:

Only Abstract When Reused

Memory Management

Allocator Convention

Functions that allocate take an explicit allocator:

Memory Ownership

Return to caller, caller frees:

defer and errdefer

Always clean up with defer/errdefer:

ArrayList (0.15.1 API)

Zig 0.15.1 uses unmanaged ArrayList. This is different from older versions.

ArrayList Operations

Struct Pattern

Simple Data Struct

Using @This()

Error Handling

Error Sets

Error Union Returns

Testing

Inline Tests

Tests live in the same file as implementation:

Testing Utilities

Running Tests

Constant Time Operations

Security-critical code must be constant-time.

Comptime

Use comptime for zero-cost abstractions:

Common Mistakes

Avoid these patterns: