Operations on the ubiquitous option type.

Type option represents an optional value.

Every option<T> value can either be some(T) or none. Where in other languages you might use a nullable type, in Rust you would use an option type.

Enum option

The option type

Variants

Implementation extensions for option<T>

Method chain

fn chain<U>(f: fn(T) -> option<U>) -> option<U>

Update an optional value by optionally running its content through a function that returns an option.

Method map_default

fn map_default<U: copy>(def: U, f: fn(T) -> U) -> U

Applies a function to the contained value or returns a default

Method iter

fn iter(f: fn(T))

Performs an operation on the contained value or does nothing

Method is_none

fn is_none() -> bool

Returns true if the option equals none

Method is_some

fn is_some() -> bool

Returns true if the option contains some value

Method map

fn map<U: copy>(f: fn(T) -> U) -> option<U>

Maps a some value from one type to another

Implementation extensions for option<T>

Method get

fn get() -> T

Gets the value out of an option

Failure

Fails if the value equals none

Method get_default

fn get_default(def: T) -> T

Method expect

pure fn expect(reason: str) -> T

Gets the value out of an option, printing a specified message on failure

Failure

Fails if the value equals none

Function chain

pure fn chain<T, U>(opt: option<T>, f: fn(T) -> option<U>) -> option<U>

Update an optional value by optionally running its content through a function that returns an option.

Function expect

pure fn expect<T: copy>(opt: option<T>, reason: str) -> T

Gets the value out of an option, printing a specified message on failure

Failure

Fails if the value equals none

Function get

pure fn get<T: copy>(opt: option<T>) -> T

Gets the value out of an option

Failure

Fails if the value equals none

Function get_default

pure fn get_default<T: copy>(opt: option<T>, def: T) -> T

Returns the contained value or a default

Function is_none

pure fn is_none<T>(opt: option<T>) -> bool

Returns true if the option equals none

Function is_some

pure fn is_some<T>(opt: option<T>) -> bool

Returns true if the option contains some value

Function iter

pure fn iter<T>(opt: option<T>, f: fn(T))

Performs an operation on the contained value or does nothing

Function map

pure fn map<T, U: copy>(opt: option<T>, f: fn(T) -> U) -> option<U>

Maps a some value from one type to another

Function map_default

pure fn map_default<T, U: copy>(opt: option<T>, def: U, f: fn(T) -> U) -> U

Applies a function to the contained value or returns a default

Function unwrap

pure fn unwrap<T>(-opt: option<T>) -> T

Moves a value out of an option type and returns it.

Useful primarily for getting strings, vectors and unique pointers out of option types without copying them.