有没有一种方法可以在Rust中定义多参数特征?

时间:2019-08-08 15:59:41

标签: rust

在Haskell中,我可以定义一个引用两个或更多类型的类型类:

class Combiner a b c where
  combine :: a -> b -> c

这称为多参数类型类。有没有办法在Rust中直接或通过实现相似效果的宏编写等效特征?像这样:

trait Combiner {
    fn combine(&self1, &self2) -> self3;
}

1 个答案:

答案 0 :(得分:3)

是的,您可以参数化特征:

trait Combiner<A, B, C> {
    fn combine(a: &A, b: &B) -> C;
}

on the Rust playground