2014-01-25 07:37:51 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00: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.
|
|
|
|
|
2016-03-04 22:37:11 +00:00
|
|
|
//! Optional values.
|
2013-10-31 22:09:24 +00:00
|
|
|
//!
|
2016-09-09 14:07:56 +00: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
|
2014-03-17 01:43:47 +00:00
|
|
|
//! they have a number of uses:
|
2013-10-31 22:09:24 +00:00
|
|
|
//!
|
2014-03-17 01:43:47 +00: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 22:09:24 +00:00
|
|
|
//!
|
2016-09-09 14:07:56 +00:00
|
|
|
//! [`Option`]s are commonly paired with pattern matching to query the presence
|
|
|
|
//! of a value and take action, always accounting for the [`None`] case.
|
2013-10-31 22:09:24 +00:00
|
|
|
//!
|
2014-03-17 01:43:47 +00:00
|
|
|
//! ```
|
2014-05-11 16:23:46 +00: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-17 01:43:47 +00:00
|
|
|
//!
|
|
|
|
//! // Pattern match to retrieve the value
|
2014-05-11 16:23:46 +00:00
|
|
|
//! match result {
|
|
|
|
//! // The division was valid
|
|
|
|
//! Some(x) => println!("Result: {}", x),
|
|
|
|
//! // The division was invalid
|
2015-07-05 18:27:13 +00:00
|
|
|
//! None => println!("Cannot divide by 0"),
|
2014-03-17 01:43:47 +00: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
|
2016-09-09 14:07:56 +00:00
|
|
|
//! the optional owned box, [`Option`]`<`[`Box<T>`]`>`.
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
2016-09-09 14:07:56 +00:00
|
|
|
//! The following example uses [`Option`] to create an optional box of
|
2017-01-30 06:39:03 +00:00
|
|
|
//! [`i32`]. Notice that in order to use the inner [`i32`] value first, the
|
2014-03-17 01:43:47 +00:00
|
|
|
//! `check_optional` function needs to use pattern matching to
|
2016-09-09 14:07:56 +00:00
|
|
|
//! determine whether the box has a value (i.e. it is [`Some(...)`][`Some`]) or
|
|
|
|
//! not ([`None`]).
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
|
|
|
//! ```
|
2017-05-04 15:53:24 +00:00
|
|
|
//! let optional = None;
|
|
|
|
//! check_optional(optional);
|
2013-10-31 22:09:24 +00:00
|
|
|
//!
|
2017-05-04 15:53:24 +00:00
|
|
|
//! let optional = Some(Box::new(9000));
|
|
|
|
//! check_optional(optional);
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
2017-05-04 15:53:24 +00:00
|
|
|
//! fn check_optional(optional: Option<Box<i32>>) {
|
|
|
|
//! match optional {
|
2017-01-30 06:39:03 +00:00
|
|
|
//! Some(ref p) => println!("has value {}", p),
|
|
|
|
//! None => println!("has no value"),
|
2014-03-17 01:43:47 +00:00
|
|
|
//! }
|
|
|
|
//! }
|
2013-10-31 22:09:24 +00:00
|
|
|
//! ```
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
2016-09-09 14:07:56 +00:00
|
|
|
//! This usage of [`Option`] to create safe nullable pointers is so
|
2014-03-17 01:43:47 +00:00
|
|
|
//! common that Rust does special optimizations to make the
|
2016-09-09 14:07:56 +00:00
|
|
|
//! representation of [`Option`]`<`[`Box<T>`]`>` a single pointer. Optional pointers
|
2014-03-17 01:43:47 +00:00
|
|
|
//! in Rust are stored as efficiently as any other pointer type.
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
2016-09-09 14:07:56 +00:00
|
|
|
//! Basic pattern matching on [`Option`]:
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let msg = Some("howdy");
|
2013-10-31 22:09:24 +00:00
|
|
|
//!
|
|
|
|
//! // Take a reference to the contained string
|
2016-03-24 12:24:39 +00:00
|
|
|
//! if let Some(ref m) = msg {
|
|
|
|
//! println!("{}", *m);
|
2013-10-31 22:09:24 +00:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // Remove the contained string, destroying the Option
|
2016-03-24 12:24:39 +00:00
|
|
|
//! let unwrapped_msg = msg.unwrap_or("default message");
|
2013-10-31 22:09:24 +00:00
|
|
|
//! ```
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
2016-09-09 14:07:56 +00:00
|
|
|
//! Initialize a result to [`None`] before a loop:
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
|
|
|
//! ```
|
2015-02-15 16:45:10 +00:00
|
|
|
//! enum Kingdom { Plant(u32, &'static str), Animal(u32, &'static str) }
|
2014-03-17 01:43:47 +00:00
|
|
|
//!
|
|
|
|
//! // A list of data to search through.
|
|
|
|
//! let all_the_big_things = [
|
2014-11-06 08:05:53 +00: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-17 01:43:47 +00: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;
|
2015-06-10 16:22:20 +00:00
|
|
|
//! for big_thing in &all_the_big_things {
|
2014-03-17 01:43:47 +00:00
|
|
|
//! match *big_thing {
|
2014-11-06 08:05:53 +00:00
|
|
|
//! Kingdom::Animal(size, name) if size > size_of_biggest_animal => {
|
2014-03-17 01:43:47 +00: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 08:05:53 +00:00
|
|
|
//! Kingdom::Animal(..) | Kingdom::Plant(..) => ()
|
2014-03-17 01:43:47 +00:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! match name_of_biggest_animal {
|
|
|
|
//! Some(name) => println!("the biggest animal is {}", name),
|
2015-07-05 18:27:13 +00:00
|
|
|
//! None => println!("there are no animals :("),
|
2014-03-17 01:43:47 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
2016-09-09 14:07:56 +00:00
|
|
|
//!
|
|
|
|
//! [`Option`]: enum.Option.html
|
|
|
|
//! [`Some`]: enum.Option.html#variant.Some
|
|
|
|
//! [`None`]: enum.Option.html#variant.None
|
|
|
|
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
|
|
|
|
//! [`i32`]: ../../std/primitive.i32.html
|
2013-10-31 22:09:24 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-08-19 00:42:11 +00:00
|
|
|
|
2016-10-20 12:34:34 +00:00
|
|
|
use iter::{FromIterator, FusedIterator, TrustedLen};
|
2018-07-30 03:03:15 +00:00
|
|
|
use {hint, mem, ops::{self, Deref}};
|
2018-05-23 00:07:51 +00:00
|
|
|
use mem::PinMut;
|
2012-08-28 00:24:15 +00:00
|
|
|
|
2014-07-22 03:54:28 +00: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-05-04 16:53:08 +00:00
|
|
|
/// The `Option` type. See [the module level documentation](index.html) for more.
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2012-10-01 21:08:34 +00:00
|
|
|
pub enum Option<T> {
|
2013-10-31 22:09:24 +00:00
|
|
|
/// No value
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2012-08-20 19:23:37 +00:00
|
|
|
None,
|
2013-10-31 22:09:24 +00:00
|
|
|
/// Some value `T`
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-09-09 14:07:56 +00:00
|
|
|
Some(#[stable(feature = "rust1", since = "1.0.0")] T),
|
2011-12-14 00:25:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Type implementation
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<T> Option<T> {
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Querying the contained values
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Returns `true` if the option is a [`Some`] value.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = Some(2);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.is_some(), true);
|
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.is_some(), false);
|
|
|
|
/// ```
|
2017-05-23 01:59:42 +00:00
|
|
|
///
|
|
|
|
/// [`Some`]: #variant.Some
|
2013-10-31 22:09:24 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn is_some(&self) -> bool {
|
2013-08-03 23:59:24 +00:00
|
|
|
match *self {
|
2013-10-31 22:09:24 +00:00
|
|
|
Some(_) => true,
|
2015-07-05 18:27:13 +00:00
|
|
|
None => false,
|
2013-08-03 23:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Returns `true` if the option is a [`None`] value.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = Some(2);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.is_none(), false);
|
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.is_none(), true);
|
|
|
|
/// ```
|
2017-05-23 01:59:42 +00:00
|
|
|
///
|
|
|
|
/// [`None`]: #variant.None
|
2013-10-31 22:09:24 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
!self.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Adapter for working with references
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-09 14:07:56 +00:00
|
|
|
/// Converts from `Option<T>` to `Option<&T>`.
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
2017-03-20 14:06:05 +00:00
|
|
|
/// Convert an `Option<`[`String`]`>` into an `Option<`[`usize`]`>`, preserving the original.
|
2016-09-09 14:07:56 +00:00
|
|
|
/// The [`map`] method takes the `self` argument by value, consuming the original,
|
2014-03-17 01:43:47 +00:00
|
|
|
/// so this technique uses `as_ref` to first take an `Option` to a reference
|
|
|
|
/// to the value inside the original.
|
|
|
|
///
|
2016-09-09 14:07:56 +00:00
|
|
|
/// [`map`]: enum.Option.html#method.map
|
2017-03-20 14:06:05 +00:00
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
|
|
|
/// [`usize`]: ../../std/primitive.usize.html
|
2016-09-09 14:07:56 +00:00
|
|
|
///
|
2014-03-17 01:43:47 +00:00
|
|
|
/// ```
|
2018-02-25 20:46:17 +00:00
|
|
|
/// let text: Option<String> = Some("Hello, world!".to_string());
|
2014-05-22 23:57:53 +00:00
|
|
|
/// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
|
2018-02-25 20:46:17 +00:00
|
|
|
/// // then consume *that* with `map`, leaving `text` on the stack.
|
|
|
|
/// let text_length: Option<usize> = text.as_ref().map(|s| s.len());
|
|
|
|
/// println!("still can print text: {:?}", text);
|
2014-03-17 01:43:47 +00:00
|
|
|
/// ```
|
2013-09-20 06:08:47 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-09-03 09:49:08 +00:00
|
|
|
pub fn as_ref(&self) -> Option<&T> {
|
2014-11-13 16:58:03 +00:00
|
|
|
match *self {
|
|
|
|
Some(ref x) => Some(x),
|
2015-07-05 18:27:13 +00:00
|
|
|
None => None,
|
2014-11-13 16:58:03 +00:00
|
|
|
}
|
2013-09-20 06:08:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 14:07:56 +00:00
|
|
|
/// Converts from `Option<T>` to `Option<&mut T>`.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let mut x = Some(2);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// match x.as_mut() {
|
2014-10-21 01:18:59 +00:00
|
|
|
/// Some(v) => *v = 42,
|
2014-09-16 15:20:03 +00:00
|
|
|
/// None => {},
|
|
|
|
/// }
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x, Some(42));
|
2014-09-16 15:20:03 +00:00
|
|
|
/// ```
|
2013-09-20 06:08:47 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-09-03 09:49:08 +00:00
|
|
|
pub fn as_mut(&mut self) -> Option<&mut T> {
|
2014-11-13 16:58:03 +00:00
|
|
|
match *self {
|
|
|
|
Some(ref mut x) => Some(x),
|
2015-07-05 18:27:13 +00:00
|
|
|
None => None,
|
2014-11-13 16:58:03 +00:00
|
|
|
}
|
2013-09-20 06:08:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 00:07:51 +00:00
|
|
|
/// Converts from `Option<T>` to `Option<PinMut<'_, T>>`
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "pin", issue = "49150")]
|
|
|
|
pub fn as_pin_mut<'a>(self: PinMut<'a, Self>) -> Option<PinMut<'a, T>> {
|
|
|
|
unsafe {
|
2018-06-23 11:32:53 +00:00
|
|
|
PinMut::get_mut_unchecked(self).as_mut().map(|x| PinMut::new_unchecked(x))
|
2018-05-23 00:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Getting to contained values
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Unwraps an option, yielding the content of a [`Some`].
|
2014-06-02 22:49:42 +00:00
|
|
|
///
|
2014-10-09 19:17:22 +00:00
|
|
|
/// # Panics
|
2014-06-02 22:49:42 +00:00
|
|
|
///
|
2017-03-20 14:06:05 +00:00
|
|
|
/// Panics if the value is a [`None`] with a custom panic message provided by
|
2014-06-02 22:49:42 +00:00
|
|
|
/// `msg`.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`Some`]: #variant.Some
|
2017-03-20 14:06:05 +00:00
|
|
|
/// [`None`]: #variant.None
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("value");
|
|
|
|
/// assert_eq!(x.expect("the world is ending"), "value");
|
|
|
|
/// ```
|
|
|
|
///
|
2015-03-26 20:30:33 +00:00
|
|
|
/// ```{.should_panic}
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let x: Option<&str> = None;
|
2015-05-31 08:49:22 +00:00
|
|
|
/// x.expect("the world is ending"); // panics with `the world is ending`
|
2014-09-16 15:20:03 +00:00
|
|
|
/// ```
|
2014-06-02 22:49:42 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-06-02 22:49:42 +00:00
|
|
|
pub fn expect(self, msg: &str) -> T {
|
|
|
|
match self {
|
|
|
|
Some(val) => val,
|
2016-01-22 17:19:00 +00:00
|
|
|
None => expect_failed(msg),
|
2014-06-02 22:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Moves the value `v` out of the `Option<T>` if it is [`Some(v)`].
|
2013-10-31 22:09:24 +00:00
|
|
|
///
|
2014-10-09 19:17:22 +00:00
|
|
|
/// In general, because this function may panic, its use is discouraged.
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Instead, prefer to use pattern matching and handle the [`None`]
|
2013-10-31 22:09:24 +00:00
|
|
|
/// case explicitly.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2016-09-19 22:55:44 +00:00
|
|
|
/// # Panics
|
|
|
|
///
|
2017-03-20 14:06:05 +00:00
|
|
|
/// Panics if the self value equals [`None`].
|
|
|
|
///
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`Some(v)`]: #variant.Some
|
2017-03-20 14:06:05 +00:00
|
|
|
/// [`None`]: #variant.None
|
2016-09-19 22:55:44 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("air");
|
|
|
|
/// assert_eq!(x.unwrap(), "air");
|
|
|
|
/// ```
|
|
|
|
///
|
2015-03-26 20:30:33 +00:00
|
|
|
/// ```{.should_panic}
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let x: Option<&str> = None;
|
|
|
|
/// assert_eq!(x.unwrap(), "air"); // fails
|
|
|
|
/// ```
|
2013-10-31 22:09:24 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn unwrap(self) -> T {
|
|
|
|
match self {
|
|
|
|
Some(val) => val,
|
2014-10-09 19:17:22 +00:00
|
|
|
None => panic!("called `Option::unwrap()` on a `None` value"),
|
2013-10-31 22:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 03:42:25 +00:00
|
|
|
/// Returns the contained value or a default.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2017-12-07 17:19:24 +00:00
|
|
|
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
|
|
|
|
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
|
|
|
|
/// which is lazily evaluated.
|
|
|
|
///
|
|
|
|
/// [`unwrap_or_else`]: #method.unwrap_or_else
|
2017-12-07 01:30:57 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// assert_eq!(Some("car").unwrap_or("bike"), "car");
|
|
|
|
/// assert_eq!(None.unwrap_or("bike"), "bike");
|
|
|
|
/// ```
|
2013-10-31 22:09:24 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn unwrap_or(self, def: T) -> T {
|
|
|
|
match self {
|
|
|
|
Some(x) => x,
|
2015-07-05 18:27:13 +00:00
|
|
|
None => def,
|
2013-10-31 22:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 03:42:25 +00:00
|
|
|
/// Returns the contained value or computes it from a closure.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-03-03 08:42:26 +00:00
|
|
|
/// let k = 10;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(Some(4).unwrap_or_else(|| 2 * k), 4);
|
|
|
|
/// assert_eq!(None.unwrap_or_else(|| 2 * k), 20);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// ```
|
2013-10-31 22:09:24 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 23:06:41 +00:00
|
|
|
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
|
2013-10-31 22:09:24 +00:00
|
|
|
match self {
|
|
|
|
Some(x) => x,
|
2015-07-05 18:27:13 +00:00
|
|
|
None => f(),
|
2013-10-31 22:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Transforming contained values
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-09 14:07:56 +00:00
|
|
|
/// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
2017-03-20 14:06:05 +00:00
|
|
|
/// Convert an `Option<`[`String`]`>` into an `Option<`[`usize`]`>`, consuming the original:
|
|
|
|
///
|
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
|
|
|
/// [`usize`]: ../../std/primitive.usize.html
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-05-31 07:37:52 +00:00
|
|
|
/// let maybe_some_string = Some(String::from("Hello, World!"));
|
|
|
|
/// // `Option::map` takes self *by value*, consuming `maybe_some_string`
|
|
|
|
/// let maybe_some_len = maybe_some_string.map(|s| s.len());
|
|
|
|
///
|
|
|
|
/// assert_eq!(maybe_some_len, Some(13));
|
2014-03-17 01:43:47 +00:00
|
|
|
/// ```
|
2013-09-20 06:08:47 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 23:06:41 +00:00
|
|
|
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
|
2014-11-13 16:58:03 +00:00
|
|
|
match self {
|
|
|
|
Some(x) => Some(f(x)),
|
2015-07-05 18:27:13 +00:00
|
|
|
None => None,
|
2014-11-13 16:58:03 +00:00
|
|
|
}
|
2013-09-20 06:08:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 08:16:49 +00:00
|
|
|
/// Applies a function to the contained value (if any),
|
2018-01-16 19:43:57 +00:00
|
|
|
/// or returns the provided default (if not).
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.map_or(42, |v| v.len()), 3);
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.map_or(42, |v| v.len()), 42);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// ```
|
2013-09-20 06:08:47 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-31 08:16:49 +00:00
|
|
|
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
|
2014-11-13 16:58:03 +00:00
|
|
|
match self {
|
|
|
|
Some(t) => f(t),
|
2015-05-31 08:16:49 +00:00
|
|
|
None => default,
|
2014-11-13 16:58:03 +00:00
|
|
|
}
|
2013-09-20 06:08:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 08:16:49 +00:00
|
|
|
/// Applies a function to the contained value (if any),
|
2018-01-16 19:43:57 +00:00
|
|
|
/// or computes a default (if not).
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let k = 21;
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// ```
|
2014-08-19 00:42:11 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-31 08:16:49 +00:00
|
|
|
pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
|
2014-11-13 16:58:03 +00:00
|
|
|
match self {
|
|
|
|
Some(t) => f(t),
|
2015-07-05 18:27:13 +00:00
|
|
|
None => default(),
|
2014-11-13 16:58:03 +00:00
|
|
|
}
|
2014-08-19 00:42:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
|
|
|
|
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
|
2016-09-09 14:07:56 +00:00
|
|
|
///
|
2017-12-07 01:30:57 +00:00
|
|
|
/// Arguments passed to `ok_or` are eagerly evaluated; if you are passing the
|
2017-12-07 17:19:24 +00:00
|
|
|
/// result of a function call, it is recommended to use [`ok_or_else`], which is
|
2017-12-07 01:30:57 +00:00
|
|
|
/// lazily evaluated.
|
|
|
|
///
|
2016-09-09 14:07:56 +00:00
|
|
|
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
|
|
|
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`Err(err)`]: ../../std/result/enum.Result.html#variant.Err
|
|
|
|
/// [`None`]: #variant.None
|
|
|
|
/// [`Some(v)`]: #variant.Some
|
2017-12-07 17:19:24 +00:00
|
|
|
/// [`ok_or_else`]: #method.ok_or_else
|
2014-09-23 06:18:30 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-23 06:18:30 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.ok_or(0), Ok("foo"));
|
2014-09-23 06:18:30 +00:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.ok_or(0), Err(0));
|
2014-09-23 06:18:30 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-03-27 00:47:13 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-23 06:18:30 +00:00
|
|
|
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
|
|
|
|
match self {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
None => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
|
|
|
|
/// [`Ok(v)`] and [`None`] to [`Err(err())`].
|
2016-09-09 14:07:56 +00:00
|
|
|
///
|
|
|
|
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
|
|
|
|
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`Err(err())`]: ../../std/result/enum.Result.html#variant.Err
|
|
|
|
/// [`None`]: #variant.None
|
|
|
|
/// [`Some(v)`]: #variant.Some
|
2014-09-23 06:18:30 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-23 06:18:30 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("foo");
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.ok_or_else(|| 0), Ok("foo"));
|
2014-09-23 06:18:30 +00:00
|
|
|
///
|
|
|
|
/// let x: Option<&str> = None;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.ok_or_else(|| 0), Err(0));
|
2014-09-23 06:18:30 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-03-27 00:47:13 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 23:06:41 +00:00
|
|
|
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
|
2014-09-23 06:18:30 +00:00
|
|
|
match self {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
None => Err(err()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Iterator constructors
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-17 03:42:25 +00:00
|
|
|
/// Returns an iterator over the possibly contained value.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let x = Some(4);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.iter().next(), Some(&4));
|
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.iter().next(), None);
|
|
|
|
/// ```
|
2013-06-10 21:45:59 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 18:34:23 +00:00
|
|
|
pub fn iter(&self) -> Iter<T> {
|
|
|
|
Iter { inner: Item { opt: self.as_ref() } }
|
2013-06-10 21:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 03:42:25 +00:00
|
|
|
/// Returns a mutable iterator over the possibly contained value.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let mut x = Some(4);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// match x.iter_mut().next() {
|
2015-09-16 08:17:38 +00:00
|
|
|
/// Some(v) => *v = 42,
|
2014-09-16 15:20:03 +00:00
|
|
|
/// None => {},
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(x, Some(42));
|
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let mut x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x.iter_mut().next(), None);
|
|
|
|
/// ```
|
2013-06-10 21:45:59 +00:00
|
|
|
#[inline]
|
2015-03-27 00:47:13 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 18:34:23 +00:00
|
|
|
pub fn iter_mut(&mut self) -> IterMut<T> {
|
|
|
|
IterMut { inner: Item { opt: self.as_mut() } }
|
2013-06-10 21:45:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Boolean operations on the values, eager and lazy
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2013-03-16 19:49:12 +00:00
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Returns [`None`] if the option is [`None`], otherwise returns `optb`.
|
|
|
|
///
|
|
|
|
/// [`None`]: #variant.None
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let x = Some(2);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let y: Option<&str> = None;
|
|
|
|
/// assert_eq!(x.and(y), None);
|
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let y = Some("foo");
|
|
|
|
/// assert_eq!(x.and(y), None);
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let x = Some(2);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let y = Some("foo");
|
|
|
|
/// assert_eq!(x.and(y), Some("foo"));
|
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let y: Option<&str> = None;
|
|
|
|
/// assert_eq!(x.and(y), None);
|
|
|
|
/// ```
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn and<U>(self, optb: Option<U>) -> Option<U> {
|
2013-03-16 19:49:12 +00:00
|
|
|
match self {
|
2013-09-11 16:00:27 +00:00
|
|
|
Some(_) => optb,
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Returns [`None`] if the option is [`None`], otherwise calls `f` with the
|
2013-09-13 01:54:02 +00:00
|
|
|
/// wrapped value and returns the result.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-02-09 11:44:19 +00:00
|
|
|
/// Some languages call this operation flatmap.
|
|
|
|
///
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`None`]: #variant.None
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-02-15 16:45:10 +00:00
|
|
|
/// fn sq(x: u32) -> Option<u32> { Some(x * x) }
|
|
|
|
/// fn nope(_: u32) -> Option<u32> { None }
|
2014-09-16 15:20:03 +00: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 16:00:27 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 23:06:41 +00:00
|
|
|
pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> {
|
2013-09-11 16:00:27 +00:00
|
|
|
match self {
|
2013-09-11 19:52:17 +00:00
|
|
|
Some(x) => f(x),
|
2013-09-11 16:00:27 +00:00
|
|
|
None => None,
|
2013-03-16 19:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 10:27:42 +00:00
|
|
|
/// Returns `None` if the option is `None`, otherwise calls `predicate`
|
|
|
|
/// with the wrapped value and returns:
|
|
|
|
///
|
|
|
|
/// - `Some(t)` if `predicate` returns `true` (where `t` is the wrapped
|
|
|
|
/// value), and
|
|
|
|
/// - `None` if `predicate` returns `false`.
|
|
|
|
///
|
|
|
|
/// This function works similar to `Iterator::filter()`. You can imagine
|
|
|
|
/// the `Option<T>` being an iterator over one or zero elements. `filter()`
|
|
|
|
/// lets you decide which elements to keep.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn is_even(n: &i32) -> bool {
|
|
|
|
/// n % 2 == 0
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(None.filter(is_even), None);
|
|
|
|
/// assert_eq!(Some(3).filter(is_even), None);
|
|
|
|
/// assert_eq!(Some(4).filter(is_even), Some(4));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2018-04-02 04:11:51 +00:00
|
|
|
#[stable(feature = "option_filter", since = "1.27.0")]
|
2017-10-03 10:27:42 +00:00
|
|
|
pub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Self {
|
2017-11-11 14:01:55 +00:00
|
|
|
if let Some(x) = self {
|
|
|
|
if predicate(&x) {
|
|
|
|
return Some(x)
|
2017-10-03 10:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-11 14:01:55 +00:00
|
|
|
None
|
2017-10-03 10:27:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 16:00:27 +00:00
|
|
|
/// Returns the option if it contains a value, otherwise returns `optb`.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2017-12-07 01:30:57 +00:00
|
|
|
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
|
2017-12-07 17:19:24 +00:00
|
|
|
/// result of a function call, it is recommended to use [`or_else`], which is
|
2017-12-07 01:30:57 +00:00
|
|
|
/// lazily evaluated.
|
|
|
|
///
|
2017-12-07 17:19:24 +00:00
|
|
|
/// [`or_else`]: #method.or_else
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let x = Some(2);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let y = None;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(x.or(y), Some(2));
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// let x = None;
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let y = Some(100);
|
|
|
|
/// assert_eq!(x.or(y), Some(100));
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let x = Some(2);
|
|
|
|
/// let y = Some(100);
|
|
|
|
/// assert_eq!(x.or(y), Some(2));
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let x: Option<u32> = None;
|
2014-09-16 15:20:03 +00:00
|
|
|
/// let y = None;
|
|
|
|
/// assert_eq!(x.or(y), None);
|
|
|
|
/// ```
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-05-31 22:17:22 +00:00
|
|
|
pub fn or(self, optb: Option<T>) -> Option<T> {
|
2013-03-16 19:49:12 +00:00
|
|
|
match self {
|
2013-09-11 16:00:27 +00:00
|
|
|
Some(_) => self,
|
2015-07-05 18:27:13 +00:00
|
|
|
None => optb,
|
2013-09-11 16:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 01:54:02 +00:00
|
|
|
/// Returns the option if it contains a value, otherwise calls `f` and
|
|
|
|
/// returns the result.
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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 16:00:27 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 23:06:41 +00:00
|
|
|
pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> {
|
2013-09-11 16:00:27 +00:00
|
|
|
match self {
|
|
|
|
Some(_) => self,
|
2015-07-05 18:27:13 +00:00
|
|
|
None => f(),
|
2013-09-11 16:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 01:30:38 +00:00
|
|
|
/// Returns [`Some`] if exactly one of `self`, `optb` is [`Some`], otherwise returns `None`.
|
|
|
|
///
|
|
|
|
/// [`Some`]: #variant.Some
|
|
|
|
/// [`None`]: #variant.None
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(option_xor)]
|
|
|
|
///
|
|
|
|
/// let x = Some(2);
|
|
|
|
/// let y: Option<u32> = None;
|
|
|
|
/// assert_eq!(x.xor(y), Some(2));
|
|
|
|
///
|
|
|
|
/// let x: Option<u32> = None;
|
|
|
|
/// let y = Some(2);
|
|
|
|
/// assert_eq!(x.xor(y), Some(2));
|
|
|
|
///
|
|
|
|
/// let x = Some(2);
|
|
|
|
/// let y = Some(2);
|
|
|
|
/// assert_eq!(x.xor(y), None);
|
|
|
|
///
|
|
|
|
/// let x: Option<u32> = None;
|
|
|
|
/// let y: Option<u32> = None;
|
|
|
|
/// assert_eq!(x.xor(y), None);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "option_xor", issue = "50512")]
|
|
|
|
pub fn xor(self, optb: Option<T>) -> Option<T> {
|
|
|
|
match (self, optb) {
|
|
|
|
(Some(a), None) => Some(a),
|
|
|
|
(None, Some(b)) => Some(b),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 06:13:26 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Entry-like operations to insert if None and return a reference
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Inserts `v` into the option if it is [`None`], then
|
2017-01-25 06:13:26 +00:00
|
|
|
/// returns a mutable reference to the contained value.
|
|
|
|
///
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`None`]: #variant.None
|
|
|
|
///
|
2017-01-25 06:13:26 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut x = None;
|
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let y: &mut u32 = x.get_or_insert(5);
|
|
|
|
/// assert_eq!(y, &5);
|
|
|
|
///
|
|
|
|
/// *y = 7;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(x, Some(7));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2017-07-20 22:40:23 +00:00
|
|
|
#[stable(feature = "option_entry", since = "1.20.0")]
|
2017-01-25 06:13:26 +00:00
|
|
|
pub fn get_or_insert(&mut self, v: T) -> &mut T {
|
|
|
|
match *self {
|
|
|
|
None => *self = Some(v),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
Some(ref mut v) => v,
|
2018-06-20 08:08:11 +00:00
|
|
|
None => unsafe { hint::unreachable_unchecked() },
|
2017-01-25 06:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Inserts a value computed from `f` into the option if it is [`None`], then
|
2017-01-25 06:13:26 +00:00
|
|
|
/// returns a mutable reference to the contained value.
|
|
|
|
///
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`None`]: #variant.None
|
|
|
|
///
|
2017-01-25 06:13:26 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut x = None;
|
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let y: &mut u32 = x.get_or_insert_with(|| 5);
|
|
|
|
/// assert_eq!(y, &5);
|
|
|
|
///
|
|
|
|
/// *y = 7;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(x, Some(7));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2017-07-20 22:40:23 +00:00
|
|
|
#[stable(feature = "option_entry", since = "1.20.0")]
|
2017-01-25 06:13:26 +00:00
|
|
|
pub fn get_or_insert_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
|
|
|
|
match *self {
|
|
|
|
None => *self = Some(f()),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
Some(ref mut v) => v,
|
2018-06-20 08:08:11 +00:00
|
|
|
None => unsafe { hint::unreachable_unchecked() },
|
2017-01-25 06:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// Misc
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Takes the value out of the option, leaving a [`None`] in its place.
|
|
|
|
///
|
|
|
|
/// [`None`]: #variant.None
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let mut x = Some(2);
|
2018-07-10 17:26:44 +00:00
|
|
|
/// let y = x.take();
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x, None);
|
2018-07-10 17:26:44 +00:00
|
|
|
/// assert_eq!(y, Some(2));
|
2014-09-16 15:20:03 +00:00
|
|
|
///
|
2015-02-15 16:45:10 +00:00
|
|
|
/// let mut x: Option<u32> = None;
|
2018-07-10 17:26:44 +00:00
|
|
|
/// let y = x.take();
|
2014-09-16 15:20:03 +00:00
|
|
|
/// assert_eq!(x, None);
|
2018-07-10 17:26:44 +00:00
|
|
|
/// assert_eq!(y, None);
|
2014-09-16 15:20:03 +00:00
|
|
|
/// ```
|
2013-10-31 22:09:24 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn take(&mut self) -> Option<T> {
|
2014-01-31 20:35:36 +00:00
|
|
|
mem::replace(self, None)
|
2013-10-31 22:09:24 +00:00
|
|
|
}
|
2018-07-04 19:54:45 +00:00
|
|
|
|
|
|
|
/// Replaces the actual value in the option by the value given in parameter,
|
|
|
|
/// returning the old value if present,
|
2018-07-09 12:52:32 +00:00
|
|
|
/// leaving a [`Some`] in its place without deinitializing either one.
|
2018-07-04 19:54:45 +00:00
|
|
|
///
|
|
|
|
/// [`Some`]: #variant.Some
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(option_replace)]
|
|
|
|
///
|
|
|
|
/// let mut x = Some(2);
|
|
|
|
/// let old = x.replace(5);
|
|
|
|
/// assert_eq!(x, Some(5));
|
|
|
|
/// assert_eq!(old, Some(2));
|
|
|
|
///
|
|
|
|
/// let mut x = None;
|
|
|
|
/// let old = x.replace(3);
|
|
|
|
/// assert_eq!(x, Some(3));
|
|
|
|
/// assert_eq!(old, None);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "option_replace", issue = "51998")]
|
|
|
|
pub fn replace(&mut self, value: T) -> Option<T> {
|
|
|
|
mem::replace(self, Some(value))
|
|
|
|
}
|
2013-10-31 22:09:24 +00:00
|
|
|
}
|
2013-04-10 20:11:35 +00:00
|
|
|
|
2015-03-27 00:47:13 +00:00
|
|
|
impl<'a, T: Clone> Option<&'a T> {
|
2015-10-07 13:30:51 +00:00
|
|
|
/// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
|
|
|
|
/// option.
|
2016-11-30 17:44:33 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = 12;
|
|
|
|
/// let opt_x = Some(&x);
|
|
|
|
/// assert_eq!(opt_x, Some(&12));
|
|
|
|
/// let cloned = opt_x.cloned();
|
|
|
|
/// assert_eq!(cloned, Some(12));
|
|
|
|
/// ```
|
2015-03-27 00:47:13 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-07 19:05:50 +00:00
|
|
|
pub fn cloned(self) -> Option<T> {
|
2015-03-27 00:47:13 +00:00
|
|
|
self.map(|t| t.clone())
|
2014-11-07 19:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 23:25:43 +00:00
|
|
|
impl<'a, T: Clone> Option<&'a mut T> {
|
|
|
|
/// Maps an `Option<&mut T>` to an `Option<T>` by cloning the contents of the
|
|
|
|
/// option.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut x = 12;
|
|
|
|
/// let opt_x = Some(&mut x);
|
|
|
|
/// assert_eq!(opt_x, Some(&mut 12));
|
|
|
|
/// let cloned = opt_x.cloned();
|
|
|
|
/// assert_eq!(cloned, Some(12));
|
|
|
|
/// ```
|
2018-03-06 18:30:42 +00:00
|
|
|
#[stable(since = "1.26.0", feature = "option_ref_mut_cloned")]
|
2017-08-06 23:25:43 +00:00
|
|
|
pub fn cloned(self) -> Option<T> {
|
|
|
|
self.map(|t| t.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
impl<T: Default> Option<T> {
|
2014-03-17 01:43:47 +00:00
|
|
|
/// Returns the contained value or a default
|
|
|
|
///
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Consumes the `self` argument then, if [`Some`], returns the contained
|
2018-01-16 19:43:57 +00:00
|
|
|
/// value, otherwise if [`None`], returns the [default value] for that
|
2014-03-17 01:43:47 +00:00
|
|
|
/// type.
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
|
|
|
/// Convert a string to an integer, turning poorly-formed strings
|
2017-05-23 01:59:42 +00:00
|
|
|
/// into 0 (the default value for integers). [`parse`] converts
|
|
|
|
/// a string to any other type that implements [`FromStr`], returning
|
|
|
|
/// [`None`] on error.
|
2014-03-17 01:43:47 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let good_year_from_input = "1909";
|
|
|
|
/// let bad_year_from_input = "190blarg";
|
2015-01-28 06:52:32 +00: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-17 01:43:47 +00:00
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// assert_eq!(1909, good_year);
|
|
|
|
/// assert_eq!(0, bad_year);
|
2014-03-17 01:43:47 +00:00
|
|
|
/// ```
|
2017-05-23 01:59:42 +00:00
|
|
|
///
|
|
|
|
/// [`Some`]: #variant.Some
|
|
|
|
/// [`None`]: #variant.None
|
2018-01-16 19:43:57 +00:00
|
|
|
/// [default value]: ../default/trait.Default.html#tymethod.default
|
2017-05-23 01:59:42 +00:00
|
|
|
/// [`parse`]: ../../std/primitive.str.html#method.parse
|
|
|
|
/// [`FromStr`]: ../../std/str/trait.FromStr.html
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-10-31 22:09:24 +00:00
|
|
|
pub fn unwrap_or_default(self) -> T {
|
2013-03-16 19:49:12 +00:00
|
|
|
match self {
|
2013-08-03 23:59:24 +00:00
|
|
|
Some(x) => x,
|
2015-07-05 18:27:13 +00:00
|
|
|
None => Default::default(),
|
2013-03-16 19:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 22:09:24 +00:00
|
|
|
}
|
2012-12-19 02:55:19 +00:00
|
|
|
|
2018-04-30 19:51:43 +00:00
|
|
|
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
2018-04-27 02:54:24 +00:00
|
|
|
impl<T: Deref> Option<T> {
|
|
|
|
/// Converts from `&Option<T>` to `Option<&T::Target>`.
|
|
|
|
///
|
|
|
|
/// Leaves the original Option in-place, creating a new one with a reference
|
|
|
|
/// to the original one, additionally coercing the contents via `Deref`.
|
|
|
|
pub fn deref(&self) -> Option<&T::Target> {
|
|
|
|
self.as_ref().map(|t| t.deref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:34:03 +00:00
|
|
|
impl<T, E> Option<Result<T, E>> {
|
|
|
|
/// Transposes an `Option` of a `Result` into a `Result` of an `Option`.
|
|
|
|
///
|
|
|
|
/// `None` will be mapped to `Ok(None)`.
|
|
|
|
/// `Some(Ok(_))` and `Some(Err(_))` will be mapped to `Ok(Some(_))` and `Err(_)`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(transpose_result)]
|
|
|
|
///
|
|
|
|
/// #[derive(Debug, Eq, PartialEq)]
|
|
|
|
/// struct SomeErr;
|
|
|
|
///
|
|
|
|
/// let x: Result<Option<i32>, SomeErr> = Ok(Some(5));
|
|
|
|
/// let y: Option<Result<i32, SomeErr>> = Some(Ok(5));
|
|
|
|
/// assert_eq!(x, y.transpose());
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "transpose_result", issue = "47338")]
|
|
|
|
pub fn transpose(self) -> Result<Option<T>, E> {
|
|
|
|
match self {
|
|
|
|
Some(Ok(x)) => Ok(Some(x)),
|
|
|
|
Some(Err(e)) => Err(e),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 17:19:00 +00:00
|
|
|
// This is a separate function to reduce the code size of .expect() itself.
|
|
|
|
#[inline(never)]
|
|
|
|
#[cold]
|
|
|
|
fn expect_failed(msg: &str) -> ! {
|
|
|
|
panic!("{}", msg)
|
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Trait implementations
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-09-10 02:29:11 +00:00
|
|
|
impl<T> Default for Option<T> {
|
2017-05-23 01:59:42 +00:00
|
|
|
/// Returns [`None`].
|
|
|
|
///
|
|
|
|
/// [`None`]: #variant.None
|
2013-09-12 04:49:25 +00:00
|
|
|
#[inline]
|
2013-09-10 02:29:11 +00:00
|
|
|
fn default() -> Option<T> { None }
|
|
|
|
}
|
|
|
|
|
2015-04-17 21:31:30 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T> IntoIterator for Option<T> {
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = IntoIter<T>;
|
|
|
|
|
|
|
|
/// Returns a consuming iterator over the possibly contained value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let x = Some("string");
|
|
|
|
/// let v: Vec<&str> = x.into_iter().collect();
|
|
|
|
/// assert_eq!(v, ["string"]);
|
|
|
|
///
|
|
|
|
/// let x = None;
|
|
|
|
/// let v: Vec<&str> = x.into_iter().collect();
|
|
|
|
/// assert!(v.is_empty());
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
fn into_iter(self) -> IntoIter<T> {
|
|
|
|
IntoIter { inner: Item { opt: self } }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 06:37:40 +00:00
|
|
|
#[stable(since = "1.4.0", feature = "option_iter")]
|
|
|
|
impl<'a, T> IntoIterator for &'a Option<T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = Iter<'a, T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Iter<'a, T> {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(since = "1.4.0", feature = "option_iter")]
|
|
|
|
impl<'a, T> IntoIterator for &'a mut Option<T> {
|
|
|
|
type Item = &'a mut T;
|
|
|
|
type IntoIter = IterMut<'a, T>;
|
|
|
|
|
2017-08-01 12:03:03 +00:00
|
|
|
fn into_iter(self) -> IterMut<'a, T> {
|
2015-08-27 06:37:40 +00:00
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 19:17:39 +00:00
|
|
|
#[stable(since = "1.12.0", feature = "option_from")]
|
|
|
|
impl<T> From<T> for Option<T> {
|
|
|
|
fn from(val: T) -> Option<T> {
|
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2014-12-14 18:34:23 +00:00
|
|
|
// The Option Iterators
|
2013-10-31 22:09:24 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2013-01-03 23:36:23 +00:00
|
|
|
|
2016-03-05 02:49:43 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2014-12-14 18:34:23 +00:00
|
|
|
struct Item<A> {
|
2014-03-27 22:09:47 +00:00
|
|
|
opt: Option<A>
|
2013-06-10 21:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<A> Iterator for Item<A> {
|
|
|
|
type Item = A;
|
|
|
|
|
2013-08-03 19:34:00 +00:00
|
|
|
#[inline]
|
2013-08-03 17:40:20 +00:00
|
|
|
fn next(&mut self) -> Option<A> {
|
2013-08-03 19:34:00 +00:00
|
|
|
self.opt.take()
|
2013-06-10 21:45:59 +00:00
|
|
|
}
|
2013-07-08 22:31:26 +00:00
|
|
|
|
2013-08-03 19:34:00 +00:00
|
|
|
#[inline]
|
2015-02-13 19:36:59 +00:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2013-07-08 22:31:26 +00:00
|
|
|
match self.opt {
|
|
|
|
Some(_) => (1, Some(1)),
|
|
|
|
None => (0, Some(0)),
|
|
|
|
}
|
|
|
|
}
|
2013-06-10 21:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<A> DoubleEndedIterator for Item<A> {
|
2013-08-11 02:21:31 +00:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
self.opt.take()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<A> ExactSizeIterator for Item<A> {}
|
2016-08-13 18:42:36 +00:00
|
|
|
impl<A> FusedIterator for Item<A> {}
|
2016-10-20 12:34:34 +00:00
|
|
|
unsafe impl<A> TrustedLen for Item<A> {}
|
2013-09-01 16:20:24 +00:00
|
|
|
|
2017-04-01 16:47:48 +00:00
|
|
|
/// An iterator over a reference to the [`Some`] variant of an [`Option`].
|
|
|
|
///
|
2017-04-02 00:40:34 +00:00
|
|
|
/// The iterator yields one value if the [`Option`] is a [`Some`], otherwise none.
|
2017-04-01 16:47:48 +00:00
|
|
|
///
|
2017-04-02 00:40:34 +00:00
|
|
|
/// This `struct` is created by the [`Option::iter`] function.
|
2016-09-09 14:07:56 +00:00
|
|
|
///
|
|
|
|
/// [`Option`]: enum.Option.html
|
2017-04-01 16:47:48 +00:00
|
|
|
/// [`Some`]: enum.Option.html#variant.Some
|
|
|
|
/// [`Option::iter`]: enum.Option.html#method.iter
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-03-05 02:49:43 +00:00
|
|
|
#[derive(Debug)]
|
2014-12-14 18:34:23 +00:00
|
|
|
pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<'a, A> Iterator for Iter<'a, A> {
|
|
|
|
type Item = &'a A;
|
|
|
|
|
2014-12-14 18:34:23 +00:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a A> { self.inner.next() }
|
|
|
|
#[inline]
|
2015-02-13 19:36:59 +00:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2014-12-14 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
|
2014-12-14 18:34:23 +00:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a A> { self.inner.next_back() }
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
|
2014-12-14 18:34:23 +00:00
|
|
|
|
2018-03-03 13:15:28 +00:00
|
|
|
#[stable(feature = "fused", since = "1.26.0")]
|
2016-08-13 18:42:36 +00:00
|
|
|
impl<'a, A> FusedIterator for Iter<'a, A> {}
|
|
|
|
|
2016-11-03 23:24:59 +00:00
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
2016-10-20 12:34:34 +00:00
|
|
|
unsafe impl<'a, A> TrustedLen for Iter<'a, A> {}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 18:34:23 +00:00
|
|
|
impl<'a, A> Clone for Iter<'a, A> {
|
|
|
|
fn clone(&self) -> Iter<'a, A> {
|
|
|
|
Iter { inner: self.inner.clone() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 16:39:10 +00:00
|
|
|
/// An iterator over a mutable reference to the [`Some`] variant of an [`Option`].
|
|
|
|
///
|
2017-04-02 00:40:34 +00:00
|
|
|
/// The iterator yields one value if the [`Option`] is a [`Some`], otherwise none.
|
2017-04-01 16:39:10 +00:00
|
|
|
///
|
2017-04-02 00:40:34 +00:00
|
|
|
/// This `struct` is created by the [`Option::iter_mut`] function.
|
2016-09-09 14:07:56 +00:00
|
|
|
///
|
|
|
|
/// [`Option`]: enum.Option.html
|
2017-04-01 16:39:10 +00:00
|
|
|
/// [`Some`]: enum.Option.html#variant.Some
|
|
|
|
/// [`Option::iter_mut`]: enum.Option.html#method.iter_mut
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-03-05 02:49:43 +00:00
|
|
|
#[derive(Debug)]
|
2014-12-14 18:34:23 +00:00
|
|
|
pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<'a, A> Iterator for IterMut<'a, A> {
|
|
|
|
type Item = &'a mut A;
|
|
|
|
|
2014-12-14 18:34:23 +00:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
|
|
|
|
#[inline]
|
2015-02-13 19:36:59 +00:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2014-12-14 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
|
2014-12-14 18:34:23 +00:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a mut A> { self.inner.next_back() }
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
|
2014-12-14 18:34:23 +00:00
|
|
|
|
2018-03-03 13:15:28 +00:00
|
|
|
#[stable(feature = "fused", since = "1.26.0")]
|
2016-08-13 18:42:36 +00:00
|
|
|
impl<'a, A> FusedIterator for IterMut<'a, A> {}
|
2016-11-03 23:24:59 +00:00
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
2016-10-20 12:34:34 +00:00
|
|
|
unsafe impl<'a, A> TrustedLen for IterMut<'a, A> {}
|
2016-08-13 18:42:36 +00:00
|
|
|
|
2017-04-01 16:28:30 +00:00
|
|
|
/// An iterator over the value in [`Some`] variant of an [`Option`].
|
|
|
|
///
|
2017-04-02 00:40:34 +00:00
|
|
|
/// The iterator yields one value if the [`Option`] is a [`Some`], otherwise none.
|
2017-04-01 16:28:30 +00:00
|
|
|
///
|
2017-04-02 00:40:34 +00:00
|
|
|
/// This `struct` is created by the [`Option::into_iter`] function.
|
2016-09-09 14:07:56 +00:00
|
|
|
///
|
|
|
|
/// [`Option`]: enum.Option.html
|
2017-04-01 16:28:30 +00:00
|
|
|
/// [`Some`]: enum.Option.html#variant.Some
|
|
|
|
/// [`Option::into_iter`]: enum.Option.html#method.into_iter
|
2016-03-05 02:49:43 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-14 18:34:23 +00:00
|
|
|
pub struct IntoIter<A> { inner: Item<A> }
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<A> Iterator for IntoIter<A> {
|
|
|
|
type Item = A;
|
|
|
|
|
2014-12-14 18:34:23 +00:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> { self.inner.next() }
|
|
|
|
#[inline]
|
2015-02-13 19:36:59 +00:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2014-12-14 18:34:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<A> DoubleEndedIterator for IntoIter<A> {
|
2014-12-14 18:34:23 +00:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> { self.inner.next_back() }
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 21:18:41 +00:00
|
|
|
impl<A> ExactSizeIterator for IntoIter<A> {}
|
2014-12-14 18:34:23 +00:00
|
|
|
|
2018-03-03 13:15:28 +00:00
|
|
|
#[stable(feature = "fused", since = "1.26.0")]
|
2016-08-13 18:42:36 +00:00
|
|
|
impl<A> FusedIterator for IntoIter<A> {}
|
|
|
|
|
2016-11-03 23:24:59 +00:00
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
2016-10-20 12:34:34 +00:00
|
|
|
unsafe impl<A> TrustedLen for IntoIter<A> {}
|
|
|
|
|
2013-12-21 04:56:07 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2014-11-15 04:39:41 +00:00
|
|
|
// FromIterator
|
2013-12-21 04:56:07 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-08-19 00:42:11 +00:00
|
|
|
impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
|
2017-05-23 01:59:42 +00:00
|
|
|
/// 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
|
2014-08-19 00:42:11 +00:00
|
|
|
/// container with the values of each `Option` is returned.
|
|
|
|
///
|
|
|
|
/// Here is an example which increments every integer in a vector,
|
|
|
|
/// checking for overflow:
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
/// ```
|
2015-02-13 19:36:59 +00:00
|
|
|
/// use std::u16;
|
2014-08-19 00:42:11 +00:00
|
|
|
///
|
2016-10-29 21:54:04 +00:00
|
|
|
/// let v = vec![1, 2];
|
2015-02-13 19:36:59 +00:00
|
|
|
/// let res: Option<Vec<u16>> = v.iter().map(|&x: &u16|
|
|
|
|
/// if x == u16::MAX { None }
|
2014-08-19 00:42:11 +00:00
|
|
|
/// else { Some(x + 1) }
|
|
|
|
/// ).collect();
|
2016-10-29 21:54:04 +00:00
|
|
|
/// assert!(res == Some(vec![2, 3]));
|
2014-08-19 00:42:11 +00:00
|
|
|
/// ```
|
2017-05-23 01:59:42 +00:00
|
|
|
///
|
|
|
|
/// [`Iterator`]: ../iter/trait.Iterator.html
|
|
|
|
/// [`None`]: enum.Option.html#variant.None
|
2014-08-19 00:42:11 +00:00
|
|
|
#[inline]
|
2015-02-18 18:06:21 +00:00
|
|
|
fn from_iter<I: IntoIterator<Item=Option<A>>>(iter: I) -> Option<V> {
|
2014-08-19 00:42:11 +00: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 21:18:41 +00:00
|
|
|
impl<T, Iter: Iterator<Item=Option<T>>> Iterator for Adapter<Iter> {
|
|
|
|
type Item = T;
|
|
|
|
|
2014-08-19 00:42:11 +00: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 23:27:54 +00:00
|
|
|
}
|
2013-12-21 04:56:07 +00:00
|
|
|
}
|
2018-03-20 09:33:59 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2018-03-31 05:13:02 +00:00
|
|
|
if self.found_none {
|
|
|
|
(0, Some(0))
|
|
|
|
} else {
|
|
|
|
let (_, upper) = self.iter.size_hint();
|
|
|
|
(0, upper)
|
|
|
|
}
|
2018-03-20 09:33:59 +00:00
|
|
|
}
|
2013-12-21 04:56:07 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 18:06:21 +00:00
|
|
|
let mut adapter = Adapter { iter: iter.into_iter(), found_none: false };
|
2014-08-19 00:42:11 +00:00
|
|
|
let v: V = FromIterator::from_iter(adapter.by_ref());
|
2013-12-21 04:56:07 +00:00
|
|
|
|
2014-08-19 00:42:11 +00:00
|
|
|
if adapter.found_none {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(v)
|
|
|
|
}
|
2013-12-21 04:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-08 03:51:45 +00:00
|
|
|
|
2017-06-18 18:07:09 +00:00
|
|
|
/// The error type that results from applying the try operator (`?`) to a `None` value. If you wish
|
|
|
|
/// to allow `x?` (where `x` is an `Option<T>`) to be converted into your error type, you can
|
|
|
|
/// implement `impl From<NoneError>` for `YourErrorType`. In that case, `x?` within a function that
|
|
|
|
/// returns `Result<_, YourErrorType>` will translate a `None` value into an `Err` result.
|
2017-06-08 03:51:45 +00:00
|
|
|
#[unstable(feature = "try_trait", issue = "42327")]
|
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
2017-06-18 18:07:09 +00:00
|
|
|
pub struct NoneError;
|
2017-06-08 03:51:45 +00:00
|
|
|
|
|
|
|
#[unstable(feature = "try_trait", issue = "42327")]
|
|
|
|
impl<T> ops::Try for Option<T> {
|
|
|
|
type Ok = T;
|
2017-06-18 18:07:09 +00:00
|
|
|
type Error = NoneError;
|
2017-06-08 03:51:45 +00:00
|
|
|
|
2017-06-18 18:07:09 +00:00
|
|
|
fn into_result(self) -> Result<T, NoneError> {
|
|
|
|
self.ok_or(NoneError)
|
2017-06-08 03:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_ok(v: T) -> Self {
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
|
2017-06-18 18:07:09 +00:00
|
|
|
fn from_error(_: NoneError) -> Self {
|
2017-06-08 03:51:45 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|