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

More Self usage in PartialOrd trait docs example #76542

Closed
leonardo-m opened this issue Sep 9, 2020 · 5 comments · Fixed by #77079
Closed

More Self usage in PartialOrd trait docs example #76542

leonardo-m opened this issue Sep 9, 2020 · 5 comments · Fixed by #77079
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@leonardo-m
Copy link

leonardo-m commented Sep 9, 2020

In this docs page:
https://doc.rust-lang.org/nightly/std/cmp/trait.PartialOrd.html

There's this code:

use std::cmp::Ordering;

#[derive(Eq)]
struct Person {
    id: u32,
    name: String,
    height: u32,
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Person {
    fn cmp(&self, other: &Person) -> Ordering {
        self.height.cmp(&other.height)
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Person) -> bool {
        self.height == other.height
    }
}

But I think docs must show the best and up to date idioms, so I think it should be replaced with (some Person replaced by more DRY Self):

use std::cmp::Ordering;

#[derive(Eq)]
struct Person {
    id: u32,
    name: String,
    height: u32,
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Person {
    fn cmp(&self, other: &Self) -> Ordering {
        self.height.cmp(&other.height)
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}
@leonardo-m
Copy link
Author

Something similar could be done for Sub:
https://doc.rust-lang.org/nightly/std/ops/trait.Sub.html

@leonardo-m
Copy link
Author

This needs a enhancement_request tag, I can't add it.

@LeSeulArtichaut
Copy link
Contributor

@leonardo-m You can use the Rustbot to set labels for you:

@rustbot modify labels: C-enhancement T-doc

@rustbot rustbot added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools labels Sep 10, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 11, 2020

@leonardo-m are you interested in making this enhancement yourself? ;) There are instructions at https://rustc-dev-guide.rust-lang.org/, although for something this simple you could probably get away with just web edits or x.py check.

@poliorcetics
Copy link
Contributor

@rustbot claim

I'll start on that today, some crude regexes and ripgrep should help a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants