Skip to content

Inconsistent format string support for binary and hex representations of arrays #119300

Open
@ChaiTRex

Description

@ChaiTRex

I tried this code:

fn main() {
    println!("{:02x?}", [10, 11, 12]);
    println!("{:08b?}", [10, 11, 12]);
}

I expected to see this happen:

It prints the given arrays in hexadecimal and in binary.

Instead, this happened:

Hexadecimal works if you comment out the binary line, but binary doesn't work.

Meta

rustc --version --verbose:

rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4
rustc 1.75.0-beta.7 (b216e7b0e 2023-12-16)
binary: rustc
commit-hash: b216e7b0e7a2bdf11300a21a614dac6be3e99c5b
commit-date: 2023-12-16
host: x86_64-unknown-linux-gnu
release: 1.75.0-beta.7
LLVM version: 17.0.6
rustc 1.76.0-nightly (3f28fe133 2023-12-18)
binary: rustc
commit-hash: 3f28fe133475ec5faf3413b556bf3cfb0d51336c
commit-date: 2023-12-18
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.6
   Compiling bug v0.1.0 (/home/chaitrex/dev/bug)
error: invalid format string: expected `'}'`, found `'?'`
 --> src/main.rs:3:20
  |
3 |     println!("{:08b?}", [10, 20, 30]);
  |               -    ^ expected `'}'` in format string
  |               |
  |               because of this opening brace
  |
  = note: if you intended to print `{`, you can escape it using `{{`

error: could not compile `bug` (bin "bug") due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-fmtArea: `core::fmt`C-bugCategory: This is a bug.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler 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