From c2426e34f75de78d688e5e532faf54008ba4e564 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 7 Feb 2023 17:04:03 -0300 Subject: [PATCH] Testing early stop with options --- mapoptiontest/Cargo.lock | 7 +++++++ mapoptiontest/Cargo.toml | 8 ++++++++ mapoptiontest/README.md | 4 ++++ mapoptiontest/src/main.rs | 17 +++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 mapoptiontest/Cargo.lock create mode 100644 mapoptiontest/Cargo.toml create mode 100644 mapoptiontest/README.md create mode 100644 mapoptiontest/src/main.rs diff --git a/mapoptiontest/Cargo.lock b/mapoptiontest/Cargo.lock new file mode 100644 index 0000000..03307f9 --- /dev/null +++ b/mapoptiontest/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "mapoptiontest" +version = "0.1.0" diff --git a/mapoptiontest/Cargo.toml b/mapoptiontest/Cargo.toml new file mode 100644 index 0000000..655885b --- /dev/null +++ b/mapoptiontest/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "mapoptiontest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/mapoptiontest/README.md b/mapoptiontest/README.md new file mode 100644 index 0000000..e55dfed --- /dev/null +++ b/mapoptiontest/README.md @@ -0,0 +1,4 @@ +# MapOptionTest + +Test how one can stop a collect when a map produces an option and the option is +none. diff --git a/mapoptiontest/src/main.rs b/mapoptiontest/src/main.rs new file mode 100644 index 0000000..6d18bd7 --- /dev/null +++ b/mapoptiontest/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + let all_evens = [2u8, 4, 6, 8]; + let result: Result, _> = all_evens + .iter() + .map(|value| if value % 2 == 0 { Some(value) } else { None }) + .map(|opt| opt.ok_or(format!("Not good"))) + .collect(); + println!("All Evens: {:?}", result); + + let not_evens = [2, 2, 2, 2, 2, 1]; + let result: Result, _> = not_evens + .iter() + .map(|value| if value % 2 == 0 { Some(value) } else { None }) + .map(|opt| opt.ok_or(format!("Not good"))) + .collect(); + println!("Not Evens: {:?}", result); +}