Yeah I did see the feature which i guess is in the nightly build. I am on the stable build (1.22.1) and I was trying to find a different way to implement this functionality. Guess it isn't possible then.
impl Trait won’t help here. You can either return the type in a Box or create a custom enum that holds the different types (assuming you can name them).
Thanks. Well, the types are a structure that implement a certain trait. So I am wondering if there is a better way to solve this. Also how does box-ing it solve the issue?
Boxing allows you to return a trait object, which has its type erased - that allows you to return different underlying types across your match arms - caller just sees a trait object.
I am thinking of rewriting the entire thing by implementing the trait on the enum directly. Is this present in the stable build? Cause when I searched online i found an RFC of September.
What would the meaning of this code be? Foo::Bar is not a type, so implementing a trait on it makes no sense. You could implement a trait on the enum itself. If payload for each enum variant implements a trait, you can implement that trait on the enum like this:
I am hitting this problem too, and from reading the above it seems returning "impl Trait" might be an answer although it's a newer feature?
Specifically my use case is matching over strings and then returning different types which all have a particular Trait - something like a switch/return in other languages, but with the constraint that they all share a specific Trait.
What would be the best way to go about that these days? I'd prefer not to have to write new enums to handle this, if possible...
Well, then you'd go for a trait object, i.e., dyn Trait, or in your case it'd probably be best to have a Box<dyn Trait> because trait objects are !Sized