Sub topic 9
Width, fill, and alignment
This chapter assumes you can write a basic Rust program, know that {} is a placeholder in println!, and have seen {:?} for Debug output. It covers the part of a placeholder after the colon that controls how wide the output is and where the value sits inside that width.
The shape of a format spec
A placeholder has the form {argument:spec}. For layout, the spec reads [[fill]align][width]. Each piece is optional.
- Width is a minimum number of characters. Output shorter than the width is padded. Output longer than the width is never cut.
- Alignment is
<(left),^(center), or>(right). - Fill is the character used for padding. It is written just before the alignment symbol. The default is a space.
fn main() {
println!("[{:8}]", "ab");
println!("[{:<8}]", "ab");
println!("[{:^8}]", "ab");
println!("[{:>8}]", "ab");
println!("[{:*>6}]", 42);
println!("[{:-^9}]", "mid");
}This prints:
[ab ]
[ab ]
[ ab ]
[ ab]
[****42]
[---mid---]Default alignment depends on the type
If you give a width but no alignment, strings and most other non-numeric values are left-aligned. Numbers are right-aligned. That is why {:8} pushed "ab" to the left above, while {:8} on 42 would put the digits at the right edge.
fn main() {
println!("[{:6}]", "ab");
println!("[{:6}]", 42);
}Output:
[ab ]
[ 42]Centering with an odd amount of padding
When the padding cannot be split evenly, the extra space goes on the right. {:^6} on "abc" leaves three spaces to share, so one goes on the left and two on the right.
Building a simple table
Width and alignment are most useful for columns. Left-align text and right-align numbers so the digits line up.
fn main() {
let rows = [("apples", 3, 0.5), ("kiwis", 12, 0.25), ("melons", 1, 2.0)];
println!("{:<8}|{:>4}|{:>7}", "item", "qty", "price");
println!("{:-<8}+{:->4}+{:->7}", "", "", "");
for (item, qty, price) in rows {
println!("{:<8}|{:>4}|{:>7.2}", item, qty, price);
}
}The separator line uses an empty string with a fill character, which produces a row of dashes of exactly the column width.
Common errors
- Width counts characters, not display columns. Rust counts Unicode scalar values. Wide characters such as many CJK glyphs, and emoji, take two terminal columns, so tables containing them can misalign.
- A fill character needs an alignment.
{:*8}does not compile as you might hope. Write{:*<8}or{:*>8}. - Width does not truncate. Use precision (covered in a later chapter) to cut a string, as in
{:.3}. - Custom types may ignore width. A
Displayimplementation that uses plainwrite!ignores the width and alignment the caller asked for. A later chapter shows howFormatter::padfixes that.
Exercise
Write a println! that prints the text hi centered in a field of width 7, padded with . characters on both sides. What does it print, and where does the extra padding go if the width is 8?
Show answer
println!("{:.^7}", "hi"); prints ..hi..., wait: 5 padding characters split 2 left and 3 right, so the output is ..hi.... With width 8 there are 6 padding characters, which split evenly as 3 and 3: ...hi....