🔥 Zig Philosophy & Introduction
🎯 What is Zig?
Zig is a general-purpose, low-level programming language designed for robustness, optimal performance, and clarity. Created by Andrew Kelley, Zig offers manual memory control without macros or hidden allocations. It compiles to native code via LLVM, features compile-time code execution (comptime), and seamlessly integrates with C libraries. Zig prioritizes "no hidden control flow" and explicit error handling.
🔬 Core Pillars
- No hidden allocations: Every allocation is explicit via allocators.
- Comptime reflection: Generate types/code at compile time.
- Cross-compilation first-class: Build for any target from one machine.
- Error as values: No exceptions, explicit error unions.
- Zero-cost abstractions: High-level syntax with full control.
📦 Variables & Primitives
🎯 Variable declaration
var (mutable) and const (immutable). Zig enforces strict typing with inference via '='. Primitives: u8, i32, f64, bool, comptime_int, etc. Integer sizes explicit. Supports sentinel-terminated arrays and pointers.
🏷️ Primitive types
- u8, u16, u32, u64 – unsigned integers
- i8..i128 – signed integers
- f16, f32, f64, f128 – floating point
- bool, void, type – meta types
- optional (?T) and error unions
⚙️ Operators & Pointers
🎯 Arithmetic & Bitwise
Zig provides arithmetic: +, -, *, /, % (remainder). Bitwise: &, |, ^, <<, >>. Also @intCast, @ptrCast for safe conversions. Pointers: *T (single item) and [*]T (many). Address-of: &var.
📋 Special operators
- orelse – default for optionals
- catch – handle errors
- try – propagate error
🔀 Control Flow (if/switch)
🎯 If expressions & Switch
if works with booleans, optionals, error unions. Switch exhaustive, supports ranges and tuples. No implicit fallthrough.
🔄 Loops: while & for
🎯 while and for loops
while condition-based; supports continue/break. for iterates over arrays/slices. Inline for for comptime loops.
⚡ Functions & Defer
🎯 Functions as first-class
fn keyword, explicit return type, defer ensures cleanup, errdefer on error path. Supports inline and comptime.
📐 Arrays & Slices
🎯 Fixed-size arrays & slices
[N]T compile-time length; []T slice (ptr+len). Slices are bounds-checked by default. Multi-dimensional arrays supported.
🏗️ Structs & Enums
🎯 Structs with methods
Structs group fields, can have decls, methods via 'pub fn'. Enums are explicit with possible payloads. Default field values.
🛡️ Error Handling (try/catch)
🎯 Error Union (E!T)
Errors are values, defined with error{FileNotFound, AccessDenied}. Use try to bubble, catch to handle, orelse for defaults.
💾 Allocators & Memory
🎯 Explicit Allocators
Zig forces explicit memory management: std.heap.page_allocator, GeneralPurposeAllocator. allocator.alloc(T, n), free with allocator.free.
✨ Comptime (Compile-time)
🎯 Compile-time execution
comptime keyword runs code at compilation. Generic programming, type generation, and static evaluation. @typeInfo, @compileLog for reflection.
📍 Pointers & Optionals
🎯 Single-item & many-item pointers
*T pointer to one value, [*]T many. Optional ?T can be null, handled with orelse or if unwrap.
🔗 Unions & Tagged Unions
🎯 Tagged unions (sum types)
Unions without tag are unsafe; tagged unions (enum + union) provide safe discriminated unions. switch on the tag.
⏩ Async & Build System
🎯 Async functions & cross-compilation
Zig supports async/await for cooperative multitasking (optional). The build system (build.zig) provides declarative cross-compilation, package management, and C integration.
🔧 Modern Zig workflow
- build.zig – configure exe/lib, targets, modules.
- zig cc / zig c++ – drop-in C compiler replacement.
- Zero-dependency cross builds for Windows/Linux/macOS/WASM.