Skip to content

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

@dhardy

Description

@dhardy

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)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-fmtArea: `core::fmt`C-discussionCategory: Discussion or questions that doesn't represent real issues.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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