String formatting
The core of the formatting system is the Formattable protocol. Implementing it on custom types allows them to be formatted using the standard string-formatting macros (format, write, ...) and the convenience standard I/O macros (print, println, ...).
Protocols
Structs
Functions
-
fn debug<T>(value: T) -> DebugAdapter<T>
Adapter for printing a debug representation of a value.
This can print most types without them needing to implment custom formatting. Types can override the default behavior by implementing the DebugFormattable protocol, but this is only recommended for types that e.g. contain untagged unions or normally uninitialized fields.
Example
use std::fmt::debug; struct Point { x: i32, y: i32 } println!("{}", Point { x: 1, y: 2 }.debug()); // prints 'Point { x: 1, y: 2 }'
Run this example -
fn hex<T>(val: T) -> NumFormatAdapter<T>
T: IntegerWrite value as a hexadecimal number
Example
use std::fmt::hex; println!("{}", 42.hex()); // prints '2a'
Run this example -
fn bin<T>(val: T) -> NumFormatAdapter<T>
T: IntegerWrite value as a binary number
Example
use std::fmt::bin; println!("{}", 42.bin()); // prints '101010'
Run this example -
fn oct<T>(val: T) -> NumFormatAdapter<T>
T: IntegerWrite value as an octal number
Example
use std::fmt::oct; println!("{}", 42.oct()); // prints '52'
Run this example -
fn zero_pad<T>(val: T, pad: usize) -> NumFormatAdapter<T>
T: IntegerPads number with zeros on the left.
Example
use std::fmt::zero_pad; println!("{}", 42.zero_pad(5)); // prints '00042'
Run this example -
fn precision<T>(val: T, prec: u16) -> FloatFormatAdapter<T>
-
fn repeat<T>(inner: T, times: usize) -> RepeatAdapter<T>
T: Formattable<T>Repeats value
times
times.Example
use std::fmt::repeat; println!("{}", "ha".repeat(3)); // prints 'hahaha'
Run this example -
fn pad_with<T>(val: T, len: usize, pad: u8) -> GenericPadAdapter<T>
T: Formattable<T>Pads arbitrary formattable value on the left with a specified character.
Example
use std::fmt::pad_with; println!("{}", "ha".pad_with(5, ' ')); // prints ' ha'
Run this example -
fn char(val: u8) -> CharAdapter
Writes a single u8 as a character (byte).
By default, u8 is written as a number.
Example
use std::fmt::char; println!("{}", 'A'); // prints '65' println!("{}", 'A'.char()); // prints 'A'
Run this example
Types
Macros
StringBuf
.