From 0962a0b7416a828a1ebb00337f22b6679f34e233 Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Wed, 4 Nov 2026 17:26:18 -0600 Subject: feat: ex5_4 commit e47d24769a0cba53310c0c5feb048d6aac9a3175 Author: Brad Clawsie Date: Wed Nov 4 17:25:25 2026 -0600 feat: ex5_4 --- ex5_4/Cargo.lock | 7 +++++++ ex5_4/Cargo.toml | 6 ++++++ ex5_4/src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 ex5_4/Cargo.lock create mode 100644 ex5_4/Cargo.toml create mode 100644 ex5_4/src/main.rs diff --git a/ex5_4/Cargo.lock b/ex5_4/Cargo.lock new file mode 100644 index 0000000..96af1ff --- /dev/null +++ b/ex5_4/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ex5_4" +version = "0.1.0" diff --git a/ex5_4/Cargo.toml b/ex5_4/Cargo.toml new file mode 100644 index 0000000..b2277e1 --- /dev/null +++ b/ex5_4/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ex5_4" +version = "0.1.0" +edition = "2024" + +[dependencies] 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(); + } +} -- cgit v1.2.3