rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
95.12k stars 12.27k forks source link

Tracking Issue for Iterator::try_reduce (feature iterator_try_reduce) #87053

Open kit-981 opened 3 years ago

kit-981 commented 3 years ago

Feature gate: #![feature(iterator_try_reduce)]

This is a tracking issue for adding a try_reduce method to the Iteratortrait. There is foldand try_fold but only reduce. It's possible for users to use try_fold directly but it's also possible to use fold directly instead of reduce. I reason that if reduce exists that a fallible try_reduce should also exist to encourage the safe handling of errors.

I use the Try trait as suggested by @sinkuu here for a similar feature.

Steps / History

Unresolved Questions

Xiretza commented 2 years ago

Can we get this toward stabilization? It's been in nightly for about half a year now, and I can't find any issues related to it.

Rua commented 1 year ago

I'd like this to be stabilised as well.

Xiretza commented 1 year ago

Stabilization Report

Implementation History

API Summary

A summary of the API can be found in the std documentation.

Experience Report

Example Usages

The method is being used as a fairly straightforward short-circuiting version of reduce(); both to abort on errors and to avoid unnecessary iteration when no more work needs to be done.

It has been part of nightly for over a year now, no related issues have been opened since. I think it should be safe to stabilize.

scottmcm commented 1 year ago

Nominated for libs-api discussion of the stabilization request above. Two important discussion topics for you:

joshtriplett commented 1 year ago

@scottmcm We discussed this in today's @rust-lang/libs-api meeting. We had the same concerns about Try/Residual that we did with array_from_fn, and don't want to move forward with stabilizing this until Try feels a bit less in flux. We didn't know what the state of evolving Try was.

joshtriplett commented 1 year ago

Also, there was a lot of interest in having a generic solution to avoid proliferating try_ methods. I personally would love to see "closures that can explicitly affect control flow" added, if we have a clear design for them.

simonvarey commented 1 year ago

I just opened #109578 which notes a type inference which seems to be missing in try_reduce.

simonvarey commented 1 year ago

Never mind, I was mistaken on that point