我试过了:
#[enum_dispatch(BarTrait, BazTrait)]
pub enum Foo {
VariantZero,
...
}
它似乎忽略了第一个之后的任何特征,默默地。
这会导致错误,因为在这种情况下,编译器似乎不相信 Foo 实现了 BazTrait。
更新:@kmdreko 的代码只要BazTrait
与Foo
.
When BazTrait
is 在另一个 crate 中,它也使用enum_dispatch
,BazTrait
被忽略并导致以下形式的两个错误:
error[E0599]: no method named `baz` found for enum `Foo` in the current scope
--> src/main.rs:53:9
|
45 | enum Foo {
| -------- method `baz` not found for this
...
53 | foo.baz();
| ^^^ method not found in `Foo`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `baz`, perhaps you need to implement it:
candidate #1: `mylib::BazTrait`
#[enum_dispatch(BarTrait, BazTrait)]
重要的是要注意或都没有错误pub enum Foo {
。