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

Add missing example for Display::fmt #40299

Merged
merged 1 commit into from
Mar 12, 2017

Conversation

GuillaumeGomez
Copy link
Member

/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// match *self {
/// Foo::Bar => write!(f, "Foo::Bar"),
/// Foo::Stack => write!(f, "Foo::Stack"),
Copy link
Member

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?

Copy link
Member Author

@GuillaumeGomez GuillaumeGomez Mar 7, 2017

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),
}

Copy link
Member

@frewsxcv frewsxcv Mar 8, 2017

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 ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so cruel? :O

@GuillaumeGomez
Copy link
Member Author

Updated.

@frewsxcv
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 11, 2017

📌 Commit a5a3981 has been approved by frewsxcv

arielb1 pushed a commit to arielb1/rust that referenced this pull request Mar 11, 2017
… r=frewsxcv

Add missing example for Display::fmt

r? @frewsxcv
bors added a commit that referenced this pull request Mar 11, 2017
Rollup of 13 pull requests

- Successful merges: #40146, #40299, #40315, #40319, #40344, #40345, #40367, #40372, #40373, #40385, #40400, #40404, #40431
- Failed merges:
arielb1 pushed a commit to arielb1/rust that referenced this pull request Mar 11, 2017
… r=frewsxcv

Add missing example for Display::fmt

r? @frewsxcv
bors added a commit that referenced this pull request Mar 12, 2017
Rollup of 12 pull requests

- Successful merges: #40146, #40299, #40315, #40319, #40344, #40345, #40372, #40373, #40400, #40404, #40419, #40431
- Failed merges:
@bors bors merged commit a5a3981 into rust-lang:master Mar 12, 2017
@GuillaumeGomez GuillaumeGomez deleted the fmt-display-example branch March 13, 2017 10:24
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

Successfully merging this pull request may close these issues.

None yet

3 participants