2 unstable releases

Uses old Rust 2015

0.2.0 Jan 11, 2023
0.1.0 Jan 17, 2022

#641 in Rust patterns

Download history 308064/week @ 2024-03-12 319146/week @ 2024-03-19 291832/week @ 2024-03-26 317988/week @ 2024-04-02 319428/week @ 2024-04-09 323043/week @ 2024-04-16 317958/week @ 2024-04-23 308248/week @ 2024-04-30 312478/week @ 2024-05-07 330535/week @ 2024-05-14 338735/week @ 2024-05-21 344339/week @ 2024-05-28 341535/week @ 2024-06-04 350213/week @ 2024-06-11 346399/week @ 2024-06-18 292734/week @ 2024-06-25

1,397,401 downloads per month
Used in 2,477 crates (via dirs-sys)

MPL-2.0 license

8KB

crates.io API documentation actively developed License: MPL-2.0

option-ext

Introduction

This crate extends Option with additional methods, currently:

  • contains
  • map_or2 (as a replacement for map_or)
  • map_or_else2 (as a replacement for map_or_else)

Its sister crate is result-ext, which extends Result.

Requirements

Rust 1.0 or newer.

Usage

Dependency

Add the library as a dependency to your project by inserting

option-ext = "0.2.0"

into the [dependencies] section of your Cargo.toml file.

Example

use option_ext::OptionExt;

fn example_contains() {
    let x: Option<u32> = Some(2);
    assert_eq!(x.contains(&2), true);

    let x: Option<u32> = Some(3);
    assert_eq!(x.contains(&2), false);

    let x: Option<u32> = None;
    assert_eq!(x.contains(&2), false);
}

fn example_map_or2() {
    let x = Some("bar");
    assert_eq!(x.map_or2(|v| v.len(), 42), 3);

    let x: Option<&str> = None;
    assert_eq!(x.map_or2(|v| v.len(), 42), 42);
}

fn example_map_or_else2() {
    let k = 23;
    
    let x = Some("bar");
    assert_eq!(x.map_or_else2(|v| v.len(), || 2 * k), 3);
    
    let x: Option<&str> = None;
    assert_eq!(x.map_or_else2(|v| v.len(), || 2 * k), 46);
}

No runtime deps