std::iter::Map as an associated type

I have a trait that looks similar to this one

trait MakeIter<T> {
    type Iter: Iterator<Item = T>;
    fn make_iter(&self) -> Self::Iter;
}

Can I specify std::iter::Map as Iter in an implementation? Map takes FnMut(Self::Item) -> B as an argument which can't be expressed as a concrete type.

1 Like

You can use the function pointer type.

But it supposed to be a capturing closure

There is the unstable feature type_alias_impl_trait for this kind of use-case; on stable Rust you will have to use dyn in some form unfortunately, either with a Box<dyn FnMut ...> closure type, or with a Box<dyn Iterator...> iterator type.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.