outcome

A Success/Failure type for rust

7 releases

Uses old Rust 2015

0.1.7 Oct 22, 2017
0.1.6 Oct 20, 2017

#6 in #and-then

Download history 6/week @ 2024-02-14 26/week @ 2024-02-21 48/week @ 2024-02-28

80 downloads per month
Used in bitboard_xo

MIT license

8KB
64 lines

Build Status Latest Version

The outcome crate

Type Outcome represents a success or failure: Every Outcome is either Success or Failure

use outcome::*;
 
fn do_something() -> Outcome {
    Success
}
 
// The return value is an outcome
let result = do_something();
 
// Pattern Match
match result {
    Success => println!("Well done!"),
    Failure => println!("Oh well :("),
}

Examples

Using and_then on an Outcome:

/use outcome::*;
 
// Returns `Failure`
let result = Outcome::from_bool(false);
 
match result.and_then(|| Success) {
    Success => println!("Success! :)"),
    Failure => println!("Failure :("),
}

Using or_none on an Outcome to transform it into an Option:

use outcome::*;
 
let result = Success;
 
// Encapsulates arg within an option
match result.or_none("hello!") {
    Some(s) => println!("{}", s),
    None => println!("Nothing here!"),
}

No runtime deps