试图回答时
this question
trait MyTrait {
type A;
type B;
}
trait WithFoo: MyTrait {
fn foo(a: Self::A) -> Self::B;
}
impl<T, U: MyTrait<A = T, B = T>> WithFoo for U {
fn foo(a: T) -> T {
a
}
}
struct S1;
impl MyTrait for S1 {
type A = u32;
type B = f32;
}
impl WithFoo for S1 {
fn foo<T>(a: Self::A) -> Self::B {
a as f32
}
}
struct S2;
impl MyTrait for S2 {
type A = u32;
type B = u32;
}
fn main() {
S1::foo(42);
S2::foo(42);
}
playground
编译失败,出现以下错误:
error[E0119]: conflicting implementations of trait `WithFoo` for type `S1`:
--> src/main.rs:23:1
|
10 | impl<T, U: MyTrait<A = T, B = T>> WithFoo for U {
| ----------------------------------------------- first implementation here
...
23 | impl WithFoo for S1 {
| ^^^^^^^^^^^^^^^^^^^ conflicting implementation for `S1`
根据
these
answers
S1
impl
当编译器不能确定某个未来的毯子
执行
会导致
第一节
但是在这种情况下,不可能
第一节
MyTrait
具有
A == B
因为它已经实现了
我的特质
具有
A != B
. 这个错误是当前编译器实现的一个限制,可能在以后的某个时候被解除,还是我还缺少其他东西?