result-ext

Crates.ioresult-ext
lib.rsresult-ext
version0.2.0
sourcesrc
created_at2022-01-17 16:24:11.15124
updated_at2023-01-11 19:53:59.924001
descriptionExtends `Result` with additional operations
homepagehttps://github.com/soc/result-ext
repositoryhttps://github.com/soc/result-ext.git
max_upload_size
id515665
size24,108
(soc)

documentation

https://docs.rs/result-ext/

README

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

result-ext

Introduction

This crate extends Result with additional methods, currently:

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

Its sister crate is option-ext, which extends Option.

Requirements

Rust 1.0 or newer.

Usage

Dependency

Add the library as a dependency to your project by inserting

result-ext = "0.2.0"

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

Example

use result_ext::ResultExt;

fn example_contains() {
    use result_ext::ResultExt;

    let x: Result<u32, &str> = Ok(2);
    assert_eq!(x.contains(&2), true);

    let x: Result<u32, &str> = Ok(3);
    assert_eq!(x.contains(&2), false);

    let x: Result<u32, &str> = Err("Some error message");
    assert_eq!(x.contains(&2), false);
}

fn example_contains_err() {
    let x: Result<u32, &str> = Ok(2);
    assert_eq!(x.contains_err(&"Some error message"), false);
    
    let x: Result<u32, &str> = Err("Some error message");
    assert_eq!(x.contains_err(&"Some error message"), true);
    
    let x: Result<u32, &str> = Err("Some other error message");
    assert_eq!(x.contains_err(&"Some error message"), false);
}

fn example_map_or2() {
    let x: Result<_, &str> = Ok("foo");
    assert_eq!(x.map_or2(|v| v.len(), 23), 3);
    
    let x: Result<&str, _> = Err("bar");
    assert_eq!(x.map_or2(|v| v.len(), 23), 23);
}

fn example_map_or_else2() {
    let k = 23;
    
    let x : Result<_, &str> = Ok("foo");
    assert_eq!(x.map_or_else2(|v| v.len(), |e| k * 2), 3);
    
    let x : Result<&str, _> = Err("bar");
    assert_eq!(x.map_or_else2(|v| v.len(), |e| k * 2), 46);  
}
Commit count: 4

cargo fmt