take-if

Crates.iotake-if
lib.rstake-if
version1.0.0
sourcesrc
created_at2020-05-02 21:51:37.642879
updated_at2020-05-02 21:58:29.841869
descriptionA tiny utility for conditionally taking the contents of an option.
homepage
repositoryhttps://github.com/randomPoison/take-if
max_upload_size
id236811
size5,765
David LeGare (randomPoison)

documentation

README

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);
Commit count: 8

cargo fmt