Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sorted-impl-items

Level: warn · Fixed by rabot fmt · Article: Sorting

Method ordering: constructor, then pub (alpha), then private (alpha). Fine. It’s documented at the top of the class. Within each section, still alphabetical.

What it checks

Items inside an inherent impl follow the article’s documented exception: associated consts, associated types, constructors (associated functions returning Self), pub methods, then private methods, each group alphabetical. Inside impl Trait for T, consts, types, then fns.

An impl containing a macro invocation is left alone.

Don’t

#![allow(unused)]
fn main() {
impl Users {
    fn exists(&self, id: &UserId) -> bool {
        self.db.contains(id)
    }

    pub fn delete(&self, id: &UserId) {
        self.db.remove(id);
    }

    pub fn create(&self, user: User) {
        self.db.insert(user);
    }

    pub fn new(db: Database) -> Self {
        Self { db }
    }
}
}

Do

#![allow(unused)]
fn main() {
impl Users {
    pub fn new(db: Database) -> Self {
        Self { db }
    }

    pub fn create(&self, user: User) {
        self.db.insert(user);
    }

    pub fn delete(&self, id: &UserId) {
        self.db.remove(id);
    }

    fn exists(&self, id: &UserId) -> bool {
        self.db.contains(id)
    }
}
}

The constructor is where a reader starts. Public API next, in an order that needs no explaining. Implementation details last.

Silence it

#![allow(unused)]
fn main() {
// rabot: allow(sorted-impl-items) distance_to and is_near are inseparable: is_near wraps distance_to
impl GpsCoordinates {
    fn distance_to(&self, other: &Self) -> Distance { .. }
    fn is_near(&self, other: &Self, radius: Distance) -> bool { .. }
}
}

The article’s own example: one sentence says why they are together. If it takes more than one sentence, there is a separate type trying to escape.