Rust: Holding a Filter inside a struct

340 views Asked by At

I'm attemping to make a struct with a Filter iterator inside:

struct Foo {
    inner_iter: Filter<...>,
}

I'm then going to make Foo an Iterator itself, which will use the Filter internally.

The problem is, I can't seem to figure out what goes in between the braces after Filter. The docs say Filter is the following:

pub struct Filter<'a, A, T> {
    // some fields omitted
}

A seems to be my return type. What are 'a and T? Nothing I attempt makes it compile.

Thanks!

1

There are 1 answers

4
Vladimir Matveev On

You can see here that Filter implements Iterator trait in the following way:

impl<'a, A, T: Iterator<A>> Iterator<A> for Filter<'a, A, T>

This means that yes, A is its element type, and T is underlying iterator type. If you look into Filter definition, you will also find that 'a is lifetime of environment of predicate closure.

If you want to make your own wrapper for the Filter, it could look like this:

struct CustomWrapper<'a, A, T> {
    inner_iter: Filter<'a, A, T>
}

You need to pass through all parameters to Filter. Of course, if you're working with some specific element type, you can specify it instead of using type parameter:

struct CustomWrapper<'a, T> {
    inner_iter: Filter<'a, int, T>
}

However, you still need to pass 'a and T because they are likely to be arbitrary.