trait

Ord — total ordering

Ord requires Eq and PartialOrd and describes a total order. Deriving it on a struct compares fields in declaration order. Manual implementations must keep cmp, partial_cmp and equality consistent. Expected stdout (not executed): 9 Common mistake: Using floating-point values directly with a sort operation requiring Ord.

Formal Syntax & Structure

rust syntaxrust
fn cmp(&self, other: &Self) -> Ordering

Working Example

Example Snippetrust
#[derive(PartialEq, Eq, PartialOrd, Ord)]struct Job { priority: u8, id: u8 }fn main() {    let mut queue = [Job { priority: 2, id: 1 }, Job { priority: 1, id: 9 }];    queue.sort();    println!("{}", queue[0].id);}
Rust Reference