Crates.io | eitherable |
lib.rs | eitherable |
version | 0.1.0 |
source | src |
created_at | 2023-09-30 11:57:30.3943 |
updated_at | 2023-09-30 11:57:30.3943 |
description | Extension trait to create the `either` type from booleans. (and maybe other types) |
homepage | |
repository | https://github.com/asibahi/eitherable |
max_upload_size | |
id | 988587 |
size | 4,327 |
eitherable
This crate extends bool
with a simple way to create the Either
type, from
the either
crate.
It is a simple convenience trait and method, and also a way for me to learn about how crates.io and cargo work.
either
crateThe either
crate emphasies that the two sides of Either
, Left
nd Right
,
have no element of truthiness or falsiness. However, the trait here immediately
assigns Left
to be the truthy value. So this is not quite suitable for the
either
crate.
The reason I chose Left
to be the truthy value is simple: it retains the dame
order as the if
statement. The two following statements are equivalent.
let fst_example = if my_cond { Either::Left(left) } else { Either::Right(right) };
let snd_example = my_cond.either(left, right);
If you think Right
should be the truthy value, you can .flip()
.
Add the library as a dependency to your project by inserting
eitherable = "0.1.0"
into the [dependencies]
section of your Cargo.toml file.
use eitherable::*;
let x = true;
assert_eq!(x.either(1, "right"), Either::Left(1));
let x = false;
assert_eq!(x.either(1, "right"), Either::Right("right"));
let x = true;
assert_eq!(x.either_else(|| 1,|| "right"), Either::Left(1));
let x = false;
assert_eq!(x.either_else(|| 1,|| "right"), Either::Right("right"));