对特征实现成员的类型提出要求

Putting a requirement on the type of a member of a trait implementation

我有一个实现另一个特征的特征:

trait RandomAccessIterator : Sub + VariousOthers {}

我如何指定,对于此特征的所有实现,减法的结果(Sub 中的 Output 类型)必须是特定类型,例如 isize?这样,如果我编写一个使用实现此特征的对象的通用函数,我知道(更重要的是,编译器知道)A - B 的结果是 isize.

类型
trait RandomAccessIterator : Sub<Output = isize> + VariousOthers {}

The Rust Programming Language chapter about associated types in the section for trait objects with associated types 中所述:

The N=Node syntax allows us to provide a concrete type, Node, for the N type parameter. Same with E=Edge. If we didn’t provide this constraint, we couldn’t be sure which impl to match this trait object to.

虽然这不是特征对象,但适用相同的语法。大多数人 运行 进入此