Cannot pass writer as first anytype parameter

What am I doing wrong here?

const std = @import("std");

pub fn main() !void {
    var std_out = std.io.getStdOut();
    const writer = std_out.writer();
    try hello(writer);
}

pub fn hello(writer: anytype) !void {
    try writer.print("Hello world!");
}
$ zig run test.zig
test.zig:10:15: error: member function expected 2 argument(s), found 1
    try writer.print("Hello world!");
        ~~~~~~^~~~~~
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/linux-x86_64-0.14.0-dev.3187+d4c85079c/lib/std/io.zig:311:20: note: function declared here
        pub inline fn print(self: Self, comptime format: []const u8, args: anytype) Error!void {

Not at a computer but my instinct is that you need the args. Just an empty ‘.{}’ I think. Should be just like ‘std.debug.print’.

1 Like

forgot the second argument to writer.print

try writer.print("Hello world!\n", .{});

If you’re not using zig’s print formatting, instead of print use writeAll it takes a single slice argument

3 Likes