How to achieve different `impl' for each enum members?

enum Foo{
  Bar_1{},
  Bar_2{},
  Bar_3{},
}

trait common {
  fn setup();
}

//both of these impl gives compilation error
impl common for Foo::Bar_1 {

}

impl common for Foo::Bar_2 {

}

I have above code where i want to have different impl for each Bar_* . Is it even possible ? how i can achieve these behavior?

No. That is what match is for. In the previous post, RedDocMD has shown you how you can match on an enum in order to check which of its variants is currently active.

1 Like

When you are using enums as a replacement for inheritance, you typically do not need a trait.

2 Likes