I have a trait that is implemented by the same struct in different ways. In order to accomplish this I have different structs that have different implementations. For abstraction I'm going to call these structs A-Z:
trait Trait {
fn bar<T: Trait> (&self, z: &Struct3<T>) -> Struct3<T>;
}
struct StructA {
name: String,
color: String,
}
impl Trait for StructA {
fn bar<T: Trait> (&self, z: &Struct3<T>) -> Struct3<T>{
let mut list = Vec::new();
list.push(self.clone());
let y = Struct2::<T> {
name: z.y.name,
list: list,
};
Struct3::<T> {
point: 1,
y: y,
}
}
}
struct StructZ {
name: String,
color: String,
}
impl Trait for StructZ {
fn bar<T: Trait> (&self, z: &Struct3<T>) -> Struct3<T>{
let mut list = Vec::new();
list.push(self.clone());
let y = Struct2::<T> {
name: z.y.name,
list: list,
};
Struct3::<T> {
point: 26,
y: y,
}
}
}
Is there another way to approach this so that each instance of the struct has a different implementation of the trait, or is making a new struct the best way to go?
I am new to compiled languages. Most of the work I have done has been using Python and TypeScript.