pub enum Option<T> { None, Some(T), }
The option type
impl<T> Option<T>
fn iter<'r>(&'r self) -> OptionIterator<&'r T>
Return an iterator over the possibly contained value
fn mut_iter<'r>(&'r mut self) -> OptionIterator<&'r mut T>
Return a mutable iterator over the possibly contained value
fn move_iter(self) -> OptionIterator<T>
Return a consuming iterator over the possibly contained value
fn is_none(&self) -> bool
Returns true if the option equals None
fn is_some(&self) -> bool
Returns true if the option contains a Some
value
fn and(self, optb: Option<T>) -> Option<T>
Returns None
if the option is None
, otherwise returns optb
.
fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U>
Returns None
if the option is None
, otherwise calls f
with the
wrapped value and returns the result.
fn and_then_ref<'a, U>(&'a self, f: &fn(&'a T) -> Option<U>) -> Option<U>
Returns None
if the option is None
, otherwise calls f
with a
reference to the wrapped value and returns the result.
fn and_then_mut_ref<'a, U>(&'a mut self, f: &fn(&'a mut T) -> Option<U>) -> Option<U>
Returns None
if the option is None
, otherwise calls f
with a
mutable reference to 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: &fn() -> Option<T>) -> Option<T>
Returns the option if it contains a value, otherwise calls f
and
returns the result.
fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T>
Filters an optional value using given function.
fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U>
Maps a Some
value from one type to another by reference
fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U>
Maps a Some
value from one type to another by a mutable reference
fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U
Applies a function to the contained value or returns a default
fn map_mut_default<'a, U>(&'a mut self, def: U, f: &fn(&'a mut T) -> U) -> U
Maps a Some
value from one type to another by a mutable reference,
or returns a default value.
fn map_move<U>(self, f: &fn(T) -> U) -> Option<U>
As map
, but consumes the option and gives f
ownership to avoid
copying.
fn map_move_default<U>(self, def: U, f: &fn(T) -> U) -> U
As map_default
, but consumes the option and gives f
ownership to avoid copying.
fn take(&mut self) -> Option<T>
Take the value out of the option, leaving a None
in its place.
fn mutate(&mut self, f: &fn(T) -> T) -> bool
Apply a function to the contained value or do nothing. Returns true if the contained value was mutated.
fn mutate_default(&mut self, def: T, f: &fn(T) -> T) -> bool
Apply a function to the contained value or set it to a default. Returns true if the contained value was mutated, or false if set to the default.
fn get_ref<'a>(&'a self) -> &'a 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.
fn get_mut_ref<'a>(&'a mut self) -> &'a 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.
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.
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
.
Fails if the value equals None
.
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
fn unwrap_or(self, def: T) -> T
Returns the contained value or a default
fn unwrap_or_else(self, f: &fn() -> T) -> T
Returns the contained value or computes it from a closure
fn while_some(self, blk: &fn(v: T) -> Option<T>)
Applies a function zero or more times until the result is None
.
impl<T: Default> Option<T>
fn unwrap_or_default(self) -> T
Returns the contained value or default (for this type)
impl<T: Zero> Option<T>
fn unwrap_or_zero(self) -> T
Returns the contained value or zero (for this type)
impl<A: IterBytes> IterBytes for Option<A>
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool
Call the provided callback f
one or more times with
byte-slices that should be used when computing a hash
value or otherwise "flattening" the structure into
a sequence of bytes. The lsb0
parameter conveys
whether the caller is asking for little-endian bytes
(true
) or big-endian (false
); this should only be
relevant in implementations that represent a single
multi-byte datum such as a 32 bit integer or 64 bit
floating-point value. It can be safely ignored for
larger structured types as they are usually processed
left-to-right in declaration order, regardless of
underlying memory endianness.
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.
impl<T: DeepClone> DeepClone for Option<T>
fn deep_clone(&self) -> Option<T>
Return a deep copy of the value. Unlike Clone
, the contents of shared pointer types
are copied.
impl<T: Eq> Eq for Option<T>
impl<T: Eq + Ord> Ord for Option<T>
fn lt(&self, other: &Option<T>) -> bool
fn le(&self, other: &Option<T>) -> bool
fn ge(&self, other: &Option<T>) -> bool
fn gt(&self, other: &Option<T>) -> bool
impl<T: ToStr> ToStr for Option<T>
fn to_str(&self) -> ~str
Converts the value of self
to an owned string
impl<T: Clone> ToOption<T> for Option<T>
fn to_option(&self) -> Option<T>
Convert to the option
type
impl<T> IntoOption<T> for Option<T>
fn into_option(self) -> Option<T>
Convert to the option
type
impl<T> AsOption<T> for Option<T>
fn as_option<'a>(&'a self) -> Option<&'a T>
Convert to the option
type
impl<T: Clone> ToResult<T, ()> for Option<T>
fn to_result(&self) -> Result<T, ()>
Convert to the result
type
impl<T> IntoResult<T, ()> for Option<T>
fn into_result(self) -> Result<T, ()>
Convert to the result
type
impl<T: Clone> ToEither<(), T> for Option<T>
fn to_either(&self) -> Either<(), T>
Convert to the either
type
impl<T> IntoEither<(), T> for Option<T>
fn into_either(self) -> Either<(), T>
Convert to the either
type
impl<T> Default for Option<T>
fn default() -> Option<T>
Return the "default value" for a type.
impl<T: Rand> Rand for Option<T>
fn rand<R: Rng>(rng: &mut R) -> Option<T>
Generates a random instance of this type using the specified source of randomness
impl<W: Writer> Writer for Option<W>
fn write(&mut self, buf: &[u8])
Write the given buffer
Raises the io_error
condition on error
fn flush(&mut self)
Flush output
impl<R: Reader> Reader for Option<R>
fn read(&mut self, buf: &mut [u8]) -> Option<uint>
Read bytes, up to the length of buf
and place them in buf
.
Returns the number of bytes read. The number of bytes read my
be less than the number requested, even 0. Returns None
on EOF.
Raises the read_error
condition on error. If the condition
is handled then no guarantee is made about the number of bytes
read and the contents of buf
. If the condition is handled
returns None
(XXX see below).
This doesn't take a len
argument like the old read
.
Will people often need to slice their vectors to call this
and will that be annoying?
Is it actually possible for 0 bytes to be read successfully?
fn eof(&mut self) -> bool
Return whether the Reader has reached the end of the stream.
let reader = FileStream::new()
while !reader.eof() {
println(reader.read_line());
}
Returns true
on failure.
impl<T, A: Acceptor<T>, L: Listener<T, A>> Listener<T, A> for Option<L>
fn listen(self) -> Option<A>
Spin up the listener and start queueing incoming connections
Raises io_error
condition. If the condition is handled,
then listen
returns None
.
impl<T, A: Acceptor<T>> Acceptor<T> for Option<A>
fn accept(&mut self) -> Option<T>
Wait for and accept an incoming connection
Raise io_error
condition. If the condition is handled,
then accept
returns None
.
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).