Zig
The reason why we can often get away with using languages like Python or JavaScript to drive resource-intensive computations, is because under the hood somebody took years to perfect a C implementation of a key procedure and shared it with the world under a permissive license. - Maintain it With Zig / HN / Home
Zig position itself as better replacement to C.
- It is a direct (close to system)
- and simple langage like C, python, Visual Basic, Go…
see also
- Why we still use C despite so many C alternatives - covers:
- Zig
- Odin
- C3
# Introduction to Zig
main.zig
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, Zig!\n", .{});
}Then to run
- work from scracth with vscode .runner
zig run main.zig- compile & runzig build-exe main.zig- materialize binary./main- to execute
# RAII
Zig does not have RAII in the C++ sense, but it does support deterministic resource management through explicit defer/errdefer and scope-based cleanup - ChatGPT
# Object
Zig does not have classes or objects in the traditional OOP sense. - ChatGPT
Zig uses struct to group data and behavior.
- Methods are just functions with an explicit self parameter (like python)
- No inheritance (by design)
# Zig function
Zig function can return
- Primitive values
- Composite types
- Pointers and references
- Optional values
- Error unions
- Anyerror or specific error sets
- Types themselves (comptime)
# Install
As of now, Zig is generally not included in the official Debian repos. The easiest way seems to use Hombrew or Nix (prefered for linux)
# Nix Install
Using nix profile
$ nix profile add nixpkgs#zig# C++ interop
# Zig Interop Overhead Table with Metrics
These are rough per-function call latencies on modern desktop CPUs. Actual values vary based on OS, CPU, type conversion, and data size.
| Language | Interop Method | Typical Workflow | Overhead Level | Approx. Call Latency* |
|---|---|---|---|---|
| Java | JNI | Zig shared lib loaded via System.loadLibrary() |
Low | ~50–200 ns per native call |
| JNA/JNR | Reflection-based FFI | Medium | ~1–5 µs per call | |
| IPC (sockets, REST) | Zig as separate process | High | ~100 µs – 1 ms per request | |
| C++ | C ABI wrappers | extern "C" |
Low | ~5–50 ns per call |
| Direct C++ interop | @cImport + C++ |
Medium | ~50–200 ns (depends on ABI, exceptions) | |
| Ruby | C extension | Zig lib loaded as native Ruby extension | Low | ~50–200 ns per call |
Ruby FFI (ffi gem) |
Zig C ABI lib via FFI | Medium | ~1–5 µs per call | |
| Python | CPython C API | Zig compiled as Python module | Low | ~50–200 ns per call |
ctypes/cffi |
Zig C ABI lib loaded in Python | Medium | ~1–10 µs per call | |
| General | IPC | Sockets/pipes/gRPC | High | ~100 µs – 1+ ms per round-trip |
# see also
- Opinion piece: On Zig (and the design choices within) / HN
- Zig Toolchain - Zig has its own build system, You define builds with Zig code.
- Carbon Language: An experimental successor to C++ - arbon exists so that it’s possible to migrate a large C++ code base, like Chrome, from C++ to something saner, incrementally.
- Zig is designed to interoperate like this with C, and Kotlin with Java. - HN
- My game development journey & why I’m increasing my contribution to Zig to $200/mo
- What’s Zig got that C, Rust and Go don’t have? (with Loris Cro)
- rust add complexity & prefer safety over performance
- go toward simplicity, but not in minimalistic way (different way of seeing things). Can reuse C, but not be consumed by C.
- zig allow low system programming
- on linux call syscall directly (no need fot clib)
- From Go to Zig
- Why Zig When There is Already C++, D, and Rust?
- Zig feels more practical than Rust for real-world CLI tools