Result from format_args! cannot be center-aligned within another format string

2a2f35f
Opened by Colin Finck at 2025-01-03 11:26:01

I can do println!("{:=^80}", " TEST ") to pretty-print a center-aligned header within equality signs. However, the alignment is ignored when I do println!("{:=^80}", format_args!(" TEST ")). I need the latter syntax to further write code like println!("{:=^80}", format_args!(" TEST {:#016X} ", some_integer)).

I understand that a format_args! result is harder to center-align due to cases where the final width could be unknown. Nevertheless, it should be possible for the compiler to determine the string width in my example cases to let it be center-aligned.

  1. Can't you in this case use println!("{:=^80}", format!(" TEST {:#016X} ", some_integer));?

    Esteban Kuber at 2017-12-07 19:24:17

  2. No, my code must run in a no_std environment without heap allocations.

    Colin Finck at 2017-12-07 20:09:40

  3. Triage: not aware of any changes here.

    Steve Klabnik at 2020-06-20 13:02:57

  4. I met the same problem. Is this a bug? Or a feature? If it's a feature, maybe we should mention this in the document of format_args!.

    SaltyKitkat at 2025-01-03 11:26:01