错误:根源源码文件的结构体 'std' 没有名为 'io' 的成员

编程语言 2026-07-09

我尝试在Zig 0.16中打印一个Hello World程序,但遇到了一个错误。

我的代码片段是:

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("hello, world\n", .{});
}

错误是:

src/main.zig:4:24: error: root source file struct 'std' has no member named 'io'
    const stdout = std.io.getStdOut().writer()

解决方案

从0.16版本开始有一个新的Io接口:https://ziglang.org/download/0.16.0/release-notes.html#IO-as-an-Interface

结合了强大的main函数,Hello World大致是这样的:

const std = @import("std");

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    try std.Io.File.stdout().writeStreamingAll(io, "Hello, world!\n");
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章