-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add missing example for Display::fmt #40299
Conversation
src/libcore/fmt/mod.rs
Outdated
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
/// match *self { | ||
/// Foo::Bar => write!(f, "Foo::Bar"), | ||
/// Foo::Stack => write!(f, "Foo::Stack"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is slightly confusing because it looks like a Debug
representation. How do you feel about getting rid of the Foo::
in the outputted string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always add the enum's name first in case you have multiple wrappings:
enum Foo {
A,
}
enum X {
A(Foo),
}
enum Y {
A(X),
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're mixing up Debug
and Display
. Display
is for user-facing output. Users shouldn't care about enum
namespacing. We don't want to encourage Rust developers to implement Display
if their types aren't mean to be displayed to end-users.
How about this as the example:
struct Position {
longitude: f32,
latitude: f32,
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.longitude, self.latitude)
}
}
This seems a little more realistic than Foo
and Bar
and Stack
;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why so cruel? :O
8628d0a
to
a5a3981
Compare
Updated. |
@bors r+ rollup |
📌 Commit a5a3981 has been approved by |
… r=frewsxcv Add missing example for Display::fmt r? @frewsxcv
… r=frewsxcv Add missing example for Display::fmt r? @frewsxcv
r? @frewsxcv