Avatar ·

Vector de objetos que implementan un trait común en Rust: ¿cómo crear y usar?

¡Hola! Soy nuevo en Rust y estoy tratando de entender el polimorfismo. Tengo un trait `Shape` con un método `area()`, y varias estructuras (`Circle`, `Rectangle`) que lo implementan. Quiero crear un vector que pueda almacenar objetos de diferentes tipos que implementen este trait. En C++ o Java escribiría algo como `vector shapes;` o `List shapes = new ArrayList<>();`. Pero en Rust no puedo simplemente escribir `Vec` — el compilador dice que el tamaño del trait no se conoce en tiempo de compilación. Aquí está mi código que no funciona: ```rust trait Shape { fn area(&self) -> f64; } struct Circle { radius: f64, } impl Shape for Circle { fn area(&self) -> f64 { std::f64::consts::PI * self.radius * self.radius } } struct Rectangle { width: f64, height: f64, } impl Shape for Rectangle { fn area(&self) -> f64 { self.width * self.height } } fn main() { let shapes: Vec = vec![ Circle { ra