This extremely simple Rust program:
fn main() { let c = "hello"; println!(c); }
throws the following compile-time error:
error: expected a literal --> src/main.rs:3:14 | 3 | println!(c); | ^
In previous versions of Rust, the error said:
error: format argument must be a string literal. println!(c); ^
Replacing the program with:
fn main() { println!("Hello"); }
Works fine.
The meaning of this error isn't clear to me and a Google search hasn't really shed light on it. Why does passing c
to the println!
macro cause a compile time error? This seems like quite unusual behaviour.
This should work:
fn main() { let c = "hello"; println!("{}", c); }
The string "{}"
is a template where {}
will be replaced by the next argument passed to println!
.
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