Created
July 4, 2023 14:31
-
-
Save sshaplygin/5728d84d574ec8b4c31392ca2f69a1f1 to your computer and use it in GitHub Desktop.
Revisions
-
sshaplygin revised this gist
Jul 4, 2023 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ trait Figure { fn area(&self) -> f64; } struct Circle { -
sshaplygin created this gist
Jul 4, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ trait Figure { fn area(&self) -> f64; } struct Circle { radius: f64, } struct Rectangle { a: f64, b: f64, } struct Square { a: f64, } impl Figure for Circle { fn area(&self) -> f64 { self.radius * self.radius * 3.14 } } impl Figure for Square { fn area(&self) -> f64 { self.a * self.a } } impl Figure for Rectangle { fn area(&self) -> f64 { todo!() } } fn main() { let figures: Vec<Box<dyn Figure>> = vec![ Box::new(Circle{radius:1.0}), Box::new(Square{a:2.0}), Box::new(Rectangle{a:1.0, b:2.0}), ]; for figure in figures.iter() { println!("{}", figure.area()) }