I'm having a E0599 (no associated item named Variant1
found for type parameter MyEnum
in the current scope) error with the following code :
enum MyEnum {
Variant1,
Variant2,
Variant3
}
struct MyStruct {}
trait MyTrait<T> {
fn get_some_variant(self) -> T;
}
impl<MyEnum> MyTrait<MyEnum> for MyStruct {
fn get_some_variant(self) -> MyEnum {
MyEnum::Variant1
}
}
Here is a Playground link.
Why can't I build a variant from my enum in this case ?
What are the workaround ? Are enums not meant to be used as generics ?
CodePudding user response:
impl<MyEnum>
defines MyEnum
as an arbitrary generic type, which is shadowing your actual MyEnum
definition.
impl MyTrait<MyEnum> for MyStruct {
fn get_some_variant(self) -> MyEnum {
MyEnum::Variant1
}
}
works as expected and creates an implementation for MyTrait<MyEnum>
.