rust-lang / rust-lang/rust

`format!` does not support width/alignment with `#x`

Open
#144,023 12 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

A-diagnostics A-docs A-fmt C-discussion E-medium T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

    println!("Next u64: {0:#X>18} = {0:>20}", rand::rng().next_u64());

The format! / format_args! docs are not exactly clear on how to combine width/alignment with formatting traits, but I expected the above to print out a right-aligned hex number (0x + 16 hex digits) followed by a right-aligned decimal representation of the same number.

It doesn't; instead I see an error:

6 |     println!("Next u64: {0:#X>18} = {0:>20}", rng.next_u64());
  |                              ^ expected `>` to occur after `:` in format string

If I omit the #, the code compiles, instead treating x as a filler. This is confusing since {0:x} is used to enable hexadecimal output, yet the number still gets printed in decimal form.

If I use 0x{0:X} or {0:#X} then the number is printed correctly, but without alignment.

(The format! docs are horrible to read as a reference by the way: far too much prose and no sign-posting of how edge cases are handled.)

rustc 1.90.0-nightly (ab68b0fb2 2025-07-08)

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.