As an exercism exercise, I'm currently trying to filter an iterator according to whether the value is even in order to produce a new iterator.
My function currently looks like:
pub fn evens<T>(iter: impl Iterator<Item = T>) -> impl Iterator<Item = T>
where T: std::ops::Rem<Output = T>
{
iter.filter(|x| x % 2 != 0)
}
But this won't compile because:
error[E0369]: cannot mod `&T` by `{integer}`
--> src/lib.rs:4:23
|
4 | iter.filter(|x| x % 2 != 0)
| - ^ - {integer}
| |
| &T
|
help: consider further restricting this bound
|
2 | where T: std::ops::Rem<Output = T> + std::ops::Rem<Output = {integer}>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
However, I know I can't simply change this to
pub fn evens<T>(iter: impl Iterator<Item = T>) -> impl Iterator<Item = T>
where T: std::ops::Rem<Output = T> + std::ops::Rem<Output = {integer}>
{
iter.filter(|x| x % 2 != 0)
}
as this fails to compile with:
error: cannot constrain an associated constant to a value
--> src/lib.rs:2:56
|
2 | where T: std::ops::Rem<Output = T> + std::ops::Rem<Output = {integer}>
| ------^^^---------
| | |
| | ...cannot be constrained to this value
| this associated constant...
I'm vaguely aware of some "Num" traits, but the exercism doesn't seem to accept answers which require importing dependencies through Cargo.toml, so I'm looking for a native/built-in solution.
Any ideas how I can make this work?
(P.S. I've now figured out that I misunderstood the exercise, where "even" describes the enumerated index, not the value... but never mind. I'd still like to know whether/how this could be made to work.)
%
can be overloaded by theRem
trait: doc.rust-lang.org/std/ops/trait.Rem.html. So you needfn foo<T: Rem>(t: T)
– Coinstantaneous2
to aT
:) so you need another trait for that and it is not available instd
– Coinstantaneousshare
button and shared an old version, :-(. I've updated the comment, with a version that does work fori8
too. – Frenchy