rust-lang / rust-lang/rust

Parse formatting strings existing in other languages and explain we don't support them

Open
#145,718 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics D-lack-of-suggestion D-terse P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Rust has a similar formatting string syntax to other languages', like Python's, but with fewer supported formatters. We already emit a pretty comprehensive output when encountering bad textual formatters:

error: unknown format trait `g`
 --> src/main.rs:3:14
  |
3 | println!("{x:g}");
  |              ^
  |
  = note: the only appropriate formatting traits are:
          - ``, which uses the `Display` trait
          - `?`, which uses the `Debug` trait
          - `e`, which uses the `LowerExp` trait
          - `E`, which uses the `UpperExp` trait
          - `o`, which uses the `Octal` trait
          - `p`, which uses the `Pointer` trait
          - `b`, which uses the `Binary` trait
          - `x`, which uses the `LowerHex` trait
          - `X`, which uses the `UpperHex` trait
error: unknown format trait `_b`
 --> src/main.rs:3:16
  |
3 | println!("{x:09_b}");
  |                ^^
  |
  = note: the only appropriate formatting traits are:
          - ``, which uses the `Display` trait
          - `?`, which uses the `Debug` trait
          - `e`, which uses the `LowerExp` trait
          - `E`, which uses the `UpperExp` trait
          - `o`, which uses the `Octal` trait
          - `p`, which uses the `Pointer` trait
          - `b`, which uses the `Binary` trait
          - `x`, which uses the `LowerHex` trait
          - `X`, which uses the `UpperHex` trait

but when using symbols for the formatter we emit simpler diagnostics without much guidance:

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

The parser should recognize these, tell people that Rust isn't Python :) and link to the documentation. If we can identify what the corresponding intended operation was, mention it. For example println!("{x=") likely meant to be dbg!("{x}"), _b should have been b (but we don't have support to customize its separator). This way we don't have to show the full list of valid formatters every time, and we explain more than "we didn't expect this here".

CC https://github.com/rust-lang/rust/issues/144023

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the format-string parser and diagnostic handling, using the {x=}, {x:,}, and {x:09_b} examples in the issue. Done means recognizing these cross-language forms, explaining the likely Rust alternative where identifiable, and linking the formatting documentation without showing the full formatter list each time.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.