Zig调用print函数报错:expected error union type, found 'void'
Zig编译错误:
try误用在返回void的print函数上 初始代码
const bufPrint = @import("std").fmt.bufPrint; const print = @import("std").debug.print; const DefaultPrng = @import("std").rand.DefaultPrng; fn qr(comptime a: []const u8) !void { print("\n{s}\n", .{a}); } fn coati(comptime a: []const u8, b: i32) ![]const u8 { var buffer: [64]u8 = undefined; return try bufPrint(&buffer, "{d}: a {s} number.", .{b, a}); } fn tapir(comptime a: []const u8) ![]const u8 { var buffer: [64]u8 = undefined; return try bufPrint(&buffer, "Checked by {s}.", .{a}); } pub fn main() !void { const qa = @constCast(&DefaultPrng.init(0)).random().intRangeAtMost(i32, 0, 59); // const qb = qa / 20; try qr("Statements."); if (qa < 20) { const head = try coati("lesser", qa); const tail = try tapir("if-else statement"); try print("{s} {s}\n", .{head, tail}); } else if (qa < 40) { const head = try coati("middle", qa); const tail = try tapir("if-else statement"); try print("{s} {s}\n", .{head, tail}); } else { const head = try coati("greater", qa); const tail = try tapir("if-else statement"); try print("{s} {s}\n", .{head, tail}); } }
编译错误信息
error: expected error union type, found 'void' try print("{s} {s}\n", .{head, tail}); ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~ referenced by: callMain: C:\ProgramData\chocolatey\lib\zig\tools\zig-windows-x86_64-0.12.0\lib\std\start.zig:511:32 WinStartup: C:\ProgramData\chocolatey\lib\zig\tools\zig-windows-x86_64-0.12.0\lib\std\start.zig:350:45 remaining reference traces hidden; use '-freference-trace' to see all reference traces
修改后的代码(仍存在相同错误)
const bufPrint = @import("std").fmt.bufPrint; const print = @import("std").debug.print; const DefaultPrng = @import("std").rand.DefaultPrng; fn qr(comptime a: []const u8) !void { print("\n{s}\n", .{a}); } fn coati(comptime a: []const u8, b: i32, c: []u8) ![]const u8 { return try bufPrint(c, "{d}: a {s} number.", .{b, a}); } fn tapir(comptime a: []const u8, c: []u8) ![]const u8 { return try bufPrint(c, "Checked by {s}.", .{a}); } pub fn main() !void { const qa = @constCast(&DefaultPrng.init(0)) .random() .intRangeAtMost(i32, 0, 59); try qr("Statements."); if (qa < 20) { var kant: [64]u8 = undefined; const face = try coati("lesser", qa, &kant); const butt = try tapir("if-else statement", &kant); try print("{s} {s}\n", .{face, butt}); } else if (qa < 40) { var kant: [64]u8 = undefined; const face = try coati("middle", qa, &kant); const butt = try tapir("if-else statement", &kant); try print("{s} {s}\n", .{face, butt}); } else { var kant: [64]u8 = undefined; const face = try coati("greater", qa, &kant); const butt = try tapir("if-else statement", &kant); try print("{s} {s}\n", .{face, butt}); } }
问题分析与解决方法
核心错误原因
错误提示已明确指出:try关键字仅能用于调用返回错误联合类型的函数,但std.debug.print返回值为void——它不会抛出任何错误,因此不能在print前添加try。此前对coati/tapir字符串分配问题的推测是误判,实际问题出在print语句的try误用。
解决步骤
- 移除
print前的try:将所有try print(...)改为print(...),直接调用即可。 - 修复buffer复用问题:修改后的代码中,同一个
kantbuffer被coati和tapir先后使用,第二次bufPrint调用会覆盖第一次的内容,导致face指向的字符串被破坏。需为两个函数分配独立buffer,避免数据覆盖。
最终正确代码
const bufPrint = @import("std").fmt.bufPrint; const print = @import("std").debug.print; const DefaultPrng = @import("std").rand.DefaultPrng; fn qr(comptime a: []const u8) void { print("\n{s}\n", .{a}); } fn coati(comptime a: []const u8, b: i32, c: []u8) ![]const u8 { return try bufPrint(c, "{d}: a {s} number.", .{b, a}); } fn tapir(comptime a: []const u8, c: []u8) ![]const u8 { return try bufPrint(c, "Checked by {s}.", .{a}); } pub fn main() !void { const qa = @constCast(&DefaultPrng.init(0)) .random() .intRangeAtMost(i32, 0, 59); qr("Statements."); if (qa < 20) { var coati_buf: [64]u8 = undefined; var tapir_buf: [64]u8 = undefined; const face = try coati("lesser", qa, &coati_buf); const butt = try tapir("if-else statement", &tapir_buf); print("{s} {s}\n", .{face, butt}); } else if (qa < 40) { var coati_buf: [64]u8 = undefined; var tapir_buf: [64]u8 = undefined; const face = try coati("middle", qa, &coati_buf); const butt = try tapir("if-else statement", &tapir_buf); print("{s} {s}\n", .{face, butt}); } else { var coati_buf: [64]u8 = undefined; var tapir_buf: [64]u8 = undefined; const face = try coati("greater", qa, &coati_buf); const butt = try tapir("if-else statement", &tapir_buf); print("{s} {s}\n", .{face, butt}); } }
额外优化:qr函数内的print同样无需try,因此可将qr的返回类型从!void改为void,去掉不必要的错误处理。
内容的提问来源于stack exchange,提问作者Prido1024
相关产品推荐
相关产品推荐

