Skip to content
CryoCryo home
Stdlibfmt

interp

import std::fmt::interp; — injected by the compiler into any module containing an f-string · source

Free Functions

f-strings

An f-string builds an owned String. Each {expr} hole is rendered through Display and each {expr:?} hole through Debug:

const line: String = f"user {name} has {count} items";
const dump: String = f"state = {state:?}";

The parser lowers the literal into a chain of calls threading one String builder left to right:

// f"x = {x}, y = {y:?}" becomes
fmt_append_debug(
    fmt_append_lit(
        fmt_append_display(
            fmt_append_lit(fmt_new(), "x = "),
            &x),
        ", y = "),
    &y)

Each helper takes the partial string by value and returns it, so the whole expression evaluates to a single owned String with no intermediate temporaries to drop. The generic fmt_append_display / fmt_append_debug carry the Display / Debug bound so bound-directed dispatch resolves the nested value.fmt(f) to the right trait. A hole with a format spec — {n:>8} — goes through fmt_append_fmt, which hands the spec text to fmt::spec at runtime.

The compiler injects import std::fmt::interp; into any module that contains an f-string, so f"..." works with no import of your own. You never call these by hand.

function fmt_new() -> String;

Start a fresh interpolation buffer. The innermost call in an f-string desugar.

function fmt_append_lit(out: String, text: string) -> String;

Append a literal text chunk (the text between {...} holes).

function fmt_append_display<T>(out: String, value: &T) -> String where T: Display;

Append a {expr} hole via Display. In-memory String sinks never fail to write, so a formatting error (which can only originate from a user Display impl returning Err) is swallowed here rather than propagated - an f-string evaluates to a plain String, not a Result.

function fmt_append_debug<T>(out: String, value: &T) -> String where T: Debug;

Append a {expr:?} hole via Debug. Same error handling as fmt_append_display.

function fmt_append_fmt<T>(out: String, value: &T, spec: string) -> String where T: Display;

Append a {expr:spec} hole carrying a non-trivial format spec (width / fill / align / precision / radix). spec is the raw spec text after the colon; the compiler emits this helper only when that text is a valid spec (a bare {expr} and a {expr:?} still lower to fmt_append_display / fmt_append_debug).