Rust constraints for generic types Item

Viewed 42

While looking through the documentation for flatten.

pub struct Flatten<I> 
where
    I: Iterator,
    <I as Iterator>::Item: IntoIterator, 

I was wondering why this is needed -- the last line. Would the following not be sufficient?

I::Item: IntoIterator,

After all I is constrained to be Iterator on the line before.

1 Answers

This is not necessary, indeed, and the actual code uses completely another form (an unstable Trait<Assoc: Trait> syntax, similar to the equality syntax Trait<Assoc = Type>). It is just rustdoc showing that, because in theory you could have a type parameter implementing two traits that both have the same associated item.

Related