Julio Biason
2 years ago
4 changed files with 36 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 = "mapoptiontest" |
||||||
|
version = "0.1.0" |
@ -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] |
@ -0,0 +1,4 @@ |
|||||||
|
# MapOptionTest |
||||||
|
|
||||||
|
Test how one can stop a collect when a map produces an option and the option is |
||||||
|
none. |
@ -0,0 +1,17 @@ |
|||||||
|
fn main() { |
||||||
|
let all_evens = [2u8, 4, 6, 8]; |
||||||
|
let result: Result<Vec<_>, _> = 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<Vec<_>, _> = 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); |
||||||
|
} |
Loading…
Reference in new issue