fmt
fmt turns values into text. Display is human-readable output for end users; Debug is developer-readable output for diagnostics. Both write through a Formatter<W> sitting on any FmtWrite sink, so formatting to stdout, into a String, or out to a file is the same code path.
public module fmt::error;
public module fmt::write;
public module fmt::float;
public module fmt::display;
public module fmt::spec;
public module fmt::interp;
The print family lives directly in this module — import std::fmt; brings it in. Note that it is not in the prelude.
| File | What it holds | Import |
|---|---|---|
display | Display, Debug, Formatter<W>, format_to_string | import std::fmt::display; |
write | FmtWrite, the sink trait | import std::fmt::write; |
error | FmtError | import std::fmt::error; |
float | f64::to_buf and friends, parse_f64 | import std::fmt::float; |
spec | FmtSpec, the parsed form of an f-string format spec | import std::fmt::spec; |
interp | The f-string runtime | (injected by the compiler wherever an f-string appears) |
Printing
Two flavours, and the distinction matters.
print / println / eprint / eprintln are Display-based. They take an already-formatted owned String, write it, and drop it. Because the f-string desugar has already run each hole through Display or Debug, there is no format string left to interpret — a % in the text is data, not a directive.
import std::fmt;
println(f"connected to {host}:{port}");
println("plain text works too"); // string literals convert implicitly
eprintln(f"failed: {err:?}");
printf / eprintf are the C-style variadic forms with %s and %d specifiers, returning the byte count (negative on error, per the libc convention). Reach for these when the output is genuinely variadic; prefer an f-string otherwise. format is the same thing into a freshly allocated C string — it returns null when formatting or allocation fails, and the result is yours to free.
Both flavours go through the real stdout, so their output stays interleaved rather than landing on a second FILE*.
function print(text: String) -> void;
function print(text: String) -> void;Write an already-formatted String (typically an f-string f"{x}") to stdout, then drop it. Display-based counterpart to printf: the f-string desugar already ran each {expr} through Display/Debug, so there is no format string to interpret here (a % in the text is data, not a directive). We go through the real stdout (printf "%.*s") so output stays interleaved with printf rather than landing on a second FILE*.
function println(text: String) -> void;
function println(text: String) -> void;Like print, followed by a newline.
function eprint(text: String) -> void;
function eprint(text: String) -> void;Write an already-formatted String (typically an f-string) to stderr, then drop it. Display-based counterpart to eprintf.
function eprintln(text: String) -> void;
function eprintln(text: String) -> void;Like eprint, followed by a newline.
function printf(fmt: string, args...) -> i32;
function printf(fmt: string, args...) -> i32;Write a printf-style formatted string to stdout. Returns the number of bytes written, or a negative value on error (libc printf convention). Use this for genuinely variadic %s/%d output; for value formatting prefer an f-string through println (Display-based) below.
function eprintf(fmt: string, args...) -> i32;
function eprintf(fmt: string, args...) -> i32;Write a printf-style formatted string to stderr. Same specifier rules as printf (see above). Returns the number of bytes vfprintf reported.
function format(fmt: string, args...) -> string;
function format(fmt: string, args...) -> string;Format printf-style into a freshly allocated string. Returns null when the formatting or the allocation failed.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
(parameter) err: ?type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}type struct Formatter<W> {
writer: W*;
}type trait FmtWrite {
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;
}type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype enum FmtError {
Io(IoError);
Alloc(AllocError);
}f64::to_buf(&this, buffer: u8*) -> u64Write this into buffer. Returns bytes written. buffer must hold at least F64_MAX_DIGITS bytes.
type struct FmtSpec {
fill: u32;
align: u8;
alternate: boolean;
zero_pad: boolean;
width: u64;
has_precision: boolean;
precision: u64;
radix: u8;
}A parsed format spec. Field defaults describe an empty spec (plain Display, no padding).
std::fmt::specfunction print(text: String) -> voidWrite an already-formatted String (typically an f-string f"{x}") to stdout, then drop it. Display-based counterpart to printf: the f-string desugar already ran each {expr} through Display/Debug, so there is no format string to interpret here (a % in the text is data, not a directive).
function eprint(text: String) -> voidWrite an already-formatted String (typically an f-string) to stderr, then drop it. Display-based counterpart to eprintf.
function printf(fmt: string, args...) -> i32Write a printf-style formatted string to stdout. Returns the number of bytes written, or a negative value on error (libc printf convention). Use this for genuinely variadic %s/%d output; for value formatting prefer an f-string through println (Display-based) below.
function eprintf(fmt: string, args...) -> i32Write a printf-style formatted string to stderr. Same specifier rules as printf (see above). Returns the number of bytes vfprintf reported.
function format(fmt: string, args...) -> stringFormat printf-style into a freshly allocated string. Returns null when the formatting or the allocation failed.
std::fmtfunction name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function print(text: String) -> voidWrite an already-formatted String (typically an f-string f"{x}") to stdout, then drop it. Display-based counterpart to printf: the f-string desugar already ran each {expr} through Display/Debug, so there is no format string to interpret here (a % in the text is data, not a directive).
(parameter) text: Stringfunction println(text: String) -> voidLike print, followed by a newline.
function eprint(text: String) -> voidWrite an already-formatted String (typically an f-string) to stderr, then drop it. Display-based counterpart to eprintf.
function eprintln(text: String) -> voidLike eprint, followed by a newline.
function printf(fmt: string, args...) -> i32Write a printf-style formatted string to stdout. Returns the number of bytes written, or a negative value on error (libc printf convention). Use this for genuinely variadic %s/%d output; for value formatting prefer an f-string through println (Display-based) below.
(parameter) fmt: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
(parameter) argsfunction name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VaArgs to read typed values from it.
function eprintf(fmt: string, args...) -> i32Write a printf-style formatted string to stderr. Same specifier rules as printf (see above). Returns the number of bytes vfprintf reported.
function format(fmt: string, args...) -> stringFormat printf-style into a freshly allocated string. Returns null when the formatting or the allocation failed.