Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust's equivalent of Python's ljust() string method

Tags:

python

rust

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?

like image 854
adder Avatar asked Mar 02 '23 13:03

adder


1 Answers

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."
like image 113
phimuemue Avatar answered Mar 05 '23 16:03

phimuemue