1
0
Fork
You've already forked result-ext
0
extends Option with additional operations
  • Rust 100%
2023年04月04日 14:50:37 +02:00
src release 0.2.0, adds map_or2 and map_or_else2 2023年01月11日 21:01:17 +01:00
.gitignore initial commit 2022年01月17日 17:25:57 +01:00
Cargo.lock release 0.2.0, adds map_or2 and map_or_else2 2023年01月11日 21:01:17 +01:00
Cargo.toml release 0.2.0, adds map_or2 and map_or_else2 2023年01月11日 21:01:17 +01:00
LICENSE.txt initial commit 2022年01月17日 17:25:57 +01:00
README.md use more modern badge style 2023年04月04日 14:50:37 +02:00

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

useresult_ext::ResultExt;fn example_contains(){useresult_ext::ResultExt;letx: Result<u32,&str>=Ok(2);assert_eq!(x.contains(&2),true);letx: Result<u32,&str>=Ok(3);assert_eq!(x.contains(&2),false);letx: Result<u32,&str>=Err("Some error message");assert_eq!(x.contains(&2),false);}fn example_contains_err(){letx: Result<u32,&str>=Ok(2);assert_eq!(x.contains_err(&"Some error message"),false);letx: Result<u32,&str>=Err("Some error message");assert_eq!(x.contains_err(&"Some error message"),true);letx: Result<u32,&str>=Err("Some other error message");assert_eq!(x.contains_err(&"Some error message"),false);}fn example_map_or2(){letx: Result<_,&str>=Ok("foo");assert_eq!(x.map_or2(|v|v.len(),23),3);letx: Result<&str,_>=Err("bar");assert_eq!(x.map_or2(|v|v.len(),23),23);}fn example_map_or_else2(){letk=23;letx: Result<_,&str>=Ok("foo");assert_eq!(x.map_or_else2(|v|v.len(),|e|k*2),3);letx: Result<&str,_>=Err("bar");assert_eq!(x.map_or_else2(|v|v.len(),|e|k*2),46);}