summaryrefslogtreecommitdiff
path: root/ex5_4/src/main.rs
blob: 48ad8c5cd3aa17c2cdf7390ba9df0ee0ade65c17 (plain)
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
enum Shape {
    Circle { radius: f64 },
    Square { border: f64 },
    Rectangle { width: f64, height: f64 },
}

impl Shape {
    pub fn radius(&self) -> f64 {
        let Shape::Circle { radius } = self else {
            panic!("not circle");
        };
        *radius
    }
}

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn t0() {
        let s: Shape = Shape::Circle { radius : 24.0 };
        assert_eq!(s.radius(), 24.0);
    }

    #[test]
    #[should_panic(expected = "not circle")]
    fn t1() {
        let s: Shape = Shape::Square { border: 1.0 };
        s.radius();
    }
}