29

I have a program that prints out data that should be printed into a format that looks like a table. However, the table breaks when the numbers are longer than 2. I know about the width parameter in std::fmt, but I can't get my head around it.

Current output:

---------------------------------------
| total | blanks: | comments: | code: |
---------------------------------------
|  0   |    0    |    0     |    0  |
|  77   |    0    |    3     |    74  |
|  112   |    0    |    6     |    106  |
|  178   |    0    |    6     |    172  |
|  218   |    0    |    7     |    211  |
|  289   |    0    |    8     |    281  |
|  380   |    0    |    9     |    371  |
|  460   |    0    |    10     |    450  |
|  535   |    0    |    11     |    524  |
|  611   |    0    |    12     |    599  |
|  692   |    0    |    14     |    678  |
|  772   |    0    |    17     |    755  |
|  873   |    0    |    18     |    855  |
|  963   |    0    |    19     |    944  |
|  1390   |    0    |    19     |    1371  |
|  1808   |    0    |    19     |    1789  |
|  2011   |    0    |    19     |    1992  |
|  2259   |    0    |    19     |    2240  |
|  2294   |    0    |    19     |    2275  |
|  2349   |    0    |    19     |    2330  |
|  2376   |    0    |    19     |    2357  |
|  2430   |    0    |    19     |    2411  |
|  2451   |    0    |    19     |    2432  |
|  2515   |    13    |    19     |    2483  |
|  2559   |    13    |    19     |    2527  |
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
XAMPPRocky
  • 3,160
  • 5
  • 25
  • 45
  • try to use TAB instead of SPACE. Should work fine with numbers or strings less than 8 characters. – Stephan May 21 '15 at 16:46
  • 3
    If you do use tabs, then I wrote a crate to do elastic tabstops, which will align columns regardless of their length: https://github.com/BurntSushi/tabwriter – BurntSushi5 May 21 '15 at 17:54

2 Answers2

46

The syntax is like the str.format syntax in Python. This:

fn main() {
    println!(
        "{0: <10} | {1: <10} | {2: <10} | {3: <10}",
        "total", "blanks", "comments", "code"
    );
    println!("{0: <10} | {1: <10} | {2: <10} | {3: <10}", 0, 0, 0, 0);
    println!("{0: <10} | {1: <10} | {2: <10} | {3: <10}", 77, 0, 3, 74);
    println!("{0: <10} | {1: <10} | {2: <10} | {3: <10}", 112, 0, 6, 106);
    println!(
        "{0: <10} | {1: <10} | {2: <10} | {3: <10}",
        460, 0, 10, 1371
    );
}

(playground)

produces the following output:

total      | blanks     | comments   | code      
0          | 0          | 0          | 0         
77         | 0          | 3          | 74        
112        | 0          | 6          | 106       
460        | 0          | 10         | 1371  
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Michael
  • 4,722
  • 6
  • 37
  • 58
  • 4
    Curious: why not `{: <10} | {: <10} ...` instead of `{0: <10} | {1: <10} ...`? I just tested the former and it seems to do the same thing. – joe Sep 03 '19 at 22:05
9

Or you could use a specialized crate for formatting tables like prettytable-rs

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
hbobenicio
  • 1,000
  • 15
  • 16
  • 7
    For anyone seeing this comment now, prettytable doesn't seem to be maintained anymore, there is [tabled](https://crates.io/crates/tabled) though. – Stef Dec 23 '21 at 20:02