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

formatting with alignment fails #98

Closed
parkr opened this issue May 18, 2015 · 5 comments
Closed

formatting with alignment fails #98

parkr opened this issue May 18, 2015 · 5 comments

Comments

@parkr
Copy link

parkr commented May 18, 2015

hey, I'm learning rust and re-writing a very basic cal in order to learn. ran into a strange formatting bug:

extern crate time;

fn main() {
    // not aligned (bug in TmFmt Display impl?)
    println!("{:>11}", time::now_utc().strftime("%B").ok().unwrap());
    // aligned properly when forced into a string
    println!("{:>11}", time::now_utc().strftime("%B").ok().unwrap().to_string());
}

know what's going on here? i don't think i should have to call .to_string() in order to get the alignment...?

parkr referenced this issue in parkr/cal-rust May 18, 2015
@lifthrasiir
Copy link

To be exact, it is a common behavior that formatting implementations discard the width and alignment information:

fn main() {
    println!("{:>11?}", "a\nb\nc"); // not aligned
    println!("{:>11}", format!("{:?}", "a\nb\nc")); // aligned
}

I would argue that this behavior is, while frustrating, justifiable because alignment requires the length of printed string beforehand and this might not be acceptable for many types. (For the case of strftime, I think there is no efficient and seemingly-correct solution; Formatter::pad obviously requires the string length.) But it is frustrating, and I agree that this has to be discussed and at least documented.

@vks
Copy link

vks commented May 18, 2015

If a type does not support alignment, then println! should fail at compile time IMHO.
Alas, this would not be backwards compatible. So we can only make it give a warning.

@alexcrichton
Copy link

Yes @lifthrasiir is right in that most types do not support the width/alignment of the underlying formatting system, and the best solution to this (as you've found) is to call .to_string() and then pad that accordingly. This is currently as-intended, however, and the documentation should probably happen in std::fmt instead of just in this library, so I'm going to close this issue.

@parkr
Copy link
Author

parkr commented May 19, 2015

This is currently as-intended, however

Poop. Ok, that was really frustrating. Maybe a warning? A warning would have saved a lot of angst.

the documentation should probably happen in std::fmt instead of just in this library, so I'm going to close this issue.

Where can I submit a PR for that?

@alexcrichton
Copy link

Maybe a warning? A warning would have saved a lot of angst

Unfortunately there's no way for the formatting infrastructure to know whether a type actually looks at the formatting flags or not (as it's all trait-based).

Where can I submit a PR for that?

A PR editing this file should do the trick!

steveklabnik added a commit to steveklabnik/rust that referenced this issue May 19, 2015
Padding and alignment are often not implemented by types and can cause confusion in the user.  Per discussion with @alexcrichton, here is my PR.

/cc time-rs/time#98
@jhpratt jhpratt added the v0.1 label Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants