0

I don't understand why Rust does not allow to format a const string. This program does not compile:

const CON: &str = "test {}";

fn main() {
    println!(CON, 4);
}

It generates the following error:

error: format argument must be a string literal
 --> a.rs:4:14
  |
4 |     println!(CON, 4);
  |              ^^^
help: you might be missing a string literal to format with
  |
4 |     println!("{} {}", CON, 4);
  |              ^^^^^^^^

error: aborting due to previous error

I read around that println! and format! only work when the first argument is a string literal. But a const string is a named string literal.

rubik
  • 8,814
  • 9
  • 58
  • 88
  • 3
    `"test {}"` is a string literal. `CON` is not a literal, it's a constant that happens to be set to a literal. See also [Literal (computer programming)](https://en.wikipedia.org/wiki/Literal_(computer_programming)) – Shepmaster Oct 08 '19 at 20:11
  • And [What does the word "literal" mean?](https://stackoverflow.com/questions/485119/what-does-the-word-literal-mean) – trent Oct 08 '19 at 22:50

0 Answers0