eval
Log in

Static Dispatch with a Trait Bound

10 points

Circle and Square already implement Shape (given, below — do not change them). Implement describe_shape, a generic function bounded by T: Shape, that returns "area = {area:.2}". Because T is fixed per call site, the compiler monomorphises a separate copy of this function for each concrete shape — no vtable involved.

Required signature

pub trait Shape {     fn area(&self) -> f64; }  pub struct Circle { pub radius: f64 } impl Shape for Circle {     fn area(&self) -> f64 { std::f64::consts::PI * self.radius * self.radius } }  pub struct Square { pub side: f64 } impl Shape for Square {     fn area(&self) -> f64 { self.side * self.side } }  pub fn describe_shape(shape: &T) -> String;

Visible tests

Your solution

Log in to submit a solution.