Skip to content
เข้าสู่ระบบ

Comparison Traits

PartialEq, Eq, PartialOrd, Ord

fn main() {
// PartialEq - equality comparison (==, !=)
let a = 42;
let b = 42;
let c = 100;
println!("=== PartialEq ===");
println!("{} == {}: {}", a, b, a == b);
println!("{} != {}: {}", a, c, a != c);
// Strings
let s1 = String::from("hello");
let s2 = String::from("hello");
let s3 = String::from("world");
println!("\ns1 == s2: {}", s1 == s2);
println!("s1 == s3: {}", s1 == s3);
// Can compare different types
let string = String::from("hello");
let str_slice = "hello";
println!("\nString == &str: {}", string == str_slice);
// NaN is not equal to itself
let nan = f64::NAN;
println!("\nNaN == NaN: {}", nan == nan); // false!
println!("That's why floats only impl PartialEq, not Eq");
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด