Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created November 1, 2023 14:14
Show Gist options
  • Save rust-play/67df891101993e82cd8876657da91ead to your computer and use it in GitHub Desktop.
Save rust-play/67df891101993e82cd8876657da91ead to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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