How to bound an iterator to a concrete Item type?


I'm new to Rust and I have been messing around with big chains of iterators. I have a struct which takes a generic iterator that should emit Results, but Result isn't a trait so I can't just do this:

struct Foo<T> {
    iter: T

impl<T> Foo<T>
    where T: Iterator,
          T::Item: Result<AnotherType, ErrType> { ... }

So is there any way to make this work or should I try another approach entirely? I also tried implementing a trait for the result type I want and using that as the bound:

trait IsMyResult {}

impl IsMyResult for Result<AnotherType, ErrType> {}

but that doesn't work without writing delegating methods of Result in the trait, which is quite annoying.

Another approach I considered was using Iterator::map, passing in a function that does a step of the computation and then putting another map after that and so on, without using structs that own the iterator “behind” themselves in the chain, but I'm not sure if this is the correct way to do things; I would appreciate some pointers.

Thank you :slight_smile:

It's possible to specify the associated type in the trait bound, like this:

impl<T> Foo<T>
    where T: Iterator<Item = Result<AnotherType, ErrType>> { ... }

Wow, I had a really hard time googling about this problem and thus missed this. Thank you!

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.