text

module std::formatsource

Formatting values as text: the Format interface.

Format is a lang item (LANGUAGE_SPEC.md section 6.1a) at the canonical path std::format::Format. A type declaring it writes itself into a String; the compiler satisfies it for every primitive type through the extensions below: integers in decimal, bool as true or false, and floats as the shortest decimal reading back to the same value (std::float_digits). out.write(&value) writes any formattable value or text, and an interpolated string, f"x = {x}", writes its parts the same way (section 6.5).

interface Formatsource

exp interface Format

Required functions

fn format(self: &, out: &var String);

Implemented by

fn writesource

exp fn write<T: Format>(self out: &var String, value: &T)

Appends the value's text.

fn writesource

exp fn write(self out: &var String, text: &[u8])

Appends the text as it is.

fn write_allsource

exp fn write_all<T: Format>(self out: &var String, values: &[T])

Appends the values as [a, b, c].

fn text_ofsource

exp fn text_of<T: Format>(value: &T) -> String

The value's text as a new String.

type Alignsource

exp type Align = enum

Where padding goes when a written piece is narrower than its width.

Variants

Left

the piece first, then the fill

Right

the fill first

Center

the fill split, the extra one after

Numeric

a number's zeros after its sign

fn pad_fromsource

exp fn pad_from(self out: &var String, from: u64, width: u64, fill: u8, align: Align)

Pads what was written since from to at least width characters (UTF-8 code points) with the fill byte, placed as align says.

fn formatsource

exp fn format(
    self value: &u8,
    out: &var String
)

fn formatsource

exp fn format(
    self value: &u16,
    out: &var String
)

fn formatsource

exp fn format(
    self value: &u32,
    out: &var String
)

fn formatsource

exp fn format(self value: &u64, out: &var String)

fn formatsource

exp fn format(self value: &i8, out: &var String)

fn formatsource

exp fn format(self value: &i16, out: &var String)

fn formatsource

exp fn format(self value: &i32, out: &var String)

fn formatsource

exp fn format(self value: &i64, out: &var String)

fn formatsource

exp fn format(self value: &bool, out: &var String)

fn write_radixsource

exp fn write_radix<T: Number>(self out: &var String, value: T, radix: u64, uppercase: bool)

Appends the integer's digits in the radix, 2 to 16, upper-case letters when asked; a negative value as the bits of its type's width, as two's complement reads them (-1 as an i16 is ffff).

fn formatsource

exp fn format(self value: &f64, out: &var String)

The shortest decimal reading back to the value: plain from 0.000001 up to below 1e21, with .0 on a whole number, and as 1.5e-7 or 1e+21 outside; nan, inf and -inf for the others.

fn formatsource

exp fn format(self value: &f32, out: &var String)

fn write_fixedsource

exp fn write_fixed(self out: &var String, value: f64, decimals: u64)

Appends the value with exactly that many decimals, rounded to the nearest, ties to even, as the value's exact binary fraction gives them (an f32 converts exactly with to f64).