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;
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;
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;
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;
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;
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).
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const line: Stringtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringconst dump: String(parameter) state: ?function fmt_append_debug<T>(out: String, value: &T) -> String
where T: DebugAppend a {expr:?} hole via Debug. Same error handling as fmt_append_display.
function fmt_append_lit(out: String, text: string) -> StringAppend a literal text chunk (the text between {...} holes).
function fmt_append_display<T>(out: String, value: &T) -> String
where T: DisplayAppend 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_new() -> StringStart a fresh interpolation buffer. The innermost call in an f-string desugar.
std::fmt::interpimport 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).
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;
}function fmt_append_fmt<T>(out: String, value: &T, spec: string) -> String
where T: DisplayAppend 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).
function 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 fmt_new() -> StringStart a fresh interpolation buffer. The innermost call in an f-string desugar.
function fmt_append_lit(out: String, text: string) -> StringAppend a literal text chunk (the text between {...} holes).
(parameter) out: String(parameter) text: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
function fmt_append_display<T>(out: String, value: &T) -> String
where T: DisplayAppend 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.
type parameter Twhere T: Display
(parameter) value: &Twhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
Result::Err(E)function fmt_append_debug<T>(out: String, value: &T) -> String
where T: DebugAppend a {expr:?} hole via Debug. Same error handling as fmt_append_display.
type parameter Twhere T: Debug
function fmt_append_fmt<T>(out: String, value: &T, spec: string) -> String
where T: DisplayAppend 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).
(parameter) spec: string