I want to use println!
and the powerful formatting tools of format!
to print a character a specific number of times. Of course this is possible with a loop, like so:
fn give_love(count: usize) {
print!("Here is love for you: ");
for i in 0..count {
print!("♥");
}
println!("");
}
But I neither want to write the loop nor the three print
s. How to do this shorter/better?
fn give_love(count: usize) {
println!("Here is love for you: {:♥<1$}", "", count);
}
You can (mis-)use the fill feature that allows to fill a printed value with some character of your choice. The grammar for this feature alone looks like:
'{' ':' <fill> <align> <width> '}'
Where width
is either a constant number or a reference to an argument of type <argument_index> '$'
. So 3
would mean a width of constant 3 and 1$
would mean a width of the value of the 1st argument of println!
.
However: here we are kind of "misusing" this feature and we mustn't forget that we are only specifying the "fill" for some other printable thing, which is passed by argument to println
. This can be an empty string though.
println!("love: {:♥<3}", ""); // love: ♥♥♥
println!("love: {:♥<1$}", "", 5); // love: ♥♥♥♥♥
Here are some examples where we don't pass an empty string:
println!("love: {:♥<5}", "#"); // love: #♥♥♥♥
println!("love: {:♥>5}", "#"); // love: ♥♥♥♥#
println!("love: {:♥^5}", "#"); // love: ♥♥#♥♥
Since 1.16 you can use .repeat()
like so:
fn main() {
println!("Here is love for you: {}", "♥".repeat(10));
}
playground link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With