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