Printing Zig Structs - Edward Loveall

7 Likes

Good article. I think that current fmt has yet to reach its final form and because it’s userland code it’s not being prioritized yet.

What the ultimate fmt code will look like, I don’t know, but I think there’s space for some optimism if you ever encountered an annoying limitation with it.

3 Likes

Nice! Given the title, I was wondering if you could mention the third common way, using {f} ?

const Greeting = struct {
    name: []const u8,
    time: []const u8,

    pub fn format(g: Greeting, w: *std.Io.Writer) std.Io.Writer.Error!void {
        try w.print("greetings {s}\n", .{g.name});
    }
};

std.debug.print("{f}\n", .{greeting});
4 Likes

You know, I never considered this pattern! As I alluded to in the article, I cut my teeth on a much higher level languages so some of the more common patterns are totally unknown to me. Everyday someone is born who doesn’t know how to allocate memory… or something :smile:

I’d encourage you to also write a short blog post either as an explicit or implicit response. I really want to see more zig patterns.

1 Like

All good, just figured I’d mention it since I bet yours will show up when people search about printing zig structs, or if you wanna write a follow-up one day. As for the response suggestion, nah, you write really well, I don’t :slight_smile: