summaryrefslogtreecommitdiff
path: root/ex5_4/src
diff options
context:
space:
mode:
Diffstat (limited to 'ex5_4/src')
-rw-r--r--ex5_4/src/main.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/ex5_4/src/main.rs b/ex5_4/src/main.rs
new file mode 100644
index 0000000..48ad8c5
--- /dev/null
+++ b/ex5_4/src/main.rs
@@ -0,0 +1,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();
+ }
+}