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.
Options are most commonly used with pattern matching to query the presence of a value and take action, always accounting for the None
case.
let msg = Some(~"howdy");
// Take a reference to the contained string
match msg {
Some(ref m) => io::println(m),
None => ()
}
// Remove the contained string, destroying the Option
let unwrapped_msg = match msg {
Some(m) => m,
None => ~"default message"
};
Option
- The option typeof ::core::clone::Clone for Option<T> where <T: ::core::clone::Clone>
of ::core::cmp::Eq for Option<T> where <T: ::core::cmp::Eq>
of Ord for Option<T> where <T: Ord>
of Add<Option<T>, Option<T>> for Option<T> where <T: Copy + Add<T, T>>
of BaseIter<T> for Option<T> where <T>
of MutableIter<T> for Option<T> where <T>
for Option<T> where <T>
for Option<T> where <T: Copy>
for Option<T> where <T: Copy + Zero>
Option
The option type
None
Some(T)
::core::clone::Clone
for Option<T>
where <T: ::core::clone::Clone>
clone
fn clone(&self) -> Option<T>
::core::cmp::Eq
for Option<T>
where <T: ::core::cmp::Eq>
eq
fn eq(&self, __other: &Option<T>) -> bool
ne
fn ne(&self, __other: &Option<T>) -> bool
Ord
for Option<T>
where <T: Ord>
lt
fn lt(&self, other: &Option<T>) -> bool
le
fn le(&self, other: &Option<T>) -> bool
ge
fn ge(&self, other: &Option<T>) -> bool
gt
fn gt(&self, other: &Option<T>) -> bool
Add<Option<T>, Option<T>>
for Option<T>
where <T: Copy + Add<T, T>>
add
fn add(&self, other: &Option<T>) -> Option<T>
BaseIter<T>
for Option<T>
where <T>
each
fn each(&self, f: &fn(x: &'self T) -> bool)
Performs an operation on the contained value by reference
size_hint
fn size_hint(&self) -> Option<uint>
MutableIter<T>
for Option<T>
where <T>
each_mut
fn each_mut(&mut self, f: &fn(&'self mut T) -> bool)
Option<T>
where <T>
is_none
fn is_none(&const self) -> bool
Returns true if the option equals none
is_some
fn is_some(&const self) -> bool
Returns true if the option contains some value
chain
fn chain<U>(self, f: &fn(t: T) -> Option<U>) -> Option<U>
Update an optional value by optionally running its content through a function that returns an option.
or
fn or(self, optb: Option<T>) -> Option<T>
Returns the leftmost Some() value, or None if both are None.
chain_ref
fn chain_ref<U>(&self, f: &fn(x: &'self T) -> Option<U>) -> Option<U>
Update an optional value by optionally running its content by reference through a function that returns an option.
map
fn map<U>(&self, f: &fn(&'self T) -> U) -> Option<U>
Maps a some
value from one type to another by reference
map_consume
fn map_consume<U>(self, f: &fn(v: T) -> U) -> Option<U>
As map
, but consumes the option and gives f
ownership to avoid copying.
map_default
fn map_default<U>(&self, def: U, f: &fn(&'self T) -> U) -> U
Applies a function to the contained value or returns a default
map_consume_default
fn map_consume_default<U>(self, def: U, f: &fn(v: T) -> U) -> U
As map_default
, but consumes the option and gives f
ownership to avoid copying.
mutate
fn mutate(&mut self, f: &fn(T) -> T)
Apply a function to the contained value or do nothing
mutate_default
fn mutate_default(&mut self, def: T, f: &fn(T) -> T)
Apply a function to the contained value or set it to a default
get_ref
fn get_ref(&self) -> &'self T
Gets an immutable reference to the value inside an option.
Fails if the value equals None
In general, because this function may fail, its use is discouraged (calling get
on None
is akin to dereferencing a null pointer). Instead, prefer to use pattern matching and handle the None
case explicitly.
get_mut_ref
fn get_mut_ref(&mut self) -> &'self mut T
Gets a mutable reference to the value inside an option.
Fails if the value equals None
In general, because this function may fail, its use is discouraged (calling get
on None
is akin to dereferencing a null pointer). Instead, prefer to use pattern matching and handle the None
case explicitly.
unwrap
fn unwrap(self) -> 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.
Fails if the value equals None
.
In general, because this function may fail, its use is discouraged. Instead, prefer to use pattern matching and handle the None
case explicitly.
swap_unwrap
fn swap_unwrap(&mut self) -> T
The option dance. Moves a value out of an option type and returns it, replacing the original with None
.
Fails if the value equals None
.
expect
fn expect(self, reason: &str) -> T
Gets the value out of an option, printing a specified message on failure
Fails if the value equals none
Option<T>
where <T: Copy>
get
fn get(self) -> T
Gets the value out of an option
Fails if the value equals None
In general, because this function may fail, its use is discouraged (calling get
on None
is akin to dereferencing a null pointer). Instead, prefer to use pattern matching and handle the None
case explicitly.
get_or_default
fn get_or_default(self, def: T) -> T
Returns the contained value or a default
while_some
fn while_some(self, blk: &fn(v: T) -> Option<T>)
Applies a function zero or more times until the result is none.
Option<T>
where <T: Copy + Zero>
get_or_zero
fn get_or_zero(self) -> T
Returns the contained value or zero (for this type)