intoif 1.0.0

A small library for construction of an Option and Result from any type based on some user-specified condition.
Documentation
  • Coverage
  • 100%
    9 out of 9 items documented1 out of 9 items with examples
  • Size
  • Source code size: 7.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 643.58 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • vpraid

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