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/option.md "Suggest an edit")

#  ` Option `

Sometimes it's desirable to catch the failure of some parts of a program
instead of calling ` panic! ` ; this can be accomplished using the ` Option `
enum.

The ` Option<T> ` enum has two variants:

  * ` None ` , to indicate failure or lack of value, and 
  * ` Some(value) ` , a tuple struct that wraps a ` value ` with type ` T ` . 

    
    
    // An integer division that doesn't `panic!`
    fn checked_division(dividend: i32, divisor: i32) -> Option<i32> {
        if divisor == 0 {
            // Failure is represented as the `None` variant
            None
        } else {
            // Result is wrapped in a `Some` variant
            Some(dividend / divisor)
        }
    }
    
    // This function handles a division that may not succeed
    fn try_division(dividend: i32, divisor: i32) {
        // `Option` values can be pattern matched, just like other enums
        match checked_division(dividend, divisor) {
            None => println!("{} / {} failed!", dividend, divisor),
            Some(quotient) => {
                println!("{} / {} = {}", dividend, divisor, quotient)
            },
        }
    }
    
    fn main() {
        try_division(4, 2);
        try_division(1, 0);
    
        // Binding `None` to a variable needs to be type annotated
        let none: Option<i32> = None;
        let _equivalent_none = None::<i32>;
    
        let optional_float = Some(0f32);
    
        // Unwrapping a `Some` variant will extract the value wrapped.
        println!("{:?} unwraps to {:?}", optional_float, optional_float.unwrap());
    
        // Unwrapping a `None` variant will `panic!`
        println!("{:?} unwraps to {:?}", none, none.unwrap());
    }
    

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

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