Julio Biason
2 years ago
4 changed files with 51 additions and 0 deletions
@ -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" |
@ -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] |
@ -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()`? |
@ -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…
Reference in new issue