# Pre-RFC: Expose \`LoopState\` to make \`try\_fold\` loops more idiomatic

**URL:** <https://internals.rust-lang.org/t/pre-rfc-expose-loopstate-to-make-try-fold-loops-more-idiomatic/12934>\
**Category:** Uncategorized\
**Created:** [August 20, 2020, 3:01pm UTC](https://internals.rust-lang.org/t/pre-rfc-expose-loopstate-to-make-try-fold-loops-more-idiomatic/12934 "2020-08-20T15:01:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoraCodes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/noracodes/32/6184_2.png) [@NoraCodes](https://internals.rust-lang.org/u/NoraCodes)\
**Post date:** [August 20, 2020, 3:01pm UTC](https://internals.rust-lang.org/t/pre-rfc-expose-loopstate-to-make-try-fold-loops-more-idiomatic/12934/1 "2020-08-20T15:01:15Z")

</div>

(I opened an issue [#75744](https://github.com/rust-lang/rust/issues/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`](https://github.com/rust-lang/rust/blob/515c9fa505e18a65d7f61bc3e9eb833b79a68618/library/core/src/iter/mod.rs#L373) and provide an example in the docs.

Originally related to: [rust-itertools/itertools#469](https://github.com/rust-itertools/itertools/issues/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](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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 18, 2020, 3:01pm UTC](https://internals.rust-lang.org/t/pre-rfc-expose-loopstate-to-make-try-fold-loops-more-idiomatic/12934/2 "2020-11-18T15:01:28Z")

</div>

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