
Zig là ngôn ngữ lập trình hệ thống hiện đại, tối ưu hiệu năng, độ an toàn và khả năng đọc. Zig phát triển từ 2015 bởi Andrew Kelley, với triết lý: keep it simple, don’t hide control flow, explicit over implicit. Không garbage collector, không runtime ẩn, không hidden allocations.

Tại sao Zig, không phải Rust/C++?
Rust giải quyết memory safety qua borrow checker — compile time phức tạp, learning curve cao. C++ linh hoạt nhưng UB (undefined behavior) phổ biến, toolchain phức tạp. Zig chọn con đường giữa:
- Memory safety có chọn lọc: No null, no buffer overflow (bound checking option), no use-after-free. Không cần borrow checker.
- Compile time execution: Hàm chạy lúc compile bằng
comptime— meta-programming trong ngôn ngữ, không cần macro phức tạp. - Cross-compilation zero-config: Build từ Mac → Linux ARM, Windows → WebAssembly chỉ bằng
zig build -Dtarget=.... Không cần sysroot, cross-compiler riêng. - C interop hoàn hảo: Include header C trực tiếp, không cần binding generator. Link với C object file tự nhiên.
Syntax cốt lõi: Simple, No Magic
Zig loại bỏ:
- No macros, no annotations, no attributes đặc biệt.
- No operator overloading (trừ optional chaining).
- No implicit allocations.
- No try-catch globally (explicit error union).
Error Handling: Error Union Type
fn parsePositiveInt(str: []const u8) !u32 {
const num = try std.fmt.parseInt(u32, str, 10);
if (num == 0) return error.ZeroNotAllowed;
return num;
}
// Usage
const result = parsePositiveInt("42");
if (result) |value| {
std.debug.print("Value: {}n", .{value});
} else |err| {
std.debug.print("Error: {}n", .{err});
}
T → success, error{E} → failure. try propagate error, catch handle. Explicit mọi bước.

Comptime: Code viết code
comptime giá trị chạy lúc compile. Hàm generic không cần template<typename T>:
fn max(comptime T: type, a: T, b: T) T {
if (a > b) return a else return b;
}
// Tự tạo phiên bản cho u32, f32, i64...
const x = max(u32, 1, 2);
const y = max(f32, 3.14, 2.71);
// Compile time JSON parser:
const json = comptime std.json.parseFromSlice(
std.json.Value, allocator, input, .{}
);
Memory Management: Explicit & Optional
Zig không force garbage collector. Developer chọn:
- Stack allocation:
var buf: [1024]u8 = undefined; - Allocator interface: Pass
std.mem.Allocatorvào hàm. Có general purpose (Arena, GPA) hoặc custom (pool, stack-based). - No implicit heap:
+mảng string không allocate. Dùngtry std.fmt.allocPrintnếu cần.
C Interop: Zig wrap C code zero-friction
File build.zig link C library:
exe.linkLibC();
exe.addIncludePath(.{ .path = "libpng/include" });
exe.addLibraryPath(.{ .path = "libpng/lib" });
exe.linkSystemLibrary("png");
Zig generate header từ Zig code:
zig translate-c -lc lib.h
zig build-lib -dynamic -I. zig_wrapper.zig
Zig Build System: Cross-compilation Native
build.zig declarative. Ví dụ build native + cross-target:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "myapp",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
// Cross-compile to Linux ARM with one flag:
// zig build -Dtarget=aarch64-linux-musl
}
So sánh nhanh Zig vs Rust vs C++
| Đặc điểm | Zig | Rust | C++ |
|---|---|---|---|
| Memory safety | Optional, explicit | Mandatory, borrow checker | Opt-in via sanitizer |
| Generics | comptime, no monomorphization fees | Monomorphization | Templates, complex |
| Cross-compile | Zero-config, built-in | Custom target files | Toolchain riêng |
| C interop | Zero-cost, header import | Unsafe FFI | Header include |
| Learning curve | Thấp (30k LOC std) | Cao | Rất cao |
| Compile time | Nhanh (incremental cache) | Chậm (morphism) | Trung bình |
Use case thực tế Zig
- System programming: OS kernel, bootloader, firmware.
- Game engine: Component của Godot 4 (Zig module).
- Embedded/IoT: No runtime, small binary, cross-compile native.
- CLI tool: Replacement cho C/C++ tools cần hiệu năng cao, build nhanh.
- WebAssembly: Compile trực tiếp, runtime nhẹ, interop JS dễ.
Lộ trình học Zig cho developer Việt Nam
- Đọc Zig Learn (chương 1-6) — 1 tuần.
- Làm Zig Samples (HTTP server, SQLite, JSON parser).
- Thử rewrite một tool nhỏ từ C/Rust sang Zig.
- Dự án thật: CLI tool, TUI app, Zig + WebAssembly demo.
