Trait custom_format::runtime::CustomFormat
source · [−]Expand description
Trait for custom formatting with runtime format checking
Required Methods
Formats the value using the given formatter.
Examples
use custom_format::runtime::{self as cfmt, CustomFormat};
use core::fmt;
#[derive(Debug)]
struct Hex(u8);
impl CustomFormat for Hex {
fn fmt(&self, f: &mut fmt::Formatter, spec: &str) -> fmt::Result {
match spec {
"x" => write!(f, "{:#02x}", self.0),
"X" => write!(f, "{:#02X}", self.0),
_ => Err(fmt::Error),
}
}
}
assert_eq!(cfmt::format!("{0:X?}, {0 :x}, {0 :X}", Hex(0xAB)), "Hex(AB), 0xab, 0xAB");
The following statement panics at runtime since "z"
is not a valid format specifier:
ⓘ
cfmt::println!("{ :z}", Hex(0));