diff --git a/capresulttest/Cargo.lock b/capresulttest/Cargo.lock new file mode 100644 index 0000000..f295b1d --- /dev/null +++ b/capresulttest/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "capresulttest" +version = "0.1.0" diff --git a/capresulttest/Cargo.toml b/capresulttest/Cargo.toml new file mode 100644 index 0000000..83620d7 --- /dev/null +++ b/capresulttest/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "capresulttest" +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/capresulttest/README.md b/capresulttest/README.md new file mode 100644 index 0000000..3717b74 --- /dev/null +++ b/capresulttest/README.md @@ -0,0 +1,5 @@ +# CapResultTest + +It is known that a `.collect()` in which the `Item` is a `Result` will stop +processing as soon as it finds an `Err`. But what if we capture this before +`.collect()`? diff --git a/capresulttest/src/main.rs b/capresulttest/src/main.rs new file mode 100644 index 0000000..0a965f9 --- /dev/null +++ b/capresulttest/src/main.rs @@ -0,0 +1,31 @@ +fn main() { + let evens = [2, 2, 2, 2, 2]; + let res = evens + .iter() + .map(|v| { + if v % 2 == 0 { + Ok(v.to_string()) + } else { + Err("No".to_string()) + } + }) + .collect::, String>>(); + println!("Evens: {:?}", res); + + let not_evens = [2, 2, 2, 2, 1, 2]; + let res = not_evens + .iter() + .map(|v| { + if v % 2 == 0 { + Ok(v.to_string()) + } else { + Err("No".to_string()) + } + }) + .map(|res| match res { + Ok(v) => v, + Err(e) => e, + }) + .collect::>(); + println!("Not evens: {:?}", res); +}