misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/std/result.md "Suggest an edit")

#  ` Result `

We've seen that the ` Option ` enum can be used as a return value from
functions that may fail, where ` None ` can be returned to indicate failure.
However, sometimes it is important to express _why_ an operation failed. To do
this we have the ` Result ` enum.

The ` Result<T, E> ` enum has two variants:

  * ` Ok(value) ` which indicates that the operation succeeded, and wraps the ` value ` returned by the operation. ( ` value ` has type ` T ` ) 
  * ` Err(why) ` , which indicates that the operation failed, and wraps ` why ` , which (hopefully) explains the cause of the failure. ( ` why ` has type ` E ` ) 

    
    
    mod checked {
        // Mathematical "errors" we want to catch
        #[derive(Debug)]
        pub enum MathError {
            DivisionByZero,
            NonPositiveLogarithm,
            NegativeSquareRoot,
        }
    
        pub type MathResult = Result<f64, MathError>;
    
        pub fn div(x: f64, y: f64) -> MathResult {
            if y == 0.0 {
                // This operation would `fail`, instead let's return the reason of
                // the failure wrapped in `Err`
                Err(MathError::DivisionByZero)
            } else {
                // This operation is valid, return the result wrapped in `Ok`
                Ok(x / y)
            }
        }
    
        pub fn sqrt(x: f64) -> MathResult {
            if x < 0.0 {
                Err(MathError::NegativeSquareRoot)
            } else {
                Ok(x.sqrt())
            }
        }
    
        pub fn ln(x: f64) -> MathResult {
            if x <= 0.0 {
                Err(MathError::NonPositiveLogarithm)
            } else {
                Ok(x.ln())
            }
        }
    }
    
    // `op(x, y)` === `sqrt(ln(x / y))`
    fn op(x: f64, y: f64) -> f64 {
        // This is a three level match pyramid!
        match checked::div(x, y) {
            Err(why) => panic!("{:?}", why),
            Ok(ratio) => match checked::ln(ratio) {
                Err(why) => panic!("{:?}", why),
                Ok(ln) => match checked::sqrt(ln) {
                    Err(why) => panic!("{:?}", why),
                    Ok(sqrt) => sqrt,
                },
            },
        }
    }
    
    fn main() {
        // Will this fail?
        println!("{}", op(1.0, 10.0));
    }
    

[ __ ](../std/option.html "Previous chapter") [ __
](../std/result/question_mark.html "Next chapter")

[ __ ](../std/option.html "Previous chapter") [ __
](../std/result/question_mark.html "Next chapter")