在从cmp()和PartialEq特性实现Ord和PartialEq时,我尝试将不止一个条件结合起来。像这样的东西:
self.id.cmp(&other.id) && self.age.cmp(&other.age)下面是一个减去合并条件的工作示例:
use std::cmp::Ordering;
#[derive(Debug, Clone, Eq)]
pub struct Person {
pub id: u32,
pub age: u32,
}
impl Person {
pub fn new(id: u32, age: u32) -> Self {
Self {
id,
age,
}
}
}
impl Ord for Person {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl PartialOrd for Person {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Person {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}https://stackoverflow.com/questions/67335967
复制相似问题