비ꡐ

μ΄λŸ¬ν•œ νŠΈλ ˆμž‡μ€ κ°’ κ°„μ˜ 비ꡐλ₯Ό μ§€μ›ν•©λ‹ˆλ‹€. λͺ¨λ“  νŠΈλ ˆμž‡μ€ μ΄λŸ¬ν•œ νŠΈλ ˆμž‡μ„ κ΅¬ν˜„ν•˜λŠ” ν•„λ“œλ₯Ό ν¬ν•¨ν•˜λŠ” νƒ€μž…μ— λŒ€ν•΄ 상속될 수 μžˆμŠ΅λ‹ˆλ‹€.

PartialEq and Eq

PartialEqλŠ” ν•„μˆ˜ λ©”μ„œλ“œμΈ eq 및 제곡된 ne λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ” λΆ€λΆ„ λ“±κ°€ κ΄€κ³„μž…λ‹ˆλ‹€. == 및 != μ—°μ‚°μžλŠ” μ΄λŸ¬ν•œ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.

struct Key {
    id: u32,
    metadata: Option<String>,
}
impl PartialEq for Key {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

Eq is a full equivalence relation (reflexive, symmetric, and transitive) and implies PartialEq. Functions that require full equivalence will use Eq as a trait bound.

PartialOrd 및 Ord

PartialOrdλŠ” partial_cmp λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ λΆ€λΆ„ μˆœμ„œλ₯Ό μ •μ˜ν•©λ‹ˆλ‹€. <, <=, >=, > μ—°μ‚°μžλ₯Ό κ΅¬ν˜„ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.

use std::cmp::Ordering;
#[derive(Eq, PartialEq)]
struct Citation {
    author: String,
    year: u32,
}
impl PartialOrd for Citation {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match self.author.partial_cmp(&other.author) {
            Some(Ordering::Equal) => self.year.partial_cmp(&other.year),
            author_ord => author_ord,
        }
    }
}

OrdλŠ” 전체 μˆœμ„œ 지정이며 cmpλŠ” Ordering을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

This slide should take about 10 minutes.

PartialEqλŠ” μ„œλ‘œ λ‹€λ₯Έ νƒ€μž… 간에 κ΅¬ν˜„λ  수 μžˆμ§€λ§Œ EqλŠ” κ΅¬ν˜„λ  수 μ—†μŠ΅λ‹ˆλ‹€. λ°˜μ‚¬μ μ΄κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

struct Key {
    id: u32,
    metadata: Option<String>,
}
impl PartialEq<u32> for Key {
    fn eq(&self, other: &u32) -> bool {
        self.id == *other
    }
}

μ‹€μ œλ‘œ μ΄λŸ¬ν•œ νŠΈλ ˆμž‡μ„ μƒμ†ν•˜λŠ” 것은 μΌλ°˜μ μ΄μ§€λ§Œ κ΅¬ν˜„ν•˜λŠ” 것은 λ“œλ¬Έ μΌμž…λ‹ˆλ‹€.