Skip to content

Raw identifiers don't work in formatting strings #115466

Closed
@ilyvion

Description

@ilyvion

I tried this code:

fn main() {
    let r#type = "foobar";
    println!("It is {r#type}");
}

I expected to see this happen: The program compiles and prints "It is foobar" to stdout.

Instead, this happened: The compiler gives an error while compiling: "error: invalid format string"

Meta

rustc --version --verbose:
(Only tested on playground, but on all three channels, reported as:)

1.74.0-nightly 2023-09-01 35e416303e6591a71ef6
1.73.0-beta.4 2023-09-01 9f37cd4f79d289492cca
1.72.0

It's not a compiler crash, so I'm going to leave out the backtrace and instead just show what the compiler outputs when you try to compile the above code:

error: invalid format string: expected `'}'`, found `'#'`
 --> src/main.rs:3:23
  |
3 |     println!("It is {r#type}");
  |                     - ^ expected `'}'` in format string
  |                     |
  |                     because of this opening brace
  |
  = note: if you intended to print `{`, you can escape it using `{{`

I'm not convinced this is solvable/possible (I can imagine allowing this could potentially interfere with other formatting options (although they all follow a :, right? So maybe not.), but I also couldn't find any existing issue about it, so I figured I'd open one to have it clarified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions