Question about example 1.2

Hello,

In the example 1.2 ( Formatted print - Rust By Example ) there is a example to write a prefix with "0".

println!("{number:>0width$}", number=1, width=6);

That works but if i want to write "1" as prefix do not work. How to do it?

println!("{number:>1width$}", number=1, width=6);

Thank you.

println!("{number:1>width$}", number=1, width=6);

0width is special case that apply only to numbers and works only with 0,
use filling character.
Documentation: std::fmt - Rust

Excuse me i am new in rust, can you help me with a example?

Hi, you can try these.

println!("{number:a>width$}", number=1, width=6);
println!("{number:1>width$}", number=1, width=6);

Thank you very much. But is possible to pass the character as variable like this:

println!("{number:var1>width$}", number=1, var1="a", width=15);

Looks like its not possible. You can use https://crates.io/crates/strfmt for construct format at runtime.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.