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
A sink for string formatting operations
Types that can be formatted. This is the standard way to implement custom
formatting for types.
Structs
Error that can occur during a string formatting operation.
Formatter that writes into a string slice.
A formatter that discards output.
Adapter for writting formatted data into a stream
Functions
-
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
Transforms a format string and arguments into a sequence of chunks.
Write a formatted string into a given formatter.
Write a formatted string and a newline into a given formatter.
Format a string into a
StringBuf
.
Format a string into a preexisting buffer.