[resolved] Trying to store an iterator: "cannot infer an appropriate lifetime for autoref due to conflicting requirements"



i am trying to wrap an iterator in another iterator in order to apply some filtering.
The original iterator is returned in a Box, because the function that returns it is defined in a trait and the concrete iterator struct is not known.

I am struggling to resolve the error:



Box<Trait> is actually shorthand for Box<Trait + 'static>, so the FilteredIterator type requires that the inner iterator not have any lifetime restrictions, which is demonstrably not true, since you parameterised on an arbitrary lifetime 'a.

The solution is to parameterise FilteredIterator in the same way:

struct FilteredIterator<'a> {
    // Some filter criteria.
    // [...]

    // The original iterator.
    iterator: Box<Iterator<Item=u32> + 'a>,

impl<'a> Iterator for FilteredIterator<'a> {

// ... the rest is the same ...


That makes sense, thank you.