What is more idiomatic way to write this code? I'm trying to write some function that gets iterator with known Item type (but with generally unknown specific type) does some iterator actions (map, filter, itertools' group_by, etc) and returns transformed iterator. Return type of this function can be very complex and I have to write top-level functions to make it possible to specify the return type. I'm feeling it could be done in some more clear way.
No, it is not a trait object, that would be the dyn Trait syntax. The compiler does not use dynamic dispatch in this situation.
At compile time, the compiler will use type inference to figure out what the concrete actual type is, and use that as the return type. Note that this means that you have to actually return a specific concrete type. For example, this will fail to compile:
If you do come across the situation I gave as an example, you can use the Either type from itertools to merge the two kinds of iterators into a single type.