Pre-RFC: Expose `LoopState` to make `try_fold` loops more idiomatic

(I opened an issue #75744 about this before realizing that, as a feature request, it requires the full process.)

I work with an organization that has a large amount of Rust graph traversal code as part of its core business. We used to use itertools 's fold_while for short-circuiting functional-style loops over slices of our graphs, which is now deprecated in favor of try_fold .

try_fold is great, but the lack of a standard library provided Try implementation that makes the loop semantics clear is confusing. We created our own, which is fine, but I think it would make a lot of sense to expose LoopState and provide an example in the docs.

Originally related to: rust-itertools/itertools#469

As I mentioned there, the naíve approach here is pretty ugly:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=23c6327290f7a03503aa2ea5146b31e5

Specifically, existing implementations of Try that most users are familiar with either won't return a value (Option) or semantically denote success and failure (Result). The fold_while use case is a common one and should be supported by the standard library in an idiomatic way.

10 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.