[src]

Enum std::option::Option

pub enum Option<T> {
    None,
    Some(T),
}

The Option

Variants

None

No value

Some

Some value T

Methods

impl<T> Option<T>

fn is_some(&self) -> bool

Returns true if the option is a Some value

fn is_none(&self) -> bool

Returns true if the option is a None value

fn as_ref<'r>(&'r self) -> Option<&'r T>

Convert from Option<T> to Option<&T>

Example

Convert an Option<~str> into an Option<int>, preserving the original. The map method takes the self argument by value, consuming the original, so this technique uses as_ref to first take an Option to a reference to the value inside the original.

let num_as_str: Option<~str> = Some(~"10");
// First, cast `Option<~str>` to `Option<&~str>` with `as_ref`,
// then consume *that* with `map`, leaving `num_as_str` on the stack.
let num_as_int: Option<uint> = num_as_str.as_ref().map(|n| n.len());
println!("still can print num_as_str: {}", num_as_str);

fn as_mut<'r>(&'r mut self) -> Option<&'r mut T>

Convert from Option<T> to Option<&mut T>

fn as_slice<'r>(&'r self) -> &'r [T]

Convert from Option<T> to &[T] (without copying)

fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T]

Convert from Option<T> to &mut [T] (without copying)

fn expect<M: Any + Send>(self, msg: M) -> T

Unwraps an option, yielding the content of a Some

Failure

Fails if the value is a None with a custom failure message provided by msg.

fn unwrap(self) -> T

Moves a value out of an option type and returns it, consuming the Option.

Failure

Fails if the self value equals None.

Safety note

In general, because this function may fail, its use is discouraged. Instead, prefer to use pattern matching and handle the None case explicitly.

fn unwrap_or(self, def: T) -> T

Returns the contained value or a default.

fn unwrap_or_else(self, f: || -> T) -> T

Returns the contained value or computes it from a closure.

fn map<U>(self, f: |T| -> U) -> Option<U>

Maps an Option<T> to Option<U> by applying a function to a contained value

Example

Convert an Option<~str> into an Option<uint>, consuming the original:

let num_as_str: Option<~str> = Some(~"10");
// `Option::map` takes self *by value*, consuming `num_as_str`
let num_as_int: Option<uint> = num_as_str.map(|n| n.len());

fn map_or<U>(self, def: U, f: |T| -> U) -> U

Applies a function to the contained value or returns a default.

fn mutate(&mut self, f: |T| -> T) -> bool

Applies a function to the contained value or does nothing. Returns true if the contained value was mutated.

fn mutate_or_set(&mut self, def: T, f: |T| -> T) -> bool

Applies a function to the contained value or sets it to a default. Returns true if the contained value was mutated, or false if set to the default.

fn iter<'r>(&'r self) -> Item<&'r T>

Returns an iterator over the possibly contained value.

fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T>

Returns a mutable iterator over the possibly contained value.

fn move_iter(self) -> Item<T>

Returns a consuming iterator over the possibly contained value.

fn and<U>(self, optb: Option<U>) -> Option<U>

Returns None if the option is None, otherwise returns optb.

fn and_then<U>(self, f: |T| -> Option<U>) -> Option<U>

Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.

fn or(self, optb: Option<T>) -> Option<T>

Returns the option if it contains a value, otherwise returns optb.

fn or_else(self, f: || -> Option<T>) -> Option<T>

Returns the option if it contains a value, otherwise calls f and returns the result.

fn take(&mut self) -> Option<T>

Takes the value out of the option, leaving a None in its place.

fn filtered(self, f: |t: &T| -> bool) -> Option<T>

Filters an optional value using a given function.

fn while_some(self, f: |v: T| -> Option<T>)

Applies a function zero or more times until the result is None.

fn take_unwrap(&mut self) -> T

The option dance. Moves a value out of an option type and returns it, replacing the original with None.

Failure

Fails if the value equals None.

fn get_ref<'a>(&'a self) -> &'a T

Gets an immutable reference to the value inside an option.

Failure

Fails if the value equals None

Safety note

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.

fn get_mut_ref<'a>(&'a mut self) -> &'a mut T

Gets a mutable reference to the value inside an option.

Failure

Fails if the value equals None

Safety note

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.

impl<T: Default> Option<T>

fn unwrap_or_default(self) -> T

Returns the contained value or a default

Consumes the self argument then, if Some, returns the contained value, otherwise if None, returns the default value for that type.

Example

Convert a string to an integer, turning poorly-formed strings into 0 (the default value for integers). from_str converts a string to any other type that implements FromStr, returning None on error.

let good_year_from_input = "1909";
let bad_year_from_input = "190blarg";
let good_year = from_str(good_year_from_input).unwrap_or_default();
let bad_year = from_str(bad_year_from_input).unwrap_or_default();

assert_eq!(1909, good_year);
assert_eq!(0, bad_year);

Trait Implementations

impl<S: Writer, T: Hash<S>> Hash<S> for Option<T>

fn hash(&self, state: &mut S)

Compute a hash of the value.

impl<T> Default for Option<T>

fn default() -> Option<T>

Return the "default value" for a type.

Derived Implementations

impl<T: Show> Show for Option<T>

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl<T: TotalOrd> TotalOrd for Option<T>

fn cmp(&self, __arg_0: &Option<T>) -> Ordering

impl<T: TotalEq> TotalEq for Option<T>

fn assert_receiver_is_total_eq(&self)

impl<T: Ord> Ord for Option<T>

fn lt(&self, __arg_0: &Option<T>) -> bool

fn le(&self, __arg_0: &Option<T>) -> bool

fn gt(&self, __arg_0: &Option<T>) -> bool

fn ge(&self, __arg_0: &Option<T>) -> bool

impl<T: Eq> Eq for Option<T>

fn eq(&self, __arg_0: &Option<T>) -> bool

fn ne(&self, __arg_0: &Option<T>) -> bool

impl<T: Clone> Clone for Option<T>

fn clone(&self) -> Option<T>

Returns a copy of the value. The contents of owned pointers are copied to maintain uniqueness, while the contents of managed pointers are not copied.

fn clone_from(&mut self, source: &Self)

Perform copy-assignment from source.

a.clone_from(&b) is equivalent to a = b.clone() in functionality, but can be overridden to reuse the resources of a to avoid unnecessary allocations.