Format String Reference?

EDIT: Format strings are quite well-documented here

What would be the best place to put a formatting specifier / formatting string reference? Right now I’m not aware of a good cheatsheet, which sometimes leads to me just trying something to see if it works (e.g., I only learned yesterday that {x} works on slices, not just numbers).

zig.guide has an ok-ish list, but it doesn’t mention optionals, {any}, {t}, or bare {}, doesn’t mention that {x} can have precision specifiers, and IMO should be a table for easy reference. Also, it doesn’t seem to have anything past 0.15.2.

Reading the std/fmt.zig source helps, but it’s not exactly terse and it would be nice to have something in the documentation or here on ziggit. Would this be a reasonable PR? Or maybe a “docs” topic here?

Or maybe this just isn’t a problem other people have?

3 Likes

The docs I use for this are here: Zig Documentation

Although I will admit, just yesterday I forgot where this page was, going through std.fmt and std.debug.print before finally remembering it was in Writer.print. Re-found it before long, but it wasn’t in the first place I looked.

3 Likes

oh my god, that’s embarrassing, thanks

1 Like

Sure thing! And no need to be embarrassed haha, I also find that page easy to miss

2 Likes

It is a zig rite of passage to try and find the fmt string docs

7 Likes

The master doc for print doesn’t seem to include the handy {q} yet.

Just as this discussion popped up, andrew made a PR changing a bit the format API (Adding a qf) https://codeberg.org/ziglang/zig/pulls/36596

I had this exact question recently. It took me a while to find std.Io.Writer.print because usually I’m looking at std.mem.Allocator.print or some other print method docs.

Makes sense for it to be in std.Io.Writer.print though.

I hope some day the docs will get good cross references.

1 Like