Rustで<T: std::ops::Sub> T - T の返り値

<T: std::ops::Sub> T - T の返り値

std::ops::Sub::Outputが返り値になる。

AddとかMulとか、Overloadable operatorsはそんな感じらしい

doc.rust-lang.org

Associated typesと一緒にやったときちょっと引っかかったのでした。 Rustはまだ数回ググらないと、自分の知識では目的までたどり着けないなあ。

たいして目的のないコードを載せると、こんな感じです。

trait Contains {
    type A: Copy + ops::Sub;
    type B: Copy + ops::Sub + convert::From<Self::A>;

    fn contains(&self, &Self::A, &Self::B) -> bool;
    fn first(&self) -> Self::A;
    fn last(&self) -> Self::B;
}

fn difference<C: Contains>(container: &C) -> <C::B as ops::Sub>::Output {
    container.last() - C::B::from(container.first())
}

お疲れ様でした。