Skip to content

Debug trait for tuples, array/slices, Vec, String (etc?) do not respect width parameter #30164

Open
@pnkfelix

Description

@pnkfelix

Consider the following println:

    let msg = "Hello";
    println!("(i,j,k): |{:30}|", msg);

This is using the width parameter to ensure that the output occupies at least width characters. (One can provide other arguments like fill/alignment to adjust the fill character or whether the output is left/right/center- alligned.) See docs here: https://doc.rust-lang.org/std/fmt/#width

In other words, it prints this (note the distance between the two occurrences of |):

(i,j,k): |Hello                         |

So, the problem:

The Debug trait seems to honor width for numeric and pointer types, but not for other types.

For example:

    println!("(i,j,k): |{:30?}|", msg);

does not necessarily put at least 30 characters between the two | characters that it prints; in the case of msg given above, it prints:

(i,j,k): |"Hello"|

Here is a playpen illustrating the problem on several inputs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-fmtArea: `core::fmt`C-bugCategory: This is a bug.T-libs-apiRelevant to the library API 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