示例:给定以下特征,
trait DirectedAcyclicGraph<V, E> where V: Add, E: Add
我希望每当一个类型的值与返回V
另一个类型的值相同类型的值时V
。每当将一个类型的值E
添加到另一个相同类型的值时,我都希望E
返回另一个值。
天真地,我认为这可能是正确的方向,
trait DirectedAcyclicGraph<V, E> where V: Add<Output = V>, E: Add<Output = E>
但这只是在黑暗中拍摄的。
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;
}
但我无法理解如何以同样的方式为泛型类型提供实现,如果这是可能的话。