Strings In Depth10 / 19
Up next

Format with an allocator

const std = @import("std");

pub fn main() !void {
    const alloc = std.heap.page_allocator;
    // allocPrint heap-allocates a right-sized string
    const s = try std.fmt.allocPrint(alloc, "{s}={d}", .{ "x", 42 });
    // free what the allocator gave us
    defer alloc.free(s);
    std.debug.print("{s}\n", .{s});
}
What you're solving

std.fmt.allocPrint returns an owned string