Rust: make format! accept const vars as format-strings

Created on 13 Feb 2020  路  2Comments  路  Source: rust-lang/rust

I wonder if we could make rustc accept code like this:
rust const y: &str = "hello {}"; let x = format!(y, "world");

Currently this errors with:

error: format argument must be a string literal --> src/main.rs:3:21 | 3 | let x = format!(y, "world"); | ^ | help: you might be missing a string literal to format with | 3 | let x = format!("{} {}", y, "world"); | ^^^^^^^^

rustc 1.43.0-nightly (58b834344 2020-02-05)

All 2 comments

Macros run before name resolution, so there's no way for format! to find the contents of the string y.

Closing with @sfackler's explanation.

Was this page helpful?
0 / 5 - 0 ratings