In macros like format!
and println!
it is possible to specify a custom fill width
via an arg-within-an-arg:
println!("The weather is {:<width$} today.", "rainy", width = 10);
which prints
The weather is rainy today.
We aren't, of course, limited to whitespace as a fill character:
println!("The weather is {:y<width$} today.", "rainy", width = 10);
The weather is rainyyyyyy today.
Nor, apparently, are we limited to ASCII:
println!("The weather is {:雨<width$} today.", "rainy", width = 10);
The weather is rainy雨雨雨雨雨 today.
Notice that in each case, we're using a char-literal to specify the fill. I would like to propose an extension to the formatting grammar to allow for dynamic specification of the fill character in a similar manner to the width
.
Proposed Syntax
Two immediate possibilities come to mind:
Same Syntax
println!("The weather is {:fill$<width$} today.", "rainy", fill = 'y', width = 10);
Problem: But then what would {:foo$}
specify, the fill char or the width? Note that even now, a fill character can be given without a width, but nothing is actually printed (e.g. {:y>}
).
Different Syntax
Looking at the remaining symbols left on the top row of a keyboard, we have:
-
!
: No, conflicts with the!
type. -
@
: Used in some languages to denote a variable. -
#
: No, conflicts with number formatting. -
%
: Reminiscent of C-formatting. -
^
: No, used to specify alignment. -
&
: No, conflicts with references. -
*
: No, conflicts with math.
So how about @
?
println!("The weather is {:fill@<width$} today.", "rainy", fill = 'y', width = 10);
This way, the fill and width could be hardcoded as usual (e.g. {:y<10}
) or done dynamically, as shown above.
Thoughts? Please and thanks.