I32 implements A+B but not trait C: A+B

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);
}

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

Yes, you can do literally this:

pub trait Math: Num + One + Zero + PartialOrd + RemAssign +  Ord {}
impl<T> Math for T where T: Num + One + Zero + PartialOrd + RemAssign +  Ord {}
3 Likes

Incidentally, you don't need + One + Zero here since num_traits::Num already requires these. Also Ord requires/implies PartialOrd.

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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.