# Idea: Fallible iterator mapping with \`try\_map\`

**URL:** <https://internals.rust-lang.org/t/idea-fallible-iterator-mapping-with-try-map/15715>\
**Category:** language design\
**Created:** [December 4, 2021, 7:01am UTC](https://internals.rust-lang.org/t/idea-fallible-iterator-mapping-with-try-map/15715 "2021-12-04T07:01:00Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 5, 2021, 3:13am UTC](https://internals.rust-lang.org/t/idea-fallible-iterator-mapping-with-try-map/15715/5 "2021-12-05T03:13:24Z")

</div>

> [@CAD97](#):
>
> Perhaps it's actually worth adding `try_collect` just as a helper for this.

Yeah, I've wanted this for a long time for discoverability.

> [@CAD97](#):
>
> I'm unsure how `Try` v2 and `Collect` would interact for non- `Result` types.

Before Friday? Poorly.

But [Make `array::{try_from_fn, try_map}` and `Iterator::try_find` generic over `Try` by scottmcm · Pull Request #91286 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/91286) was just merged to fix [Decide on generic-ness of functions like `Iterator::try_find` and `array::try_map` · Issue #85115 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/85115), which gives a way forward for this.

So like [`try_reduce`](https://github.com/rust-lang/rust/pull/87054) just did, I think it could just be `.try_collect::<Vec<_>>()`, where the method would be `fn try_collect<B>(&mut self) -> ChangeOutputType<Self::Item, B>` -- and thus if the iterator's item is `Option<T>` the method would return `Option<Vec<T>>`, for an item type of `Result<T, E>` it'd return `Result<Vec<T>, E>`, and so on.

(The `&mut self` also helps distinguish this from `collect` -- you could resume using the iterator after the error, if you wanted.)

Want to make a PR? 🙂

---

_[View the full topic](https://internals.rust-lang.org/t/idea-fallible-iterator-mapping-with-try-map/15715)._
