take-if 1.0.0

A tiny utility for conditionally taking the contents of an option.
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented1 out of 3 items with examples
  • Size
  • Source code size: 5.39 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.06 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • randomPoison/take-if
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • randomPoison

take-if

A tiny utility for conditionally taking the contents from an Option. See also Option::take.

use take_if::TakeIf;

let mut maybe_greeting = Some("Hello, World!");

if let Some(greeting) = maybe_greeting.take_if(|greeting| greeting.starts_with("Hello")) {
    println!(r#"Greeting {:?} starts with "Hello""#, greeting);
} else {
    println!(r#"There was no greeting, or it didn't start with "Hello""#);
}

Usage

Add take-if to your Cargo.toml:

[dependencies]
take-if = "1.0.0"

Import the TakeIf trait in your modules to add the take_if method to Option:

use take_if::TakeIf;

let taken = maybe_value.take_if(|value| value.id == 5);