#chaining #combinator #option #result

intoif

A small library for construction of an Option and Result from any type based on some user-specified condition

1 stable release

1.0.0 Nov 17, 2019

#6 in #chaining

Download history 50/week @ 2021-10-04 21/week @ 2021-10-11 37/week @ 2021-10-18 23/week @ 2021-10-25 30/week @ 2021-11-01 95/week @ 2021-11-08 41/week @ 2021-11-15 91/week @ 2021-11-22 49/week @ 2021-11-29 26/week @ 2021-12-06 33/week @ 2021-12-13 103/week @ 2021-12-20 12/week @ 2021-12-27 34/week @ 2022-01-03 10/week @ 2022-01-10 13/week @ 2022-01-17

69 downloads per month

MIT license

5KB
59 lines

intoif

This library provides two convenient traits that allow you to convert values into Option or Result based on the provided predicate. It is somewhat similar to the boolinator crate, except you don't need to create a boolean - the predicate will do it for you. This can be useful e.g. when writing a long chain that ends with a fold which you want to convert into Option or Result.

use intoif::IntoOption;
use boolinator::Boolinator;

fn fizz_buzz(n: u32) -> String {
    [(3, "Fizz"), (5, "Buzz")]
        .iter()
        .filter_map(|(x, s)| (n % x == 0).as_some(*s))
        .collect::<String>()
        .none_if(String::is_empty)
        .unwrap_or_else(|| n.to_string())
}

lib.rs:

This library provides two convenient traits that allow you to convert values into Option or Result based on the provided predicate. It is somewhat similar to the boolinator crate, except you don't need to create a boolean - the predicate will do it for you. This can be useful e.g. when writing a long chain that ends with a fold which you want to convert into Option or Result.

use intoif::IntoOption;

fn fizz_buzz(n: u32) -> String {
    [(3, "Fizz"), (5, "Buzz")]
        .iter()
        .filter_map(|(x, s)| if n % x == 0 { Some(*s) } else { None })
        .collect::<String>()
        .none_if(String::is_empty)
        .unwrap_or_else(|| n.to_string())
}

No runtime deps