Crate intoif

Source
Expand description

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())
}

Traits§

IntoOption
Allow construction of Option from any type using predicate to choose between Some and None.
IntoResult
Allow construction of Result from any type using predicate to choose between Ok and Err.