Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format string with constant number of characters

Tags:

rust

I want my string to be displayed with 4 characters like this: "a" -> "a " when I do format!("{}", "a").

I read the format documentation, and tried several solutions like {:<4} but it didn't work.

like image 665
Moebius Avatar asked Sep 20 '25 06:09

Moebius


1 Answers

As fjh says, this works:

fn main() {
    assert_eq!("a   ", format!("{:<4}", 'a'));
    assert_eq!("a   ", format!("{:<4}", "a"));
}
like image 78
Shepmaster Avatar answered Sep 23 '25 16:09

Shepmaster