Hone logo
Hone
Problems

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:

  1. Define a Rust enum named Shape with variants Circle, Rectangle, and Triangle.
  2. Each variant should hold the necessary data for calculating its area. Circle should hold a radius (f64), Rectangle should hold width and height (both f64), and Triangle should hold base and height (both f64).
  3. Implement a method named area on the Shape enum that returns the area of the shape as an f64.
  4. Create a function main that instantiates each shape variant, calculates and prints its area using the area method.

Key requirements:

  • The area method must correctly calculate the area for each shape variant.
  • The code must be well-formatted and idiomatic Rust.
  • The main function should demonstrate the usage of the Shape enum and its area method.

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 area method must return an f64.
  • 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 impl block.
  • You can use the std::f64::consts::PI constant for the value of π.
  • Focus on the core task of implementing the area method for each variant of the enum. Error handling is not a primary focus of this challenge.
  • Consider using println! for outputting the results.
Loading editor...
rust