gpt4 book ai didi

zig - 如何将流或写入器参数传递给 Zig 中的函数?

转载 作者:行者123 更新时间:2023-12-03 08:01:08 27 4
gpt4 key购买 nike

我试图将输出流传递给函数,但无法正确执行。此示例代码展示了我尝试过的一些事情

// Attempts to pass stream or writer to a function
const std = @import("std");
pub fn main() !void {

// #1
try print1(std.io.getStdOut(), "Hello, ");

// #2
try print2(std.io.getStdOut().writer(), "world!");

}

// error: 'File' is not marked 'pub'
pub fn print1(file: std.io.File, str: []const u8) !void
{
try file.writer().print("{s}", .{str});
}

// error: expected type 'type', found 'fn(comptime type, comptime type, comptime anytype) type'
fn print2(writer: std.io.Writer, str: []const u8) !void
{
try writer.print("{s}", .{str});
}

我正在使用 Zig 0.10.0

最佳答案

io.Writergeneric data structure 。 IE。它是一个返回类型的函数。您不能将其用作函数参数,但可以:

对于必须接受任意编写器的函数,需要

anytype。否则,使用别名或“代理”类型可能会更好。

关于zig - 如何将流或写入器参数传递给 Zig 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74173508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com