# Iterator that returns \`Result\` instead of \`Option\`

**URL:** <https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023>\
**Category:** uncategorized\
**Created:** [November 3, 2020, 4:59pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023 "2020-11-03T16:59:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robin/32/14094_2.png) [@robin](https://users.rust-lang.org/u/robin)\
**Post date:** [November 3, 2020, 4:59pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023/1 "2020-11-03T16:59:20Z")

</div>

I am writing a tokenizer. This looks a lot like an `Iterator`, at one exception: in case the tokenizer fails to parse the input, I would like to give more context than what `None` can provide. I could return an `Option<Result<TokenType>>`, but this would be redundant since it would always return `Some(...)`. Is there anything that look like an iterator but where the error case is a type that implements `Error` instead of being `None`?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [November 3, 2020, 5:00pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023/2 "2020-11-03T17:00:00Z")

</div>

You might be interested in [fallible\_iterator::FallibleIterator - Rust](https://docs.rs/fallible-iterator/0.2.0/fallible_iterator/trait.FallibleIterator.html)

---

<div class="post-metadata">

**Author:** ![robin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robin/32/14094_2.png) [@robin](https://users.rust-lang.org/u/robin)\
**Post date:** [November 3, 2020, 5:00pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023/3 "2020-11-03T17:00:28Z")

</div>

Absolutely perfect!

---

<div class="post-metadata">

**Author:** ![robin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/robin/32/14094_2.png) [@robin](https://users.rust-lang.org/u/robin)\
**Post date:** [November 3, 2020, 5:02pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023/4 "2020-11-03T17:02:29Z")

</div>

Is there any reason why this is not in `std`?

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [November 3, 2020, 5:49pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023/5 "2020-11-03T17:49:39Z")

</div>

That trait uses `Result<Option<T>, E>` as type, but this makes it equivalent to an ordinary iterator with `Result` and `Option` transposed for convenience. You might as well use an ordinary iterator.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 1, 2021, 5:49pm UTC](https://users.rust-lang.org/t/iterator-that-returns-result-instead-of-option/51023/6 "2021-02-01T17:49:53Z")

</div>

Closed on February 1, 2021, 5:49pm UTC

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.
