xur_

> module

std::fmt

format strings

use std::fmt; in the browser

std::fmt formats a template and its arguments into a string. The same syntax is used by io::print, io::println, std::panic, and the std::log functions.

> functions

# format(template: str, args?: any) -> str

Returns template with each placeholder replaced by the next argument, as described below. Unlike io::println, it always processes the template, even with no arguments, so fmt::format("{{}}") returns {}.

> format syntax

Each {} in the template is replaced by the next argument, displayed as a string.

{:x} and {:X} format an integer argument as lowercase or uppercase hexadecimal. No other format specifiers exist.

Literal braces are written by doubling them: {{ produces {, and }} produces }.

These are runtime errors: an unmatched brace, more placeholders than arguments, more arguments than placeholders, an unknown specifier, and {:x} or {:X} with a non-integer argument.

> examples

use std::fmt;
use std::io;

fn main() {
    let s = fmt::format("{} + {} = {}", 1, 2, 3);  // "1 + 2 = 3"
    io::println("{}", s);
    io::println("braces: {{{}}}", 1);   // "braces: {1}"
    io::println("hex: {:x}", 255);       // "hex: ff"
    io::println("HEX: {:X}", 48879);     // "HEX: BEEF"
}