-
-
Save rust-play/67df891101993e82cd8876657da91ead to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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 characters
pub enum Shape { | |
Square(f64), | |
Circle(f64), | |
Triangle { base: f64, height: f64 }, | |
} | |
impl Shape { | |
pub fn area(&self) -> f64 { | |
match self { | |
Shape::Square(side) => side * side, | |
Shape::Circle(radius) => std::f64::consts::PI * radius * radius, | |
Shape::Triangle { base, height } => 0.5 * base * height, | |
} | |
} | |
} | |
fn main() { | |
let square = Shape::Square(4.0); | |
let circle = Shape::Circle(3.0); | |
let triangle = Shape::Triangle { base: 5.0, height: 2.0 }; | |
println!("Area of the square: {}", square.area()); | |
println!("Area of the circle: {}", circle.area()); | |
println!("Area of the triangle: {}", triangle.area()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment