Why does T implement A+B but not trait C: A+B?

Viewed 100

I'm puzzled by this example. Even though i32 implements all of the Num + One + Zero + PartialOrd + RemAssign + Ord, when I put them into a trait so I can give an alias, it doesn't work.

use num_traits::{Num, identities::One, identities::Zero};
use std::cmp::{Ord, PartialOrd};
use std::ops::RemAssign;

pub trait Math: Num + One + Zero + PartialOrd + RemAssign + Ord {}

fn s<T: Num + One + Zero + PartialOrd + Ord + RemAssign>(t: T) {
    unimplemented!();
}

fn ss<T: Math>(t: T) {
    unimplemented!();
}

fn sss() {
    let x: i32 = 5;
    ss(x);
}

Playground

Is there a way to force all the things that implement Num + One + Zero + PartialOrd + RemAssign + Ord to implement Math?

1 Answers

Yes, by adding a blanket impl:

impl<T: Num + One + Zero + PartialOrd + RemAssign + Ord> Math for T {}
Related