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
pub trait MakeMin: PartialOrd + Sized {
    fn make_min(&mut self, other: Self) -> bool {
        let tmp = *self > other;
        if tmp {
            *self = other;
        }
        tmp
    }
}

pub trait MakeMax: PartialOrd + Sized {
    fn make_max(&mut self, other: Self) -> bool {
        let tmp = *self < other;
        if tmp {
            *self = other;
        }
        tmp
    }
}

impl<T: PartialOrd + Sized> MakeMin for T {}
impl<T: PartialOrd + Sized> MakeMax for T {}

#[test]
fn test() {
    let mut a = 10_i32;
    a.make_max(20);
    assert_eq!(a, 20);
    a.make_max(15);
    assert_eq!(a, 20);
    a.make_min(10);
    assert_eq!(a, 10);
    a.make_min(15);
    assert_eq!(a, 10);
}