about things
format strings#
0.15 changed how std.fmt works. the big ones: {f} is now required for types with format methods, and format method signatures changed.
{f} required for format methods#
in 0.14, {} would implicitly call a type's format method if it had one. in 0.15, you must use {f} explicitly:
const MyType = struct {
value: u32,
pub fn format(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
try writer.print("MyType({d})", .{self.value});
}
};
const x = MyType{ .value = 42 };
// 0.14: std.debug.print("{}", .{x}); // called format implicitly
// 0.15: std.debug.print("{f}", .{x}); // must use {f}
// std.debug.print("{any}", .{x}); // skips format method
why: prevents silent behavior changes when format methods are added or removed from types.
format method signature change#
// 0.14 signature
pub fn format(
self: @This(),
comptime fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void
// 0.15 signature
pub fn format(
self: @This(),
writer: *std.Io.Writer,
) std.Io.Writer.Error!void
no more format string or options parameters. if you need different formatting modes, use separate named methods or return a wrapper struct.
new specifiers#
| specifier | meaning | example |
|---|---|---|
{f} |
call type's format method | std.debug.print("{f}", .{my_obj}) |
{t} |
@tagName() / @errorName() |
std.debug.print("{t}", .{my_enum}) |
{b64} |
base64 encoding | std.debug.print("{b64}", .{bytes}) |
{t} is particularly useful — replaces @tagName(enum_val) in format strings.
what still works#
{s}, {d}, {x}, {any}, {?}, {} for basic types are unchanged. our zat code uses these exclusively and doesn't need migration.
sources: 0.15 release notes