specification
Used by the f-string runtime · source
This file parses and renders the format spec of an f-string hole. Its parser and render helpers are internal to the standard library — the f-string runtime calls them for every hole that carries a spec — so the spec's grammar is its whole surface.
The format spec
An f-string hole may carry a spec after a colon:
spec := [[fill]align][+][#][0][width][.precision][type]
align := '<' | '>' | '^' (left, right, centre)
type := 'x' | 'X' | 'o' | 'b' | 'e' (hex lower/upper, octal, binary, scientific)
import std::fmt;
function main() -> int {
const value: i32 = 7;
const byte: u8 = 15;
const ratio: f64 = 0.5;
const label: Str = "menu";
const delta: i32 = 42;
const big: f64 = 1234.5;
fmt::println(f"{value:>8}"); // right-align in a field of 8
fmt::println(f"{byte:#04x}"); // 0x0f - `#` adds the radix prefix, `0` zero-pads
fmt::println(f"{ratio:.3}"); // 0.500 - three fractional digits
fmt::println(f"{label:*^12}"); // ****menu**** - centred, padded with '*'
fmt::println(f"{delta:+}"); // +42 - `+` signs a non-negative number too
fmt::println(f"{big:.2e}"); // 1.23e+3
return 0;
}
widthis a minimum field width measured in Unicode scalars; a shorter value is padded withfill, a space by default.- Alignment defaults to left for text and numbers alike — chosen for predictability over a type-dependent default — and an explicit
>or^, or the0flag, overrides it. A0flag keeps the sign and any radix prefix leftmost:{-5:06}writes-00005. .precisiontruncates aDisplayvalue to that many scalars ({"hello":.3}writeshel), or sets the fractional digits of a float. A float is rounded at the 17th place at most, past the digits anf64carries; further places are written as zeros.x,X,oandbselect an integer radix, and#adds the0x/0o/0bprefix. A radix formats the value's own-width two's-complement bits with no sign, so{(-5i32):x}writesfffffffb.ewrites a number in scientific form, with.precisiondigits after the mantissa's point and six without one. An integer is written as thef64nearest it.+writes the sign of a decimal number that is not negative. A radix has no sign to write, and NaN none either.- The spec separator is the first top-level
:, so a hole that mixes a ternary with a spec parenthesises the ternary:f"{(c ? a : b):>4}".
A number in a hole with a spec is rendered on the stack; a spec on another type's Display text formats that text into a String first and pads that. The language reference covers the same grammar under f-strings.
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).
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 main() -> intconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const value: i32const byte: u8const ratio: f64const label: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
const delta: i32const big: f64(parameter) value: ?(parameter) byte: ?(parameter) ratio: ?(parameter) label: ?(parameter) delta: ?(parameter) big: ?return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
type struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.