我目前的代码如下:
pub trait A {}
pub trait HasA {
type A: A;
fn gimme_a() -> Self::A;
}
pub trait RichA: A {}
pub trait RichHasA: HasA {
type A: RichA;
fn gimme_a() -> Self::A;
// ... more things go here ...
}
pub fn main() {}
我的目标是能够将RichHasA
用作HasA
。上面的代码无法编译:
error[E0221]: ambiguous associated type `A` in bounds of `Self`
--> src/main.rs:10:21
|
3 | type A: A;
| ---------- ambiguous `A` from `HasA`
...
9 | type A: RichA;
| -------------- ambiguous `A` from `RichHasA`
10 | fn gimme_a() -> Self::A;
| ^^^^^^^ ambiguous associated type `A`
这是有道理的。如何消除相关类型的歧义?
答案 0 :(得分:6)
您可以使用所谓的完全限定语法(FQS):
{{1}}