Lyricdev
GitHub

std.fmt

Formats text from a template read at run time.

format speaks the f-strings' format language, with the values given apart from the template: fmt.format("{} has {1:>4}", name, n). What the compiler checks in an f-string is checked here when it runs, and a mistake panics: a template that leaves a brace open or closes one it did not open, a value it names that is not given, a spec that is no format or does not apply to its value.

Examples

import std.io { println };
import std.fmt;

let template = "{} has {1:>4} items";
println(fmt.format(template, "cart", 12));
cart has   12 items

Functions

pub fn format(template: string, args: Display...): string

Returns the template's text with each hole replaced by its value.

{} takes the next value and {i} the i-th, counted from 0. The two forms mix; the counter of {} advances only on {}. Either may carry a :spec before its closing brace: a format that applies to the value's type, or one ending in ? for the value's Debug text. {{ and }} write a single brace. A value no hole names is left out.

Panics

  • A } that closes no hole and is not part of }}.
  • A hole without its closing }, or holding anything but an index and a :spec.
  • A hole naming a value beyond those given.
  • A spec ending in ? for a value that is not Debug.
  • A spec that is no format, or does not apply to its value's type.