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); +}