# Handling errors from iterators

**URL:** <https://users.rust-lang.org/t/handling-errors-from-iterators/2551>\
**Category:** uncategorized\
**Created:** [August 19, 2015, 9:45pm UTC](https://users.rust-lang.org/t/handling-errors-from-iterators/2551 "2015-08-19T21:45:17Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![isagalaev](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/isagalaev/32/23479_2.png) [@isagalaev](https://users.rust-lang.org/u/isagalaev)\
**Post date:** [August 26, 2015, 8:15pm UTC](https://users.rust-lang.org/t/handling-errors-from-iterators/2551/15 "2015-08-26T20:15:34Z")

</div>

With the reasoning that everything can be done with simple loops and if statements we'd never have any convenience methods at all 🙂 My intention was to use my own perceived difficulty at implementing faulty iterators as an example and find out if there were something that can be improved on the language/stdlib level.

I do concede my point that Rust needs a harder treatment for errors, based on the examples in this thread. However there are still two things left that I feel can be improved:

- `try!` isn't usable inside `next()` — and I don't know what a good fix here might be for the general case. For now I'm content with my [local variety of it](https://github.com/isagalaev/ijson-rust/blob/errors/src/errors.rs#L5-L12).
- There's no obvious iterator adaptor implementing the behaviour I'm after: yield an error and then stop. All suggested ways have slight warts on them, like `take_while` would swallow the error and `fold_results` and `collect` require O(n) storage… **However!** I won't press this point any further as I can't really present a good convincing example, even for myself. Probably this use-case is indeed an edge case.

---

_[View the full topic](https://users.rust-lang.org/t/handling-errors-from-iterators/2551)._
