Search code examples
rusttraits

How to shorten IntoIterator<Item=MyComplexType>


Typical use case is

fn process_items(items: impl IntoIterator<Item=MyComplexType>) {...}

I wonder if this could this be shortened to

fn process_items(items: impl ItemsIter) {...}

However

type ItemsIter = IntoIterator<Item=MyComplexType>

does not work and helper trait

trait ItemsIter: IntoIterator<Item=MyComplexType> {}

requires a blanket implementation. Is there a better way?


Solution

  • In addition to other answers, note that the ability to make a "trait alias" does exist, but is unstable. If you're on nightly and fine with using unstable features, you can do e.g.

    #![feature(trait_alias)]
    
    pub struct MyComplexType;
    
    trait MyComplexIterator = IntoIterator<Item = MyComplexType>;
    

    (Playground link)