TExcelTAD
Home › Zig › Zig vs C and Rust

Zig vs C and Rust

Zig, C and Rust all target the same kind of work: software where performance, memory layout and control matter. They make very different trade-offs, though. Knowing those trade-offs helps you choose the right tool — and understand why Zig looks the way it does.

The one-line summary

Language Philosophy
C trust the programmer; minimal language, maximal freedom
Rust prove memory safety at compile time with ownership and borrowing
Zig keep the language small and explicit; catch bugs with runtime safety checks and good defaults

Side by side

Topic C Rust Zig
Memory management manual malloc/free ownership, automatic drop manual, explicit allocators, defer
Use-after-free / data races not prevented prevented at compile time detected in some cases (DebugAllocator), not proven
Null any pointer Option<T> optional ?T
Errors return codes / errno Result<T, E> + ? error unions + try
Overflow UB (signed) / wraps (unsigned) panics in debug, wraps in release panics in safe modes, explicit +% +|
Generics macros, void * traits + generics comptime parameters
Metaprogramming preprocessor macros, proc-macros comptime (plain Zig)
Hidden control flow macros can hide anything Drop, operator overloading, Deref none
Build tool make/CMake Cargo zig build
C interop native via bindgen/FFI direct @cImport
Learning curve small language, many pitfalls steep (borrow checker) moderate
Stability decades stable since 2015 pre-1.0, breaking changes

The same task in Zig

Here is a small task — parse numbers from text and report errors — written in idiomatic Zig. Compare it mentally with how you'd write it in C (check errno or a return code after every call) or Rust (Result and ?).

zig · try it yourself
const std = @import("std");

fn sumCsv(line: []const u8) !i64 {
    var total: i64 = 0;
    var fields = std.mem.splitScalar(u8, line, ',');
    while (fields.next()) |field| {
        const trimmed = std.mem.trim(u8, field, " ");
        total += try std.fmt.parseInt(i64, trimmed, 10);
    }
    return total;
}

pub fn main() void {
    const lines = [_][]const u8{ "1, 2, 3", "10,-4, 7", "5, five, 6" };
    for (lines) |line| {
        if (sumCsv(line)) |total| {
            std.debug.print("{s:<12} -> {d}\n", .{ line, total });
        } else |err| {
            std.debug.print("{s:<12} -> error: {t}\n", .{ line, err });
        }
    }
}
1, 2, 3      -> 6
10,-4, 7     -> 13
5, five, 6   -> error: InvalidCharacter

Like Rust, the error is part of the type and try propagates it. Like C, there is no hidden allocation (the splitting and trimming just create slices into line), and nothing runs that you can't see.

Where Zig's safety comes from

Zig does not have a borrow checker. Instead, it relies on:

  • Types that rule out whole bug classes: non-null pointers by default, slices with lengths, no implicit narrowing conversions, exhaustive switches.
  • Runtime checks in Debug and ReleaseSafe: overflow, bounds, null unwrap, union field access, unreachable.
  • Tooling: DebugAllocator and std.testing.allocator report leaks, double frees and some use-after-free bugs.
zig · try it yourself
const std = @import("std");

pub fn main() void {
    // Each line below would be silent undefined behaviour in C.
    const values = [_]u8{ 250, 251, 252 };
    var total: u8 = 0;
    for (values) |v| {
        total, const overflow = @addWithOverflow(total, v);
        if (overflow == 1) {
            std.debug.print("overflow detected after adding {d}\n", .{v});
            break;
        }
    }
    const index: usize = 5;
    if (index < values.len) {
        std.debug.print("value: {d}\n", .{values[index]});
    } else {
        std.debug.print("index {d} is out of bounds (len {d})\n", .{ index, values.len });
    }
}
overflow detected after adding 251
index 5 is out of bounds (len 3)

The trade-off: a Rust program that compiles is free of memory-safety bugs (outside unsafe); a Zig program needs testing in safe modes to find them. In return, Zig code is often shorter, simpler to read, and easier to write for low-level tasks like allocators and data structures with shared pointers.

Choosing

Choose When
C you need the widest platform reach, an existing C codebase, or a stable decades-long standard
Rust safety guarantees matter most, large teams, long-lived networked services
Zig you want C-level control with modern ergonomics, easy cross-compilation, painless C interop, or explicit memory strategies (games, embedded, tooling)

They also combine well: Zig can build and call C directly, and both Zig and Rust can expose and consume C ABIs, so mixed projects are common.

Tip: Even if you write mostly C, try zig cc as your compiler: you get cross-compilation and a modern toolchain without changing a line of code.

Test yourself

How does Zig mainly catch memory and arithmetic bugs that Rust rules out at compile time?

  1. A garbage collector
  2. A borrow checker
  3. Runtime safety checks in safe build modes plus debug allocators and tests
  4. It doesn't catch any

Which feature do Zig and Rust share for error handling?

  1. Exceptions
  2. Errors are part of the return type and propagated with try / ?
  3. errno
  4. Signals

What does "no hidden control flow" rule out in Zig?

  1. Loops
  2. Operator overloading and destructors that run invisibly
  3. Function calls
  4. Recursion