Browse Source

Testing early stop with options

master
Julio Biason 2 years ago
parent
commit
c2426e34f7
  1. 7
      mapoptiontest/Cargo.lock
  2. 8
      mapoptiontest/Cargo.toml
  3. 4
      mapoptiontest/README.md
  4. 17
      mapoptiontest/src/main.rs

7
mapoptiontest/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 = "mapoptiontest"
version = "0.1.0"

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

4
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.

17
mapoptiontest/src/main.rs

@ -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…
Cancel
Save