Challenge: Rust Intermediate Concepts

Test your understanding of what you learned in this chapter by tackling a traits and implementations problem.

We'll cover the following

Challenge

You have been tasked with implementing a program to manage geometric shapes in Rust. However, the shapes can vary widely in their characteristics and behaviors. To ensure flexibility and maintainability, you decide to utilize Rust’s trait system. Your challenge is to implement a set of traits and structures to represent different shapes and their properties.

Task

Create a Rust program that models different geometric shapes using traits. Define a trait called Shape with the following methods:

  • The area() method: This is a method that calculates and returns the area of the shape.

  • The perimeter() method: This is a method that calculates and returns the perimeter of the shape.

Implement this trait for three different shapes: circle, rectangle, and triangle.

Try it yourself

The skeleton code has already been given to you. You only need to create the implementations for each shape using the Shape trait.

Get hands-on with 1400+ tech skills courses.