这怎么能通过?
const std = @import("std");
pub fn main() !void {
const outcome =
std.debug.print("Hello, world\n", .{});
_ = outcome;
}
std.debug.print() 的结果是 void,但把结果绑定到 outcome 时,Zig 并不报错。
我在做项目时遇到了这个问题,当时误以为某个第三方库的 API 会返回值,其实并不会。后来用 {s} 把这个结果格式化成字符串时,编译器才报错,但从那里找到最初的错误假设花了些时间。
什么情况下把 void 存进变量会有用?
从英语翻译·查看原文How is this accepted?
const std = @import("std");
pub fn main() !void {
const outcome =
std.debug.print("Hello, world\n", .{});
_ = outcome;
}
The result of std.debug.print() is void, yet binding that result to outcome produces no complaint from Zig.
I encountered this while working on a project, after incorrectly thinking an API from a third-party library produced a value when it actually didn't. Eventually, formatting that result as a string using {s} caused a compiler error, though tracing it back to my initial assumption took some time.
When would storing void in a variable be helpful?