Avatar ·

How to specify that a struct field must implement a trait in Rust?

Hi everyone! I've been writing in Rust recently, and I ran into a problem with traits. I want to create a struct that stores some value, and this value must implement a specific trait. For example, I'm writing a simple calculator where there's a trait `Operation` with a method `compute`. I want the `Calculator` struct to have an `operation` field that can be any struct implementing this trait. Here's what I'm trying to do: ```rust trait Operation { fn compute(&self, a: i32, b: i32) -> i32; } struct Calculator { operation: Operation, // Error: trait objects must include the `dyn` keyword } fn main() { let add = AddOperation; let calc = Calculator { operation: add }; println!("{}", calc.operation.compute(5, 3)); } ``` The compiler