Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

padding in format args doesn't work with debug impls #46006

Closed
nikomatsakis opened this issue Nov 15, 2017 · 5 comments
Closed

padding in format args doesn't work with debug impls #46006

nikomatsakis opened this issue Nov 15, 2017 · 5 comments
Labels
C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 15, 2017

This:

fn main() {
    println!("Hello {0:5}!", "x");
}

prints what I expect:

Hello x    !

But this:

fn main() {
    println!("Hello {0:5?}!", "x");
}

prints Hello "x"! (play), not:

Hello "x"  !

Is this intentional? If so, the format docs are pretty misleading. Otherwise, a regression perhaps? I'm not sure what is...T-libs?

cc @alexcrichton

@nikomatsakis nikomatsakis added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Nov 15, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Nov 15, 2017
@sfackler
Copy link
Member

Each implementation of a fmt::* trait needs to individually decide if and how it's going to handle formatting flags. Debug impls tend to not handle them in general.

@alexcrichton
Copy link
Member

Yeah this is "intentional" in the sense that it's not expected to work. We likely want to be more clear about this in the documentation though

@nikomatsakis
Copy link
Contributor Author

Oh, interesting. I thought padding was imposed externally. OK, that's a shame. It'd be useful I think to have in std::fmt some wrappers that "impose" formatting from the outside -- e.g., I often wish that instead of using a flag like {:22$} I could do this:

use std::fmt;
...
format!("{}", fmt::padded(&foo, 22))

where fmt::padded takes the output of foo.fmt() and pads it to be of the given width. These could then be readily composed, so {:22$?} would be:

use std::fmt;
....
format!("{}", fmt::padded(fmt::debug(&foo), 22))

I should just make some things like this in the compiler I guess. Anyway. I'll close then!

@paul-callahan
Copy link

paul-callahan commented Nov 27, 2018

easy work around:

#[derive(Debug)]
pub enum Unprintable {
    X
}
#[test]
fn test_x() {
    println!("Hello {0:5}!", format!("{:?}", Unprintable::X));
}

@sourcefrog
Copy link
Contributor

#59933 mentions this in the docs.

Centril pushed a commit to Centril/rust that referenced this issue Apr 19, 2019
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Make clear that format padding doesn't work for Debug

As mentioned in
rust-lang#46006 (comment)
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Make clear that format padding doesn't work for Debug

As mentioned in
rust-lang#46006 (comment)
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Make clear that format padding doesn't work for Debug

As mentioned in
rust-lang#46006 (comment)
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Make clear that format padding doesn't work for Debug

As mentioned in
rust-lang#46006 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants