std-method

Formatter::pad_integral

A custom integer-like formatter can delegate sign and padding behavior to pad_integral. Pass digits without a sign and a prefix suitable for alternate mode. Expected stdout (not executed): 0x002a Common mistake: Including a sign inside digits as well as supplying the sign separately.

Formal Syntax & Structure

rust syntaxrust
f.pad_integral(is_nonnegative, prefix, digits)

Working Example

Example Snippetrust
fn main() {    use std::fmt;    struct Code(u8);    impl fmt::LowerHex for Code {     fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result { f.pad_integral(true,"0x",&format!("{:x}",self.0)) }    }    println!("{:#06x}", Code(42));}
Rust Reference