The fmt module documentation describes all the formatting options:
Fill / Alignment
The fill character is provided normally in conjunction with the
widthparameter. This indicates that if the value being formatted is
smaller thanwidthsome extra characters will be printed around it.
The extra characters are specified byfill, and the alignment can be
one of the following options:
<– the argument is left-aligned inwidthcolumns^– the argument is center-aligned inwidthcolumns>– the argument is right-aligned inwidthcolumns
assert_eq!("00000110", format!("{:0>8}", "110"));
// |||
// ||+-- width
// |+--- align
// +---- fill
See also:
- How can I 0-pad a number by a variable amount when formatting with std::fmt?
- How do I print an integer in binary with leading zeros?
- Hexadecimal formating with padded zeroes
- Convert binary string to hex string with leading zeroes in Rust