Ord — 전체 순서 트레잇

Ord — 전체 순서 트레잇

Ord는 전체 순서(total order)를 제공하는 트레잇이에요. 모든 두 값 사이의 순서를 완전하게 비교해요.

출처: Rust 공식 문서

본문

pub trait Ord: Eq + PartialOrd<Self> {
    fn cmp(&self, other: &Self) -> Ordering;

    fn max(self, other: Self) -> Self { ... }
    fn min(self, other: Self) -> Self { ... }
    fn clamp(self, min: Self, max: Self) -> Self { ... }
}

필수 메서드

// this와 other를 비교해 Ordering을 반환해요.
fn cmp(&self, other: &Self) -> Ordering;

OrderingLess, Equal, Greater 셋 중 하나예요. Ord를 구현하면 <, >, <=, >= 연산과 sort, max, min 등을 사용할 수 있어요. PartialOrd와 달리 NaN 같은 비교 불가 값이 없어요.

더 알아보기 (Learn more)