返回 2026-04-15
⚙️ 工程

Zig 0.16.0 发布:“Juicy Main”依赖注入功能上线Zig 0.16.0 release notes: "Juicy Main"

simonwillison.net·2026-04-15 节选正文

Zig 0.16.0 正式发布,其中引入了一个名为“Juicy Main”的重要特性——为程序入口函数 `main()` 提供依赖注入机制,允许接收 `process.Init` 参数。这一改进显著提升了程序的模块化和可测试性,使开发者能更灵活地管理初始化逻辑和外部依赖。

Simon Willison

15th April 2026 - Link Blog

Zig 0.16.0 release notes: "Juicy Main" (via) Zig has really good release notes - comprehensive, detailed, and with relevant usage examples for each of the new features.

Of particular note in the newly released Zig 0.16.0 is what they are calling "Juicy Main" - a dependency injection feature for your program's main() function where accepting a process.Init parameter grants access to a struct of useful properties:

const std = @import("std");

pub fn main(init: std.process.Init) !void {
    /// general purpose allocator for temporary heap allocations:
    const gpa = init.gpa;
    /// default Io implementation:
    const io = init.io;
    /// access to environment variables:
    std.log.info("{d} env vars", .{init.environ_map.count()});
    /// access to CLI arguments
    const args = try init.minimal.args.toSlice(
        init.arena.allocator()
    );
}

需要完整排版与评论请前往来源站点阅读。