Formatting
std.fmt
provides
ways to format data to and from strings.
A basic example of creating a formatted string. The format string must be
compile-time known. The d
here denotes that we want a decimal number.
Writers conveniently have a print
method, which works similarly.
Take a moment to appreciate that you now know from top to bottom how printing
Hello World works.
std.debug.print
works the same, except it writes to stderr and is protected by a mutex.
We have used the {s}
format specifier up until this point to print strings.
Here, we will use {any}
, which gives us the default formatting.
Let’s create a type with custom formatting by giving it a format
function.
This function must be marked as pub
so that std.fmt can access it (more on
packages later). You may notice the usage of {s}
instead of {}
- this is the
format specifier for strings (more on format specifiers later). This is used
here as {}
defaults to array printing over string printing.