Generics 在使用泛型时是否可以使用自引用关联类型?

Generics 在使用泛型时是否可以使用自引用关联类型?,generics,rust,traits,associated-types,Generics,Rust,Traits,Associated Types,示例:鉴于以下特点 trait DirectedAcyclicGraph<V, E> where V: Add, E: Add 但那只是在黑暗中的一个镜头 Add的文档提供了以下示例 use std::ops::Add; struct Foo; impl Add for Foo { type Output = Foo; fn add(self, _rhs: Foo) -> Foo { println!("Adding!");

示例:鉴于以下特点

trait DirectedAcyclicGraph<V, E> where V: Add, E: Add
但那只是在黑暗中的一个镜头

Add
的文档提供了以下示例

use std::ops::Add;

struct Foo;

impl Add for Foo {
    type Output = Foo;

    fn add(self, _rhs: Foo) -> Foo {
        println!("Adding!");
        self
    }
}

fn main() {
    Foo + Foo;
}

但是,如果可能的话,我无法理解如何以同样的方式为泛型类型提供实现。

这最终导致了编程错误。正如@ChrisMorgan所指出的,表达式
Add
可以编译。编译器哀号的原因是在生产源代码中的一个位置,没有一致地添加源代码Output=V

遗憾的是,我在编译浓缩的MWE时一定也犯了一些错误

因此,留给子孙后代的收获是

trait DirectedAcyclicGraph<V, E> where V: Add<Output = V>, E: Add<Output = E>
trait-directedacycgraph,其中V:Add,E:Add

有效。

您是否尝试了您认为可能正确的方向?在我看来是正确的。是的,不起作用。现在在手机上。我可以在以后跟进相关错误。谢谢,现在它可以工作了。我没有在trait的特定实现者处添加
Output=
,这导致编译器输出中出现很多错误,所以我只是没有添加。
trait DirectedAcyclicGraph<V, E> where V: Add<Output = V>, E: Add<Output = E>