Skip to content

Add Iterator::try_collect #295

@danakj

Description

@danakj

rust-lang/rust#94047

The open questions there are:

Should it have a more complicated signature to be able to return the partial results too? (@CAD97 https://internals.rust-lang.org/t/idea-fallible-iterator-mapping-with-try-map/15715/6?u=scottmcm )

No. That would defeat the primary purpose of making something more discoverable/accessible than collect() for results. If you need to do something more complicated you can use other tools for the job. Also consistency - the other try_ methods of iterators do not return partial results.

Should it take self rather than &mut self, to prevent users from accidentally continuing to use the iterator after a try_collect() failure? Note that you can still continue to use the iterator if you use by_ref() first, so it's not necessarily a functionality change.

No. Consistency with other try_ methods on iterators, and this makes it less useful. Fallible iteration is primarily useful to be able to terminate early for resuming after.

Does the name try_collect() conflict too much with the idea of collecting that's fallible in allocation? (i.e. collecting with Vec::try_reserve or similar)

The name is consistent with try_fold etc, which makes it discoverable. It should follow the same pattern for a short-circuiting fallible collect with try_collect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions