diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-12-28 12:53:02 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-12-28 12:53:02 +0100 |
commit | 0c391a2b073b1b0d6f5ea3050be81eb6e2539ba8 (patch) | |
tree | 6da0dc014f6e21716c9aa0a4abe4ab6cc2a14079 | |
parent | e034947019d5fa63ea9103d73cdc4e2f52ee7c35 (diff) |
Add examples
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | README.md | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -9,7 +9,41 @@ A collection of helpful iterator adaptors for iterating over `Result<T, E>`. +## Examples +Here go some examples what you can do with the library. + +* Altering T in `Iterator<Item = Result<T, E>>` + +```rust +iter.map(|r| r.map(|a| alter(a))) // stdlib +iter.map_ok(|a| alter(a)) // resiter +``` + + +* Altering T in `Iterator<Item = Result<T, E>>` with a function that might fail: + +```rust +iter.map(|r| r.and_then(|a| alter(a))) // stdlib +iter.and_then_ok(|a| alter(a)) // resiter +``` + + +* Unpacking T in `Iterator<Item = Result<Option<T>, E>>` + +```rust +iter.map(|r| r.and_then(|o| o.ok_or_else(|| error()))) // stdlib +iter.inner_ok_or_else(|| error()) // resiter +``` + + +* Executing code for each error in `Iterator<Item = Result<T, E>>` + +```rust +iter.map(|r| if let Err(e) = r { foo(); Err(e) } else { r })) // stdlib +iter.map(|r| r.map_err(|e| { foo(); e })) // stdlib +iter.on_err(|e| foo()) // resiter +``` ## License |