Why try_fold() does not take Result?

This methods takes a FnMut that returns Try, but I don't find a good use case other than using Result. The doc also mentions successful result, so Result seems to be the best option here.

Why does it take Try? Is there a reason for it?

At the moment, Try mainly supports Result, Option, and ControlFlow.

Regarding use-cases with Option, the linked documentation itself features multiple examples, e. g. with checked_add. If it wasn't supported, you would need to convert to something like Result<T, ()> and then back to Option<T>, which would be more tedious to write.

The last type, the ControlFlow enum, is similar to Result but it doesn't carry any implication about the short-circuiting case to have anything to do with actual "errors".

In the long run, eventually Try would become user-implementable, so there can be even more types supported.

4 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.