2014-01-25 01:37:51 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-03-16 20:43:47 -05:00
|
|
|
//! Optional values
|
2013-10-31 17:09:24 -05:00
|
|
|
//!
|
2014-03-16 20:43:47 -05:00
|
|
|
//! Type `Option` represents an optional value: every `Option`
|
|
|
|
//! is either `Some` and contains a value, or `None`, and
|
|
|
|
//! does not. `Option` types are very common in Rust code, as
|
|
|
|
//! they have a number of uses:
|
2013-10-31 17:09:24 -05:00
|
|
|
//!
|
2014-03-16 20:43:47 -05:00
|
|
|
//! * Initial values
|
|
|
|
//! * Return values for functions that are not defined
|
|
|
|
//! over their entire input range (partial functions)
|
|
|
|
//! * Return value for otherwise reporting simple errors, where `None` is
|
|
|
|
//! returned on error
|
|
|
|
//! * Optional struct fields
|
|
|
|
//! * Struct fields that can be loaned or "taken"
|
|
|
|
//! * Optional function arguments
|
|
|
|
//! * Nullable pointers
|
|
|
|
//! * Swapping things out of difficult situations
|
2013-10-31 17:09:24 -05:00
|
|
|
//!
|
2014-03-16 20:43:47 -05:00
|
|
|
//! Options are commonly paired with pattern matching to query the presence
|
2013-10-31 17:09:24 -05:00
|
|
|
//! of a value and take action, always accounting for the `None` case.
|
|
|
|
//!
|
2014-03-16 20:43:47 -05:00
|
|
|
//! ```
|
2014-05-11 11:23:46 -05:00
|
|
|
//! fn divide(numerator: f64, denominator: f64) -> Option<f64> {
|
|
|
|
//! if denominator == 0.0 {
|
|
|
|
//! None
|
|
|
|
//! } else {
|
|
|
|
//! Some(numerator / denominator)
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // The return value of the function is an option
|
|
|
|
//! let result = divide(2.0, 3.0);
|
2014-03-16 20:43:47 -05:00
|
|
|
//!
|
|
|
|
//! // Pattern match to retrieve the value
|
2014-05-11 11:23:46 -05:00
|
|
|
//! match result {
|
|
|
|
//! // The division was valid
|
|
|
|
//! Some(x) => println!("Result: {}", x),
|
|
|
|
//! // The division was invalid
|
|
|
|
//! None => println!("Cannot divide by 0")
|
2014-03-16 20:43:47 -05:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//
|
|
|
|
// FIXME: Show how `Option` is used in practice, with lots of methods
|
|
|
|
//
|
|
|
|
//! # Options and pointers ("nullable" pointers)
|
|
|
|
//!
|
|
|
|
//! Rust's pointer types must always point to a valid location; there are
|
|
|
|
//! no "null" pointers. Instead, Rust has *optional* pointers, like
|
2014-05-05 20:56:44 -05:00
|
|
|
//! the optional owned box, `Option<Box<T>>`.
|
2014-03-16 20:43:47 -05:00
|
|
|
//!
|
|
|
|
//! The following example uses `Option` to create an optional box of
|
2015-02-13 13:36:59 -06:00
|
|
|
//! `i32`. Notice that in order to use the inner `i32` value first the
|
2014-03-16 20:43:47 -05:00
|
|
|
//! `check_optional` function needs to use pattern matching to
|
|
|
|
//! determine whether the box has a value (i.e. it is `Some(...)`) or
|
|
|
|
//! not (`None`).
|
|
|
|
//!
|
|
|
|
//! ```
|
2015-02-13 13:36:59 -06:00
|
|
|
//! let optional: Option<Box<i32>> = None;
|
2014-03-16 20:43:47 -05:00
|
|
|
//! check_optional(&optional);
|
2013-10-31 17:09:24 -05:00
|
|
|
//!
|
2015-02-13 13:36:59 -06:00
|
|
|
//! let optional: Option<Box<i32>> = Some(Box::new(9000));
|
2014-03-16 20:43:47 -05:00
|
|
|
//! check_optional(&optional);
|
|
|
|
//!
|
2015-02-13 13:36:59 -06:00
|
|
|
//! fn check_optional(optional: &Option<Box<i32>>) {
|
2014-03-16 20:43:47 -05:00
|
|
|
//! match *optional {
|
|
|
|
//! Some(ref p) => println!("have value {}", p),
|
|
|
|
//! None => println!("have no value")
|
|
|
|
//! }
|
|
|
|
//! }
|
2013-10-31 17:09:24 -05:00
|
|
|
//! ```
|
2014-03-16 20:43:47 -05:00
|
|
|
//!
|
|
|
|
//! This usage of `Option` to create safe nullable pointers is so
|
|
|
|
//! common that Rust does special optimizations to make the
|
2014-05-05 20:56:44 -05:00
|
|
|
//! representation of `Option<Box<T>>` a single pointer. Optional pointers
|
2014-03-16 20:43:47 -05:00
|
|
|
//! in Rust are stored as efficiently as any other pointer type.
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! Basic pattern matching on `Option`:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let msg = Some("howdy");
|
2013-10-31 17:09:24 -05:00
|
|
|
//!
|
|
|
|
//! // Take a reference to the contained string
|
|
|
|
//! match msg {
|
2014-01-09 04:06:55 -06:00
|
|
|
//! Some(ref m) => println!("{}", *m),
|
2013-10-31 17:09:24 -05:00
|
|
|
//! None => ()
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // Remove the contained string, destroying the Option
|
|
|
|
//! let unwrapped_msg = match msg {
|
|
|
|
//! Some(m) => m,
|
2014-03-16 20:43:47 -05:00
|
|
|
//! None => "default message"
|
2013-10-31 17:09:24 -05:00
|
|
|
//! };
|
|
|
|
//! ```
|
2014-03-16 20:43:47 -05:00
|
|
|
//!
|
|
|
|
//! Initialize a result to `None` before a loop:
|
|
|
|
//!
|
|
|
|
//! ```
|
2015-02-15 10:45:10 -06:00
|
|
|
//! enum Kingdom { Plant(u32, &'static str), Animal(u32, &'static str) }
|
2014-03-16 20:43:47 -05:00
|
|
|
//!
|
|
|
|
//! // A list of data to search through.
|
|
|
|
//! let all_the_big_things = [
|
2014-11-06 02:05:53 -06:00
|
|
|
//! Kingdom::Plant(250, "redwood"),
|
|
|
|
//! Kingdom::Plant(230, "noble fir"),
|
|
|
|
//! Kingdom::Plant(229, "sugar pine"),
|
|
|
|
//! Kingdom::Animal(25, "blue whale"),
|
|
|
|
//! Kingdom::Animal(19, "fin whale"),
|
|
|
|
//! Kingdom::Animal(15, "north pacific right whale"),
|
2014-03-16 20:43:47 -05:00
|
|
|
//! ];
|
|
|
|
//!
|
|
|
|
//! // We're going to search for the name of the biggest animal,
|
|
|
|
//! // but to start with we've just got `None`.
|
|
|
|
//! let mut name_of_biggest_animal = None;
|
|
|
|
//! let mut size_of_biggest_animal = 0;
|
|
|
|
//! for big_thing in all_the_big_things.iter() {
|
|
|
|
//! match *big_thing {
|
2014-11-06 02:05:53 -06:00
|
|
|
//! Kingdom::Animal(size, name) if size > size_of_biggest_animal => {
|
2014-03-16 20:43:47 -05:00
|
|
|
//! // Now we've found the name of some big animal
|
|
|
|
//! size_of_biggest_animal = size;
|
|
|
|
//! name_of_biggest_animal = Some(name);
|
|
|
|
//! }
|
2014-11-06 02:05:53 -06:00
|
|
|
//! Kingdom::Animal(..) | Kingdom::Plant(..) => ()
|
2014-03-16 20:43:47 -05:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! match name_of_biggest_animal {
|
|
|
|
//! Some(name) => println!("the biggest animal is {}", name),
|
|
|
|
//! None => println!("there are no animals :(")
|
|
|
|
//! }
|
|
|
|
//! ```
|
2013-10-31 17:09:24 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-08-18 19:42:11 -05:00
|
|
|
|
2014-12-08 11:58:01 -06:00
|
|
|
use self::Option::*;
|
2014-11-06 02:05:53 -06:00
|
|
|
|
2015-01-07 16:58:31 -06:00
|
|
|
use clone::Clone;
|
2014-10-29 18:48:20 -05:00
|
|
|
use cmp::{Eq, Ord};
|
2013-09-09 21:29:11 -05:00
|
|
|
use default::Default;
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
use iter::{ExactSizeIterator};
|
2015-01-07 16:58:31 -06:00
|
|
|
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator};
|
2014-01-31 14:35:36 -06:00
|
|
|
use mem;
|
2015-01-07 16:58:31 -06:00
|
|
|
use ops::{Deref, FnOnce};
|
2014-11-28 10:57:41 -06:00
|
|
|
use result::Result::{Ok, Err};
|
2015-01-07 16:58:31 -06:00
|
|
|
use result::Result;
|
2014-10-06 21:55:52 -05:00
|
|
|
use slice::AsSlice;
|
2015-01-07 16:58:31 -06:00
|
|
|
use slice;
|
2012-08-27 19:24:15 -05:00
|
|
|
|
2014-07-21 22:54:28 -05:00
|
|
|
// Note that this is not a lang item per se, but it has a hidden dependency on
|
|
|
|
// `Iterator`, which is one. The compiler assumes that the `next` method of
|
|
|
|
// `Iterator` is an enumeration with one type parameter and two variants,
|
|
|
|
// which basically means it must be `Option`.
|
|
|
|
|
2015-02-13 12:47:09 -06:00
|
|
|
/// The `Option` type. See [the module level documentation](../index.html) for more.
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2012-10-01 16:08:34 -05:00
|
|
|
pub enum Option<T> {
|
2013-10-31 17:09:24 -05:00
|
|
|
/// No value
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2012-08-20 14:23:37 -05:00
|
|
|
None,
|
2013-10-31 17:09:24 -05:00
|
|
|
/// Some value `T`
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
Some(T)
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Type implementation
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<T> Option<T> {
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Querying the contained values
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-16 20:43:47 -05:00
|
|
|
/// Returns `true` if the option is a `Some` value
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.is_some(), true);
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.is_some(), false);
|
|
|
|
/// ```
|
2013-10-31 17:09:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn is_some(&self) -> bool {
|
2013-08-03 18:59:24 -05:00
|
|
|
match *self {
|
2013-10-31 17:09:24 -05:00
|
|
|
Some(_) => true,
|
|
|
|
None => false
|
2013-08-03 18:59:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 20:43:47 -05:00
|
|
|
/// Returns `true` if the option is a `None` value
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.is_none(), false);
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.is_none(), true);
|
|
|
|
/// ```
|
2013-10-31 17:09:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
!self.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Adapter for working with references
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-20 01:08:47 -05:00
|
|
|
/// Convert from `Option<T>` to `Option<&T>`
|
2014-03-16 20:43:47 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2015-02-13 13:36:59 -06:00
|
|
|
/// Convert an `Option<String>` into an `Option<usize>`, preserving the original.
|
2014-03-16 20:43:47 -05:00
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-05-25 05:17:19 -05:00
|
|
|
/// let num_as_str: Option<String> = Some("10".to_string());
|
2014-05-22 18:57:53 -05:00
|
|
|
/// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
|
2014-03-16 20:43:47 -05:00
|
|
|
/// // then consume *that* with `map`, leaving `num_as_str` on the stack.
|
2015-02-13 13:36:59 -06:00
|
|
|
/// let num_as_int: Option<usize> = num_as_str.as_ref().map(|n| n.len());
|
2015-01-06 18:16:35 -06:00
|
|
|
/// println!("still can print num_as_str: {:?}", num_as_str);
|
2014-03-16 20:43:47 -05:00
|
|
|
/// ```
|
2013-09-20 01:08:47 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-09-20 01:08:47 -05:00
|
|
|
pub fn as_ref<'r>(&'r self) -> Option<&'r T> {
|
2014-11-13 10:58:03 -06:00
|
|
|
match *self {
|
|
|
|
Some(ref x) => Some(x),
|
|
|
|
None => None
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert from `Option<T>` to `Option<&mut T>`
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let mut x = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// match x.as_mut() {
|
2014-10-20 20:18:59 -05:00
|
|
|
/// Some(v) => *v = 42,
|
2014-09-16 10:20:03 -05:00
|
|
|
/// None => {},
|
|
|
|
/// }
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x, Some(42));
|
2014-09-16 10:20:03 -05:00
|
|
|
/// ```
|
2013-09-20 01:08:47 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-09-20 01:08:47 -05:00
|
|
|
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
|
2014-11-13 10:58:03 -06:00
|
|
|
match *self {
|
|
|
|
Some(ref mut x) => Some(x),
|
|
|
|
None => None
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
}
|
|
|
|
|
2014-03-14 10:32:04 -05:00
|
|
|
/// Convert from `Option<T>` to `&mut [T]` (without copying)
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut x = Some("Diamonds");
|
|
|
|
/// {
|
|
|
|
/// let v = x.as_mut_slice();
|
2014-11-21 00:20:04 -06:00
|
|
|
/// assert!(v == ["Diamonds"]);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// v[0] = "Dirt";
|
2014-11-21 00:20:04 -06:00
|
|
|
/// assert!(v == ["Dirt"]);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// }
|
|
|
|
/// assert_eq!(x, Some("Dirt"));
|
|
|
|
/// ```
|
2014-01-15 13:39:08 -06:00
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "waiting for mut conventions")]
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
|
|
|
|
match *self {
|
2014-08-04 07:19:02 -05:00
|
|
|
Some(ref mut x) => {
|
|
|
|
let result: &mut [T] = slice::mut_ref_slice(x);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let result: &mut [T] = &mut [];
|
|
|
|
result
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Getting to contained values
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-02 17:49:42 -05:00
|
|
|
/// Unwraps an option, yielding the content of a `Some`
|
|
|
|
///
|
2014-10-09 14:17:22 -05:00
|
|
|
/// # Panics
|
2014-06-02 17:49:42 -05:00
|
|
|
///
|
2014-11-08 09:47:51 -06:00
|
|
|
/// Panics if the value is a `None` with a custom panic message provided by
|
2014-06-02 17:49:42 -05:00
|
|
|
/// `msg`.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("value");
|
|
|
|
/// assert_eq!(x.expect("the world is ending"), "value");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```{.should_fail}
|
|
|
|
/// let x: Option<&str> = None;
|
2014-11-08 09:47:51 -06:00
|
|
|
/// x.expect("the world is ending"); // panics with `world is ending`
|
2014-09-16 10:20:03 -05:00
|
|
|
/// ```
|
2014-06-02 17:49:42 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-06-02 17:49:42 -05:00
|
|
|
pub fn expect(self, msg: &str) -> T {
|
|
|
|
match self {
|
|
|
|
Some(val) => val,
|
2014-10-09 14:17:22 -05:00
|
|
|
None => panic!("{}", msg),
|
2014-06-02 17:49:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 12:47:13 -05:00
|
|
|
/// Returns the inner `T` of a `Some(T)`.
|
2013-10-31 17:09:24 -05:00
|
|
|
///
|
2014-10-09 14:17:22 -05:00
|
|
|
/// # Panics
|
2013-10-31 17:09:24 -05:00
|
|
|
///
|
2014-10-09 14:17:22 -05:00
|
|
|
/// Panics if the self value equals `None`.
|
2013-10-31 17:09:24 -05:00
|
|
|
///
|
|
|
|
/// # Safety note
|
|
|
|
///
|
2014-10-09 14:17:22 -05:00
|
|
|
/// In general, because this function may panic, its use is discouraged.
|
2013-10-31 17:09:24 -05:00
|
|
|
/// Instead, prefer to use pattern matching and handle the `None`
|
|
|
|
/// case explicitly.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
2014-09-17 08:02:26 -05:00
|
|
|
/// # Example
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("air");
|
|
|
|
/// assert_eq!(x.unwrap(), "air");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```{.should_fail}
|
|
|
|
/// let x: Option<&str> = None;
|
|
|
|
/// assert_eq!(x.unwrap(), "air"); // fails
|
|
|
|
/// ```
|
2013-10-31 17:09:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn unwrap(self) -> T {
|
|
|
|
match self {
|
|
|
|
Some(val) => val,
|
2014-10-09 14:17:22 -05:00
|
|
|
None => panic!("called `Option::unwrap()` on a `None` value"),
|
2013-10-31 17:09:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 22:42:25 -05:00
|
|
|
/// Returns the contained value or a default.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// assert_eq!(Some("car").unwrap_or("bike"), "car");
|
|
|
|
/// assert_eq!(None.unwrap_or("bike"), "bike");
|
|
|
|
/// ```
|
2013-10-31 17:09:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn unwrap_or(self, def: T) -> T {
|
|
|
|
match self {
|
|
|
|
Some(x) => x,
|
|
|
|
None => def
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 22:42:25 -05:00
|
|
|
/// Returns the contained value or computes it from a closure.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let k = 10i32;
|
|
|
|
/// assert_eq!(Some(4).unwrap_or_else(|| 2 * k), 4);
|
|
|
|
/// assert_eq!(None.unwrap_or_else(|| 2 * k), 20);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// ```
|
2013-10-31 17:09:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
|
2013-10-31 17:09:24 -05:00
|
|
|
match self {
|
|
|
|
Some(x) => x,
|
|
|
|
None => f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Transforming contained values
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-16 20:43:47 -05:00
|
|
|
/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2015-02-13 13:36:59 -06:00
|
|
|
/// Convert an `Option<String>` into an `Option<usize>`, consuming the original:
|
2014-03-16 20:43:47 -05:00
|
|
|
///
|
|
|
|
/// ```
|
2014-05-25 05:17:19 -05:00
|
|
|
/// let num_as_str: Option<String> = Some("10".to_string());
|
2014-03-16 20:43:47 -05:00
|
|
|
/// // `Option::map` takes self *by value*, consuming `num_as_str`
|
2015-02-13 13:36:59 -06:00
|
|
|
/// let num_as_int: Option<usize> = num_as_str.map(|n| n.len());
|
2014-03-16 20:43:47 -05:00
|
|
|
/// ```
|
2013-09-20 01:08:47 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
|
2014-11-13 10:58:03 -06:00
|
|
|
match self {
|
|
|
|
Some(x) => Some(f(x)),
|
|
|
|
None => None
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies a function to the contained value or returns a default.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.map_or(42, |v| v.len()), 3);
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.map_or(42, |v| v.len()), 42);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// ```
|
2013-09-20 01:08:47 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn map_or<U, F: FnOnce(T) -> U>(self, def: U, f: F) -> U {
|
2014-11-13 10:58:03 -06:00
|
|
|
match self {
|
|
|
|
Some(t) => f(t),
|
|
|
|
None => def
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:42:11 -05:00
|
|
|
/// Applies a function to the contained value or computes a default.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let k = 21;
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// ```
|
2014-08-18 19:42:11 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, def: D, f: F) -> U {
|
2014-11-13 10:58:03 -06:00
|
|
|
match self {
|
|
|
|
Some(t) => f(t),
|
|
|
|
None => def()
|
|
|
|
}
|
2014-08-18 19:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:18:30 -05:00
|
|
|
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
|
|
|
|
/// `Ok(v)` and `None` to `Err(err)`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.ok_or(0), Ok("foo"));
|
2014-09-23 01:18:30 -05:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.ok_or(0), Err(0));
|
2014-09-23 01:18:30 -05:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core")]
|
2014-09-23 01:18:30 -05:00
|
|
|
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
|
|
|
|
match self {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
None => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
|
|
|
|
/// `Ok(v)` and `None` to `Err(err())`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.ok_or_else(|| 0), Ok("foo"));
|
2014-09-23 01:18:30 -05:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.ok_or_else(|| 0), Err(0));
|
2014-09-23 01:18:30 -05:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
|
2014-09-23 01:18:30 -05:00
|
|
|
match self {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
None => Err(err()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Iterator constructors
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-16 22:42:25 -05:00
|
|
|
/// Returns an iterator over the possibly contained value.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let x = Some(4);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.iter().next(), Some(&4));
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.iter().next(), None);
|
|
|
|
/// ```
|
2013-06-10 16:45:59 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 12:34:23 -06:00
|
|
|
pub fn iter(&self) -> Iter<T> {
|
|
|
|
Iter { inner: Item { opt: self.as_ref() } }
|
2013-06-10 16:45:59 -05:00
|
|
|
}
|
|
|
|
|
2014-03-16 22:42:25 -05:00
|
|
|
/// Returns a mutable iterator over the possibly contained value.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let mut x = Some(4);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// match x.iter_mut().next() {
|
2015-01-22 08:08:56 -06:00
|
|
|
/// Some(&mut ref mut v) => *v = 42,
|
2014-09-16 10:20:03 -05:00
|
|
|
/// None => {},
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(x, Some(42));
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let mut x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// assert_eq!(x.iter_mut().next(), None);
|
|
|
|
/// ```
|
2013-06-10 16:45:59 -05:00
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "waiting for iterator conventions")]
|
2014-12-14 12:34:23 -06:00
|
|
|
pub fn iter_mut(&mut self) -> IterMut<T> {
|
|
|
|
IterMut { inner: Item { opt: self.as_mut() } }
|
2013-06-10 16:45:59 -05:00
|
|
|
}
|
|
|
|
|
2014-03-16 22:42:25 -05:00
|
|
|
/// Returns a consuming iterator over the possibly contained value.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("string");
|
|
|
|
/// let v: Vec<&str> = x.into_iter().collect();
|
|
|
|
/// assert_eq!(v, vec!["string"]);
|
|
|
|
///
|
|
|
|
/// let x = None;
|
|
|
|
/// let v: Vec<&str> = x.into_iter().collect();
|
2014-11-21 00:20:04 -06:00
|
|
|
/// assert!(v.is_empty());
|
2014-09-16 10:20:03 -05:00
|
|
|
/// ```
|
2013-08-03 12:40:20 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 12:34:23 -06:00
|
|
|
pub fn into_iter(self) -> IntoIter<T> {
|
|
|
|
IntoIter { inner: Item { opt: self } }
|
2013-08-03 12:40:20 -05:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Boolean operations on the values, eager and lazy
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2013-03-16 14:49:12 -05:00
|
|
|
|
2013-09-11 11:00:27 -05:00
|
|
|
/// Returns `None` if the option is `None`, otherwise returns `optb`.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let x = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// let y: Option<&str> = None;
|
|
|
|
/// assert_eq!(x.and(y), None);
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// let y = Some("foo");
|
|
|
|
/// assert_eq!(x.and(y), None);
|
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let x = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// let y = Some("foo");
|
|
|
|
/// assert_eq!(x.and(y), Some("foo"));
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// let y: Option<&str> = None;
|
|
|
|
/// assert_eq!(x.and(y), None);
|
|
|
|
/// ```
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn and<U>(self, optb: Option<U>) -> Option<U> {
|
2013-03-16 14:49:12 -05:00
|
|
|
match self {
|
2013-09-11 11:00:27 -05:00
|
|
|
Some(_) => optb,
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:54:02 -05:00
|
|
|
/// Returns `None` if the option is `None`, otherwise calls `f` with the
|
|
|
|
/// wrapped value and returns the result.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
2015-02-09 05:44:19 -06:00
|
|
|
/// Some languages call this operation flatmap.
|
|
|
|
///
|
2014-09-16 10:20:03 -05:00
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-15 10:45:10 -06:00
|
|
|
/// fn sq(x: u32) -> Option<u32> { Some(x * x) }
|
|
|
|
/// fn nope(_: u32) -> Option<u32> { None }
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16));
|
|
|
|
/// assert_eq!(Some(2).and_then(sq).and_then(nope), None);
|
|
|
|
/// assert_eq!(Some(2).and_then(nope).and_then(sq), None);
|
|
|
|
/// assert_eq!(None.and_then(sq).and_then(sq), None);
|
|
|
|
/// ```
|
2013-09-11 11:00:27 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> {
|
2013-09-11 11:00:27 -05:00
|
|
|
match self {
|
2013-09-11 14:52:17 -05:00
|
|
|
Some(x) => f(x),
|
2013-09-11 11:00:27 -05:00
|
|
|
None => None,
|
2013-03-16 14:49:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 11:00:27 -05:00
|
|
|
/// Returns the option if it contains a value, otherwise returns `optb`.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let x = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// let y = None;
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(x.or(y), Some(2));
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// let x = None;
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let y = Some(100);
|
|
|
|
/// assert_eq!(x.or(y), Some(100));
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let x = Some(2);
|
|
|
|
/// let y = Some(100);
|
|
|
|
/// assert_eq!(x.or(y), Some(2));
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// let y = None;
|
|
|
|
/// assert_eq!(x.or(y), None);
|
|
|
|
/// ```
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn or(self, optb: Option<T>) -> Option<T> {
|
2013-03-16 14:49:12 -05:00
|
|
|
match self {
|
2013-09-11 11:00:27 -05:00
|
|
|
Some(_) => self,
|
|
|
|
None => optb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:54:02 -05:00
|
|
|
/// Returns the option if it contains a value, otherwise calls `f` and
|
|
|
|
/// returns the result.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn nobody() -> Option<&'static str> { None }
|
|
|
|
/// fn vikings() -> Option<&'static str> { Some("vikings") }
|
|
|
|
///
|
|
|
|
/// assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians"));
|
|
|
|
/// assert_eq!(None.or_else(vikings), Some("vikings"));
|
|
|
|
/// assert_eq!(None.or_else(nobody), None);
|
|
|
|
/// ```
|
2013-09-11 11:00:27 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 17:06:41 -06:00
|
|
|
pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> {
|
2013-09-11 11:00:27 -05:00
|
|
|
match self {
|
|
|
|
Some(_) => self,
|
2014-03-16 22:42:25 -05:00
|
|
|
None => f()
|
2013-09-11 11:00:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Misc
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-16 22:42:25 -05:00
|
|
|
/// Takes the value out of the option, leaving a `None` in its place.
|
2014-09-16 10:20:03 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let mut x = Some(2);
|
2014-09-16 10:20:03 -05:00
|
|
|
/// x.take();
|
|
|
|
/// assert_eq!(x, None);
|
|
|
|
///
|
2015-02-15 10:45:10 -06:00
|
|
|
/// let mut x: Option<u32> = None;
|
2014-09-16 10:20:03 -05:00
|
|
|
/// x.take();
|
|
|
|
/// assert_eq!(x, None);
|
|
|
|
/// ```
|
2013-10-31 17:09:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn take(&mut self) -> Option<T> {
|
2014-01-31 14:35:36 -06:00
|
|
|
mem::replace(self, None)
|
2013-10-31 17:09:24 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-10 15:11:35 -05:00
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl<'a, T: Clone, D: Deref<Target=T>> Option<D> {
|
2014-11-17 19:59:46 -06:00
|
|
|
/// Maps an Option<D> to an Option<T> by dereffing and cloning the contents of the Option.
|
|
|
|
/// Useful for converting an Option<&T> to an Option<T>.
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "recently added as part of collections reform")]
|
2014-11-07 13:05:50 -06:00
|
|
|
pub fn cloned(self) -> Option<T> {
|
2014-11-17 19:59:46 -06:00
|
|
|
self.map(|t| t.deref().clone())
|
2014-11-07 13:05:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
impl<T: Default> Option<T> {
|
2014-03-16 20:43:47 -05:00
|
|
|
/// 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
|
2014-12-10 21:46:38 -06:00
|
|
|
/// into 0 (the default value for integers). `parse` converts
|
2014-03-16 20:43:47 -05:00
|
|
|
/// 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";
|
2015-01-28 00:52:32 -06:00
|
|
|
/// let good_year = good_year_from_input.parse().ok().unwrap_or_default();
|
|
|
|
/// let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();
|
2014-03-16 20:43:47 -05:00
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(1909, good_year);
|
|
|
|
/// assert_eq!(0, bad_year);
|
2014-03-16 20:43:47 -05:00
|
|
|
/// ```
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 17:09:24 -05:00
|
|
|
pub fn unwrap_or_default(self) -> T {
|
2013-03-16 14:49:12 -05:00
|
|
|
match self {
|
2013-08-03 18:59:24 -05:00
|
|
|
Some(x) => x,
|
2013-10-31 17:09:24 -05:00
|
|
|
None => Default::default()
|
2013-03-16 14:49:12 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 17:09:24 -05:00
|
|
|
}
|
2012-12-18 20:55:19 -06:00
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Trait implementations
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "waiting on the stability of the trait itself")]
|
2014-10-06 21:55:52 -05:00
|
|
|
impl<T> AsSlice<T> for Option<T> {
|
|
|
|
/// Convert from `Option<T>` to `&[T]` (without copying)
|
|
|
|
#[inline]
|
|
|
|
fn as_slice<'a>(&'a self) -> &'a [T] {
|
|
|
|
match *self {
|
|
|
|
Some(ref x) => slice::ref_slice(x),
|
|
|
|
None => {
|
|
|
|
let result: &[_] = &[];
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-09-09 21:29:11 -05:00
|
|
|
impl<T> Default for Option<T> {
|
2013-09-11 23:49:25 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-09-09 21:29:11 -05:00
|
|
|
fn default() -> Option<T> { None }
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2014-12-14 12:34:23 -06:00
|
|
|
// The Option Iterators
|
2013-10-31 17:09:24 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2013-01-03 17:36:23 -06:00
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone)]
|
2014-12-14 12:34:23 -06:00
|
|
|
struct Item<A> {
|
2014-03-27 17:09:47 -05:00
|
|
|
opt: Option<A>
|
2013-06-10 16:45:59 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<A> Iterator for Item<A> {
|
|
|
|
type Item = A;
|
|
|
|
|
2013-08-03 14:34:00 -05:00
|
|
|
#[inline]
|
2013-08-03 12:40:20 -05:00
|
|
|
fn next(&mut self) -> Option<A> {
|
2013-08-03 14:34:00 -05:00
|
|
|
self.opt.take()
|
2013-06-10 16:45:59 -05:00
|
|
|
}
|
2013-07-08 17:31:26 -05:00
|
|
|
|
2013-08-03 14:34:00 -05:00
|
|
|
#[inline]
|
2015-02-13 13:36:59 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2013-07-08 17:31:26 -05:00
|
|
|
match self.opt {
|
|
|
|
Some(_) => (1, Some(1)),
|
|
|
|
None => (0, Some(0)),
|
|
|
|
}
|
|
|
|
}
|
2013-06-10 16:45:59 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<A> DoubleEndedIterator for Item<A> {
|
2013-08-10 21:21:31 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
self.opt.take()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<A> ExactSizeIterator for Item<A> {}
|
2013-09-01 11:20:24 -05:00
|
|
|
|
2014-12-14 12:34:23 -06:00
|
|
|
/// An iterator over a reference of the contained item in an Option.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 12:34:23 -06:00
|
|
|
pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<'a, A> Iterator for Iter<'a, A> {
|
|
|
|
type Item = &'a A;
|
|
|
|
|
2014-12-14 12:34:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a A> { self.inner.next() }
|
|
|
|
#[inline]
|
2015-02-13 13:36:59 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2014-12-14 12:34:23 -06:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
|
2014-12-14 12:34:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a A> { self.inner.next_back() }
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
|
2014-12-14 12:34:23 -06:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 12:34:23 -06:00
|
|
|
impl<'a, A> Clone for Iter<'a, A> {
|
|
|
|
fn clone(&self) -> Iter<'a, A> {
|
|
|
|
Iter { inner: self.inner.clone() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over a mutable reference of the contained item in an Option.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 12:34:23 -06:00
|
|
|
pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<'a, A> Iterator for IterMut<'a, A> {
|
|
|
|
type Item = &'a mut A;
|
|
|
|
|
2014-12-14 12:34:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
|
|
|
|
#[inline]
|
2015-02-13 13:36:59 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2014-12-14 12:34:23 -06:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
|
2014-12-14 12:34:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a mut A> { self.inner.next_back() }
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
|
2014-12-14 12:34:23 -06:00
|
|
|
|
|
|
|
/// An iterator over the item contained inside an Option.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 12:34:23 -06:00
|
|
|
pub struct IntoIter<A> { inner: Item<A> }
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<A> Iterator for IntoIter<A> {
|
|
|
|
type Item = A;
|
|
|
|
|
2014-12-14 12:34:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> { self.inner.next() }
|
|
|
|
#[inline]
|
2015-02-13 13:36:59 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2014-12-14 12:34:23 -06:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<A> DoubleEndedIterator for IntoIter<A> {
|
2014-12-14 12:34:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> { self.inner.next_back() }
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<A> ExactSizeIterator for IntoIter<A> {}
|
2014-12-14 12:34:23 -06:00
|
|
|
|
2013-12-20 22:56:07 -06:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2014-11-14 22:39:41 -06:00
|
|
|
// FromIterator
|
2013-12-20 22:56:07 -06:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-08-18 19:42:11 -05:00
|
|
|
impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
|
|
|
|
/// Takes each element in the `Iterator`: if it is `None`, no further
|
|
|
|
/// elements are taken, and the `None` is returned. Should no `None` occur, a
|
|
|
|
/// container with the values of each `Option` is returned.
|
|
|
|
///
|
|
|
|
/// Here is an example which increments every integer in a vector,
|
|
|
|
/// checking for overflow:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2015-02-13 13:36:59 -06:00
|
|
|
/// use std::u16;
|
2014-08-18 19:42:11 -05:00
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let v = vec!(1, 2);
|
2015-02-13 13:36:59 -06:00
|
|
|
/// let res: Option<Vec<u16>> = v.iter().map(|&x: &u16|
|
|
|
|
/// if x == u16::MAX { None }
|
2014-08-18 19:42:11 -05:00
|
|
|
/// else { Some(x + 1) }
|
|
|
|
/// ).collect();
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert!(res == Some(vec!(2, 3)));
|
2014-08-18 19:42:11 -05:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 15:18:41 -06:00
|
|
|
fn from_iter<I: Iterator<Item=Option<A>>>(iter: I) -> Option<V> {
|
2014-08-18 19:42:11 -05:00
|
|
|
// FIXME(#11084): This could be replaced with Iterator::scan when this
|
|
|
|
// performance bug is closed.
|
|
|
|
|
|
|
|
struct Adapter<Iter> {
|
|
|
|
iter: Iter,
|
|
|
|
found_none: bool,
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:18:41 -06:00
|
|
|
impl<T, Iter: Iterator<Item=Option<T>>> Iterator for Adapter<Iter> {
|
|
|
|
type Item = T;
|
|
|
|
|
2014-08-18 19:42:11 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<T> {
|
|
|
|
match self.iter.next() {
|
|
|
|
Some(Some(value)) => Some(value),
|
|
|
|
Some(None) => {
|
|
|
|
self.found_none = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
None => None,
|
2014-06-23 18:27:54 -05:00
|
|
|
}
|
2013-12-20 22:56:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:42:11 -05:00
|
|
|
let mut adapter = Adapter { iter: iter, found_none: false };
|
|
|
|
let v: V = FromIterator::from_iter(adapter.by_ref());
|
2013-12-20 22:56:07 -06:00
|
|
|
|
2014-08-18 19:42:11 -05:00
|
|
|
if adapter.found_none {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(v)
|
|
|
|
}
|
2013-12-20 22:56:07 -06:00
|
|
|
}
|
|
|
|
}
|