๋น๊ต
์ด๋ฌํ ํธ๋ ์์ ๊ฐ ๊ฐ์ ๋น๊ต๋ฅผ ์ง์ํฉ๋๋ค. ๋ชจ๋ ํธ๋ ์์ ์ด๋ฌํ ํธ๋ ์์ ๊ตฌํํ๋ ํ๋๋ฅผ ํฌํจํ๋ ํ์ ์ ๋ํด ์์๋ ์ ์์ต๋๋ค.
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
์ ๋ฐํํฉ๋๋ค.
PartialEq
๋ ์๋ก ๋ค๋ฅธ ํ์
๊ฐ์ ๊ตฌํ๋ ์ ์์ง๋ง Eq
๋ ๊ตฌํ๋ ์ ์์ต๋๋ค. ๋ฐ์ฌ์ ์ด๊ธฐ ๋๋ฌธ์
๋๋ค.
struct Key { id: u32, metadata: Option<String>, } impl PartialEq<u32> for Key { fn eq(&self, other: &u32) -> bool { self.id == *other } }
์ค์ ๋ก ์ด๋ฌํ ํธ๋ ์์ ์์ํ๋ ๊ฒ์ ์ผ๋ฐ์ ์ด์ง๋ง ๊ตฌํํ๋ ๊ฒ์ ๋๋ฌธ ์ผ์ ๋๋ค.