-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathmain.rs
55 lines (49 loc) · 1.13 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::cmp::Ordering;
#[derive(PartialEq, Eq)]
struct Person {
name: String,
age: i32,
height: i32,
}
impl PartialOrd for Person {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.height <= 0 || other.height <= 0 {
return None;
}
if self.height > other.height {
Some(Ordering::Greater)
} else if self.height < other.height {
Some(Ordering::Less)
} else {
Some(Ordering::Equal)
}
}
}
fn main() {
let mut class: Vec<Person> = vec![
Person {
name: "aaa".to_owned(),
age: 10,
height: 110,
},
Person {
name: "bbb".to_owned(),
age: 10,
height: 100,
},
Person {
name: "ccc".to_owned(),
age: 10,
height: 120,
},
Person {
name: "ddd".to_owned(),
age: 10,
height: 90,
},
];
class.sort_by(|a, b| a.partial_cmp(b).unwrap());
for p in class.iter() {
println!("{} is {}", p.name, p.height);
}
}