Skip to content

format!() sometimes ignores width while formatting Debug #87905

Closed
@xpe

Description

@xpe

I tried this code:

// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=be8ab0a90dce4f0e53f964e41a4ae89e
fn main() {
    println!("'{:5?}'", 1);
    println!("'{:w$?}'", 2, w=5);
    println!("'{:5}'", "3");
    println!("'{:w$}'", "4", w=5);
    println!("'{:5?}'", "5");
    println!("'{:w$?}'", "6", w=5);
}

I expected to see this happen:

'    1'
'    2'
'3    '
'4    '
'"5"  '
'"6"  '

Instead, this happened:

'    1'
'    2'
'3    '
'4    '
'"5"'
'"6"'

To summarize, println!() / format!() ignores width while formatting Debug in some cases but not others.

Meta

rustc --version --verbose:

rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-apple-darwin
release: 1.54.0
LLVM version: 12.0.1

I confirmed the unexpected behavior also happens in beta and nightly:

See Also

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions