9 releases

0.3.0 May 22, 2023
0.2.1 Dec 10, 2018
0.2.0 Mar 10, 2019
0.1.6 Dec 10, 2018
0.1.2 Jul 20, 2016

#86 in Rust patterns

Download history 643043/week @ 2024-07-21 609344/week @ 2024-07-28 626095/week @ 2024-08-04 644420/week @ 2024-08-11 635395/week @ 2024-08-18 665416/week @ 2024-08-25 654430/week @ 2024-09-01 666141/week @ 2024-09-08 614419/week @ 2024-09-15 674705/week @ 2024-09-22 691104/week @ 2024-09-29 684716/week @ 2024-10-06 694610/week @ 2024-10-13 720876/week @ 2024-10-20 686591/week @ 2024-10-27 700965/week @ 2024-11-03

2,846,966 downloads per month
Used in 2,817 crates (91 directly)

MIT/Apache

88KB
2.5K SLoC

crates.io docs.rs

Continuous integration

rust-fallible-iterator

"Fallible" iterators for Rust.

Features

If the std or alloc features are enabled, this crate provides implementations for Box, Vec, BTreeMap, and BTreeSet. If the std feature is enabled, this crate additionally provides implementations for HashMap and HashSet.

If the std feature is disabled, this crate does not depend on libstd.


lib.rs:

"Fallible" iterators.

The iterator APIs in the Rust standard library do not support iteration that can fail in a first class manner. These iterators are typically modeled as iterating over Result<T, E> values; for example, the Lines iterator returns io::Result<String>s. When simply iterating over these types, the value being iterated over must be unwrapped in some way before it can be used:

for line in reader.lines() {
    let line = line?;
    // work with line
}

In addition, many of the additional methods on the Iterator trait will not behave properly in the presence of errors when working with these kinds of iterators. For example, if one wanted to count the number of lines of text in a Reader, this might be a way to go about it:

let count = reader.lines().count();

This will return the proper value when the reader operates successfully, but if it encounters an IO error, the result will either be slightly higher than expected if the error is transient, or it may run forever if the error is returned repeatedly!

In contrast, a fallible iterator is built around the concept that a call to next can fail. The trait has an additional Error associated type in addition to the Item type, and next returns Result<Option<Self::Item>, Self::Error> rather than Option<Self::Item>. Methods like count return Results as well.

This does mean that fallible iterators are incompatible with Rust's for loop syntax, but while let loops offer a similar level of ergonomics:

while let Some(item) = iter.next()? {
    // work with item
}

Fallible closure arguments

Like Iterator, many FallibleIterator methods take closures as arguments. These use the same signatures as their Iterator counterparts, except that FallibleIterator expects the closures to be fallible: they return Result<T, Self::Error> instead of simply T.

For example, the standard library's Iterator::filter adapter method filters the underlying iterator according to a predicate provided by the user, whose return type is bool. In FallibleIterator::filter, however, the predicate returns Result<bool, Self::Error>:

let numbers = convert("100\n200\nfern\n400".lines().map(Ok::<&str, Box<Error>>));
let big_numbers = numbers.filter(|n| Ok(u64::from_str(n)? > 100));
assert!(big_numbers.count().is_err());

No runtime deps

Features