Implement Enum Methods in Rust
Enums in Rust are powerful for representing a fixed set of related values. Beyond simple data representation, you can associate behavior with enums by implementing methods directly on them. This challenge will test your understanding of defining and using methods within an enum definition.
Problem Description
Your task is to create an enum that represents different types of shapes (e.g., Circle, Rectangle) and then implement a method on this enum to calculate its area. This is a common pattern for encapsulating shape-specific logic within a unified type.
Key Requirements:
- Define an Enum: Create an enum named
Shapewith variants for at least two geometric shapes.Circle: Should store the radius (a floating-point number, e.g.,f64).Rectangle: Should store the width and height (floating-point numbers, e.g.,f64).
- Implement an
areaMethod: Implement a method namedareaon theShapeenum. This method should:- Return the calculated area of the shape as an
f64. - Use the appropriate formula for each shape variant.
- Circle Area: π * radius²
- Rectangle Area: width * height
- Return the calculated area of the shape as an
- Use
std::f64::consts::PI: For the value of π when calculating the circle's area.
Expected Behavior:
When you create instances of the Shape enum and call the area method on them, the method should return the correct numerical area for that specific shape.
Edge Cases:
- Consider shapes with zero or negative dimensions. While geometrically these might not make sense, your
areamethod should gracefully handle them (e.g., return 0 or a mathematically correct negative area if applicable, though for this problem, returning 0 for non-positive dimensions is acceptable).
Examples
Example 1:
Input:
let circle = Shape::Circle(5.0);
println!("Area of circle: {}", circle.area());
let rectangle = Shape::Rectangle(10.0, 4.0);
println!("Area of rectangle: {}", rectangle.area());
Output:
Area of circle: 78.53981633974483
Area of rectangle: 40.0
Explanation: The circle with radius 5.0 has an area of π * 5.0² ≈ 78.54. The rectangle with width 10.0 and height 4.0 has an area of 10.0 * 4.0 = 40.0.
Example 2:
Input:
let small_circle = Shape::Circle(1.0);
println!("Area of small circle: {}", small_circle.area());
let square = Shape::Rectangle(7.0, 7.0);
println!("Area of square: {}", square.area());
Output:
Area of small circle: 3.141592653589793
Area of square: 49.0
Explanation: A circle with radius 1.0 has an area of π * 1.0² ≈ 3.14. A square (which is a type of rectangle) with sides 7.0 has an area of 7.0 * 7.0 = 49.0.
Example 3 (Edge Case):
Input:
let invalid_circle = Shape::Circle(-2.0);
println!("Area of invalid circle: {}", invalid_circle.area());
let zero_rectangle = Shape::Rectangle(0.0, 5.0);
println!("Area of zero rectangle: {}", zero_rectangle.area());
Output:
Area of invalid circle: 0.0
Area of zero rectangle: 0.0
Explanation: For non-positive dimensions, the area method should return 0.0.
Constraints
- The radius, width, and height will be floating-point numbers (
f64). - The
areamethod should return anf64. - The solution should be written entirely in Rust.
Notes
- Remember to use pattern matching (
match) to handle the different variants of yourShapeenum within theareamethod. - You can create a
mainfunction to test your enum and its methods with the provided examples.