by devit · 5y ago · 0 comments
You can also do this:
struct Vehicle
{
speed: f32,
type: VehicleType
}
enum VehicleType
{
Car(...),
Bicycle(...)
}
Or this (although this is the least common): struct Vehicle
{
data: VehicleData
type: Box<dyn SpecificVehicle>
}
struct VehicleData
{
speed: f32,
}
trait SpecificVehicle
{
fn quack(&self, data: &VehicleData);
}
impl SpecificVehicle for Car {...}
impl SpecificVehicle for Bicycle {...}No comments yet.