← All Exercises

Traits with Generic Bounds

intermediaterust~25min

Background

Traits are Rust approach to polymorphism, enabling shared behavior across different types.

Problem

Create a trait with a generic implementation that works for any type satisfying specific bounds.

Input

Types implementing a trait

Output

Polymorphic behavior

Sample tests

Sample 1

Input: trait Area for Circle and Square
Expected: shared area method

Your solution

Learning objective

Learn trait-based polymorphism and static vs dynamic dispatch