[][src]Crate truthy

Mimic the "truthy" behavior of languages like JavaScript and Python.

In other words,

// rust
my_value.truthy();

Should behave similarly to

// javascript
!!myValue;
Boolean(myValue);

or

# python
bool(my_value)

Behavior

let truthy_option = Some(1u32);
let falsy_options = [None, Some(0u32)];

assert!(truthy_option.truthy());
for falsy_option in &falsy_options {
    assert!(falsy_option.falsy());
}

let truthy_result: Result<_, ()> = Ok(1u32);
let falsy_results = [Ok(false), Err(false), Err(true)];

assert!(truthy_result.truthy());
for falsy_result in &falsy_results {
    assert!(falsy_result.falsy());
}

let not_empty = vec![()];
let empty: [();0] = [];

assert!(not_empty.truthy());
assert!(empty.falsy());

Example Usage

use truthy::Truthy;

fn do_something<T: Truthy>(value: T) {
    if value.truthy() {
        println!("It's truthy :)");
    }
}

Traits

Truthy

Convert to a bool.