-
Notifications
You must be signed in to change notification settings - Fork 13k
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
format!() ignores width when displaying enums #67162
Comments
Looking at In hindsight this makes sense, but was totally opaque to me and not obvious from the docs, though I see now:
It'd be great if this caveat could be pulled out into a dedicated section about custom impls. Even better if unsupported operations weren't just silently ignored, but that ship has probably sailed. |
Right, you probably want your implementation of |
Ah, thanks! So should https://doc.rust-lang.org/std/fmt/trait.Display.html#examples-1 instead use |
I would say, no. I will add some qualifications to what I said earlier: Using I don't believe there is any universal, ideal behavior for Besides, I don't see any reason why the author of a crate should be discouraged from implementing completely different semantics for width, e.g.: assert_eq!(
format!("{}", Position { longitude: 3., latitude: 7. }),
"(3, 7)",
);
assert_eq!(
format!("{:5.2}", Position { longitude: 3., latitude: 7. }),
"( 3.00, 7.00)",
); So if you have an enum that is supposed to represent one of a fixed set of string constants, that's a reasonable time to forward. Likewise if you simply desire having padding work for that type. But in general you shouldn't expect width to work for arbitrary types, nor should you feel obligated to make it work. (if a downstream crate needs padding, it can easily take the String-allocating route by itself!) |
If I understand correctly, this is not a bug and the issue can be closed. Any implementation of |
I assume this is still a point of confusion that could be improved in documentation. |
Similar to the issue reported in #55584 (and #55749), the width parameter is ignored when displaying enums, e.g. this fails:
Playground Example
Notice this is with
Display
, notDebug
. There's some question in the linked issues what the desired behavior forDebug
is, but I think it's a bug thatDisplay
formatting would not respect width (and precision).I'd be willing to take a crack at fixing this if I could get some code pointers.
The text was updated successfully, but these errors were encountered: