I'd like to create different 'flavors' of a struct. This means that depending on which enum I'm passing into the structure's constructor I'll get a version of the structure whose 'flavor' trait's type depends on that enum value. I know that I must specify a single type for my structure's fields, but what if I want that type to change based on the value I'm passing into the constructor? Can I only do something like this using traits?
pub struct ChocolateFlavor {
coco: u32,
}
pub struct VanillaFlavor {
bean: u32,
}
pub struct IceCream {
flavor: (), //should be able to be ChocholateFlavor or VanillaFlavor
}
pub enum Flavors {
Vanilla,
Chocholate,
}
impl IceCream {
pub fn new(input_type: Flavors) -> Self {
match input_type {
Flavors::Vanilla => input = VanillaFlavor,
Flavors::Chocholate => input = ChocolateFlavor,
}
let mut new_icecream = LabeledInput { flavor: input };
new_icecream
}
}
fn main() {
//give me a chocholate ice cream
let mut my_chocholate = IceCream::new(Flavors::Chocolate);
//give me a vanilla ice cream
let mut my_vanilla = IceCream::new(Flavors::Vanilla);
}