代码点读机 / Rust / PartialEq

PartialEq Rust · 常用Trait

部分相等 Trait,实现后可以用 == 和 != 比较;允许存在无法比较相等的值(如 NaN)。

怎么用

#[derive(PartialEq)],用于相等比较

小例子

#[derive(PartialEq, Debug)]
struct 点 { x: i32, y: i32 }
println!("{}", 点{x:1,y:1} == 点{x:1,y:1});

运行结果

true

提醒:浮点数 f32/f64 只实现 PartialEq,因为 NaN != NaN

官方文档:https://doc.rust-lang.org/std/cmp/trait.PartialEq.html

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Rust全部词条