Module fmt/to_string

fmt/to_string
Stability: unstable, for two named reasons rather than a release count. `Debug` itself is new — it landed with the D15 split in v0.2.28 (`plans/STD_API_STABILIZATION.md` §2) and its deprecated predecessor `derive(ToString)` is still accepted for one release as a spelling of the same structural body. Until that alias is deleted, two names produce the same render and a reader cannot tell which trait a type was meant to have. And `f64` / `f32` `to_string()` is not yet portable: it hands back the host C library's `%g` output, so a non-finite value renders `nan` on glibc/Apple libc and `-nan(ind)` on the MS CRT — a program that logs, JSON- encodes or fixture-compares a NaN produces different bytes per platform (`issues/float-to-string-is-platform-dependent-for-non-finite-values.md`, OPEN). Rust renders `NaN` / `inf` / `-inf` everywhere because it does not delegate to libc. Fixing that CHANGES output, so it has to happen before the module freezes, not after. The eighteen integer/bool/`str`/`String` impls are settled. — stable modules only change additively; this one may still change.

ToString and Debug — the two rendering channels, and their impls for every primitive.

ToString is the USER-FACING text (Rust's Display): what println, print and ${...} interpolation call, and what an error type's message is written by hand for. Debug is the STRUCTURAL render (Rust's Debug): what derive(Debug) emits, variant name and fields included. D15 split them because derive(ToString) used to emit the structural form, which meant no error enum could derive its own message.

Numbers go through one shared _snprintf_to_string (a comptime format literal, a 32-byte stack buffer) rather than a hand-rolled digit loop, so all eighteen numeric impls share one body.

Stability

unstable, for two named reasons rather than a release count.

Debug itself is new — it landed with the D15 split in v0.2.28 (plans/STD_API_STABILIZATION.md §2) and its deprecated predecessor derive(ToString) is still accepted for one release as a spelling of the same structural body. Until that alias is deleted, two names produce the same render and a reader cannot tell which trait a type was meant to have.

And f64 / f32 to_string() is not yet portable: it hands back the host C library's %g output, so a non-finite value renders nan on glibc/Apple libc and -nan(ind) on the MS CRT — a program that logs, JSON- encodes or fixture-compares a NaN produces different bytes per platform (issues/float-to-string-is-platform-dependent-for-non-finite-values.md, OPEN). Rust renders NaN / inf / -inf everywhere because it does not delegate to libc. Fixing that CHANGES output, so it has to happen before the module freezes, not after.

The eighteen integer/bool/str/String impls are settled.

Traits / Modules

ToString trait
ToString

Trait for converting values to their String representation. Types implementing this trait can be used with println, print, and template strings.

Methods

to_string : fn(self : Self) -> String

Render self as the text a USER should read — Rust's Display::fmt, not its Debug. Hand-written (or generated by derive(Error) from a per-variant format string) whenever the structural form would be wrong.

Parameters

NameTypeNotes
selfSelf

Returns: String

Implementors

Debug trait
Debug

Debug — the STRUCTURAL render of a value (Rust's std::fmt::Debug), split from ToString by D15.

derive(ToString) used to emit the structural render, which is Rust's Debug, not Rust's Display. That conflation meant no error type could DERIVE its user-facing message — every std error enum hand-writes to_string — while also getting a structural dump for free.

Now the two are separate channels:

MyError :: enum(NotFound(path : String));
derive(MyError, Debug);           // "MyError.NotFound(/tmp/x)"  — structural
impl(MyError, ToString(...));     // "no such file: /tmp/x"      — user-facing

There is deliberately NO blanket impl(T <: ToString) Debug for T: it would give any type with a hand-written message a debug_string that returns that MESSAGE rather than a structural render, which is exactly the conflation D15 removes.

Methods

debug_string : fn(self : Self) -> String

Render self STRUCTURALLY — type or variant name plus fields, the shape derive(Debug) emits. Meant for logs, assertion failures and inspection, never for text a user reads.

Parameters

NameTypeNotes
selfSelf

Returns: String