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.
option
- The option typeextensions for option<T>
extensions for option<T>
chain
- Update an optional value by optionally running its content through a function that returns an option.expect
- Gets the value out of an option, printing a specified message on failureget
- Gets the value out of an optionget_default
- Returns the contained value or a defaultis_none
- Returns true if the option equals none
is_some
- Returns true if the option contains some valueiter
- Performs an operation on the contained value or does nothingmap
- Maps a some
value from one type to anothermap_default
- Applies a function to the contained value or returns a defaultunwrap
- Moves a value out of an option type and returns it.option
The option type
none
some(T)
extensions for option<T>
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.
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
iter
fn iter(f: fn(T))
Performs an operation on the contained value or does nothing
is_none
fn is_none() -> bool
Returns true if the option equals none
is_some
fn is_some() -> bool
Returns true if the option contains some value
map
fn map<U: copy>(f: fn(T) -> U) -> option<U>
Maps a some
value from one type to another
extensions for option<T>
get
fn get() -> T
Gets the value out of an option
Fails if the value equals none
get_default
fn get_default(def: T) -> T
expect
pure fn expect(reason: str) -> T
Gets the value out of an option, printing a specified message on failure
Fails if the value equals none
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.
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
Fails if the value equals none
get
pure fn get<T: copy>(opt: option<T>) -> T
Gets the value out of an option
Fails if the value equals none
get_default
pure fn get_default<T: copy>(opt: option<T>, def: T) -> T
Returns the contained value or a default
is_none
pure fn is_none<T>(opt: option<T>) -> bool
Returns true if the option equals none
is_some
pure fn is_some<T>(opt: option<T>) -> bool
Returns true if the option contains some value
iter
pure fn iter<T>(opt: option<T>, f: fn(T))
Performs an operation on the contained value or does nothing
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
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
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.