I need to left-justify a &str
in Rust.
In Python, I would do:
f"{spam}: {eggs}".ljust(curses.COLS - 1)
How can I do this idiomatically in Rust?
Formatting macros such as println!
, write!
and format!
offer this functionality. Have a look at https://doc.rust-lang.org/std/fmt/#fillalignment and https://doc.rust-lang.org/std/fmt/#width.
{:5}
specifies a minimum width of 5{:-<5}
left-aligns to five digits and fills with -
{1:0$}
: "The value for the width can also be provided as a usize in the list of parameters by adding a postfix $, indicating that the second argument is a usize specifying the width."If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With