Browse Source

Testing capturing resutls

master
Julio Biason 1 year ago
parent
commit
0c0c47574e
  1. 7
      capresulttest/Cargo.lock
  2. 8
      capresulttest/Cargo.toml
  3. 5
      capresulttest/README.md
  4. 31
      capresulttest/src/main.rs

7
capresulttest/Cargo.lock generated

@ -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"

8
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]

5
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()`?

31
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::<Result<Vec<String>, 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::<Vec<String>>();
println!("Not evens: {:?}", res);
}
Loading…
Cancel
Save