Browse Source

Collect test

master
Julio Biason 2 years ago
parent
commit
cc77cb31d4
  1. 7
      dontcollectresulttest/Cargo.lock
  2. 8
      dontcollectresulttest/Cargo.toml
  3. 4
      dontcollectresulttest/README.md
  4. 25
      dontcollectresulttest/src/main.rs

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

8
dontcollectresulttest/Cargo.toml

@ -0,0 +1,8 @@
[package]
name = "dontcollectresulttest"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

4
dontcollectresulttest/README.md

@ -0,0 +1,4 @@
# DontCollectResultTest
Checking if one can change the way `.collect()` collects stuff by changing the
expected resulting type.

25
dontcollectresulttest/src/main.rs

@ -0,0 +1,25 @@
fn is_even(val: u8) -> Result<u8, u8> {
if val % 2 == 0 {
Ok(val)
} else {
Err(val)
}
}
fn main() {
let values = [2u8, 2, 2, 2, 2, 1];
println!(
"Result<Vec>: {:?}",
values
.iter()
.map(|x| is_even(*x))
.collect::<Result<Vec<u8>, u8>>()
);
print!(
"Vec<Result>: {:?}",
values
.iter()
.map(|x| is_even(*x))
.collect::<Vec<Result<u8, u8>>>()
);
}
Loading…
Cancel
Save