1

I have the following code

pub fn evens<T>(iter: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
    iter.filter(|x| x % 2 == 0)
}

But it doesn't compile because

error[E0369]: cannot mod `&T` by `{integer}`
  --> src/lib.rs:10:23
   |
10 |     iter.filter(|x| x % 2 == 0)
   |                     - ^ - {integer}
   |                     |
   |                     &T
   |
help: consider restricting type parameter `T`
   |
9  | pub fn evens<T: std::ops::Rem<Output = {integer}>>(iter: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
   |               +++++++++++++++++++++++++++++++++++

What do I need to do to operate on T type to check if it is even? I tried casting T but that didn't work.

I also tried using the suggested function signature in the error message, but that led to a whole host of new problems:

error: cannot constrain an associated constant to a value
 --> src/lib.rs:9:31
  |
9 | pub fn evens<T: std::ops::Rem<Output = {integer}>>(iter: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
  |                               ------^^^---------
  |                               |        |
  |                               |        ...cannot be constrained to this value
  |                               this associated constant...

error[E0282]: type annotations needed
  --> src/lib.rs:10:10
   |
10 |     iter.filter(|x| x % 2 == 0)
   |          ^^^^^^ cannot infer type
   |
   = note: type must be known at this point

error[E0599]: no method named `filter` found for type parameter `impl Iterator<Item = T>` in the current scope
  --> src/lib.rs:10:10
   |
10 |     iter.filter(|x| x % 2 == 0)
   |          ^^^^^^ method not found in `impl Iterator<Item = T>`
   |
   = note: `iter` is a function, perhaps you wish to call it
   = help: items from traits can only be used if the type parameter is bounded by the trait
Nate Houk
  • 335
  • 1
  • 10
  • 1
    Have you tried copy-pasting the suggested fix out of the error message? The error gives you a pretty clear-cut suggestion on what to do. – Silvio Mayolo Feb 16 '22 at 15:54
  • Yes I did, and I ended up with even more errors. I have edited my post above to show this. – Nate Houk Feb 16 '22 at 15:58
  • Oh, I see. Replace `{integer}` with `i32` or some specific integer type (depending on your needs) and see if that works. It's kind of weird that the suggestion writes `{integer}` there; it's not a real type but a pseudo-type used internally in the compiler. – Silvio Mayolo Feb 16 '22 at 16:01
  • 1
    This question really resembles [this one](https://stackoverflow.com/questions/69032551/in-rust-how-can-i-restrict-a-generic-t-to-allow-modulus). – at54321 Feb 16 '22 at 16:03
  • Thanks @at54321, that was my exact question. I also misread the question, and thought we needed to filter on even numbers in the vector, not on even indexes. It is much harder to filter on the items in the vector. – Nate Houk Feb 16 '22 at 16:13

1 Answers1

-1

Assuming you want to filter on INDEX (and not VALUE), then the following code works:

pub fn evens<T>(iter: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
    iter.enumerate().filter(|(i, _)| i % 2 == 0).map(|(_, e)| e)
}
Nate Houk
  • 335
  • 1
  • 10