Format! for leftpad?


The idea is to use the capabilities of format! to left-pad strings (e.g. "foo" => " foo"). This is possible using a fixed pad character (“X” in the example) like so:

format!("{:X>width$}", string, width=total_string_width)

However, this does not work:

format!("{:pc$>width$}", string, width=total_string_width, pc=pad_char)

Does anybody know a way around this?

Origin of the discussion: