[SOLVED]: Unable to use 'Rem' trait in conjunction with 'PartialEq' in a generic context

EDIT: SOLVED! by @cole-miller. Here is the working code for the example: Correct use of Rem<Output=T>

I have been unable to use % (Rem) operator with == operator in a generic context. The error I receive seems to not make sense. Here is an example of using code that only uses == in a generic context:

and here is several variations of code that does not work:

It seems like just having: Rem<u8> + From<u8> + PartialEq should allow the modulo and == comparison to work (by converting u8's to T's with from) and it shouldn't be a problem.

What am I missing?

The error I'm getting is complaining about the == operator as follows:

error[E0369]: binary operation `==` cannot be applied to type `<T as Rem>::Output`
 --> src/main.rs:4:24
  |
4 |     if el % T::from(8) == T::from(0) {
  |        --------------- ^^ ---------- T
  |        |
  |        <T as Rem>::Output

You need to constrain the Output associated type on the Rem impl for T, e.g. where T: Rem<u8, Output = T> (in addition to the PartialEq bound).

1 Like

Thanks a bunch. Still learning and I keep forgetting to check on associated types when doing this sort of thing.

EDIT: Here is the correct solution as recommended by @cole-miller: Correct Solution

1 Like

To be fair, the std::ops traits are a genuine PITA to use for generic arithmetic like this (as many others have noted).

1 Like

By the way there should be a tick button you can press next to the post that is the solution to mark this question as solved.

1 Like

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.