Implementing Methods on Rust Enums
Enums in Rust are powerful tools for representing a set of distinct possibilities. Adding methods to enums allows you to associate behavior with each variant, making your code more organized and readable. This challenge will guide you through defining and using methods on a custom enum.
Problem Description
You are tasked with creating a Rust enum representing different types of geometric shapes. The enum should have variants for Circle, Rectangle, and Triangle. Each variant should store relevant data (radius for Circle, width and height for Rectangle, base and height for Triangle). You must then implement methods on this enum to calculate the area of each shape.
What needs to be achieved:
- Define a Rust enum named
Shapewith variantsCircle,Rectangle, andTriangle. - Each variant should hold the necessary data for calculating its area.
Circleshould hold aradius(f64),Rectangleshould holdwidthandheight(both f64), andTriangleshould holdbaseandheight(both f64). - Implement a method named
areaon theShapeenum that returns the area of the shape as anf64. - Create a function
mainthat instantiates each shape variant, calculates and prints its area using theareamethod.
Key requirements:
- The
areamethod must correctly calculate the area for each shape variant. - The code must be well-formatted and idiomatic Rust.
- The
mainfunction should demonstrate the usage of theShapeenum and itsareamethod.
Expected behavior:
The program should output the area of each shape to the console. The areas should be calculated as follows:
- Circle: π * radius^2
- Rectangle: width * height
- Triangle: 0.5 * base * height
Edge cases to consider:
- While not strictly required for this problem, consider how you might handle invalid input (e.g., negative radius, width, height, base, or height) in a real-world scenario. For this challenge, assume the input values are always positive.
Examples
Example 1:
Input:
Shape::Circle { radius: 5.0 }
Shape::Rectangle { width: 4.0, height: 6.0 }
Shape::Triangle { base: 3.0, height: 8.0 }
Output:
Area of Circle: 78.53981633974483
Area of Rectangle: 24.0
Area of Triangle: 12.0
Explanation:
The areas are calculated using the formulas mentioned above.
Example 2:
Input:
Shape::Circle { radius: 2.5 }
Shape::Rectangle { width: 2.0, height: 2.0 }
Shape::Triangle { base: 4.0, height: 4.0 }
Output:
Area of Circle: 19.634954084936208
Area of Rectangle: 4.0
Area of Triangle: 8.0
Explanation:
Again, the areas are calculated using the appropriate formulas.
Constraints
- All dimensions (radius, width, height, base) must be represented as
f64. - The
areamethod must return anf64. - The program should compile and run without errors.
- The output should be formatted clearly, indicating the shape and its calculated area.
Notes
- Remember that methods are defined using the
implblock. - You can use the
std::f64::consts::PIconstant for the value of π. - Focus on the core task of implementing the
areamethod for each variant of the enum. Error handling is not a primary focus of this challenge. - Consider using
println!for outputting the results.