</>

Zig Cheat Sheet

Practical Zig cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.

zig syntax examples reference

Zig cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the Nginx Cheat Sheet and Playwright Cheat Sheet.

Setup and Tooling

Goal: Install and verify language toolchain

# Run zig command
zig version

# Run zig command
zig init-exe

# Run zig command
zig env

Core Workflows

Goal: Run and build source code

# Execute zig workflow
zig run src/main.zig

# Execute zig workflow
zig build

Goal: Use a practical code snippet

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello Zig\n", .{});
}

Testing and Formatting

Goal: Run automated tests

# Execute test-related command
zig test src/main.zig

# Execute test-related command
zig build test

Goal: Run lint and formatter checks

# Execute quality command
zig fmt src/main.zig

# Execute quality command
zig fmt .

Automation and CI

Goal: Create a repeatable CI shell step

# Run strict shell mode for CI step
set -euo pipefail

# Install dependencies deterministically
npm ci || pip install -r requirements.txt || true

# Run tests and fail fast on errors
zig test src/main.zig

Debugging and Troubleshooting

Goal: Trace runtime issues

# Print runtime and package manager versions
node --version || python --version || true

# Search stack traces and TODO markers
rg "Traceback|Exception|TODO" .

# Re-run tests with verbose output
zig test src/main.zig -v || zig test src/main.zig --verbose || true

Common Gotchas

  • Pin Zig language/runtime versions in local and CI environments.
  • Keep formatting and linting automated to avoid noisy diffs in code review.
  • Prefer explicit dependency lock files for reproducible builds.
  • Write small, deterministic tests that can run quickly in pre-commit checks.
  • Document compiler/interpreter flags used in production builds.

Related Cheat Sheets