`format!` does not support width/alignment with `#x`
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.