0

To print a string centered by 15 dashes, i'd do the following:

println!("{:-^15}", "Hi :D");

But what if i wanted to control the number of dashes that are put before and after my string? I tried the following:

let margin: usize = 15;
println!("{:-^{margin}}", "Hi :(");

And other variations, but all of them don't compile. What should i do?

1 Answers1

1

You almost had the syntax of formatting parameters. Here are a few ways to use them:

println!("{:-^1$}", "Hi :D", 15);
let margin = 15;
println!("{:-^1$}", "Hi :D", margin);
println!("{:-^margin$}", "Hi :D");
Zach Thompson
  • 272
  • 2
  • 7