diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 2dd8bf67220ab..7cea0853b2970 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -10,9 +10,9 @@ //! Optional values //! -//! Type `Option` represents an optional value: every `Option` +//! 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 +//! does not. `Option` types are very common in Rust code, as //! they have a number of uses: //! //! * Initial values @@ -51,7 +51,7 @@ //! ``` //! // -// FIXME: Show how `Option` is used in practice, with lots of methods +// FIXME: Show how `Option` is used in practice, with lots of methods // //! # Options and pointers ("nullable" pointers) //! @@ -59,7 +59,7 @@ //! no "null" pointers. Instead, Rust has *optional* pointers, like //! the optional owned box, `Option>`. //! -//! The following example uses `Option` to create an optional box of +//! The following example uses `Option` to create an optional box of //! `i32`. Notice that in order to use the inner `i32` value first the //! `check_optional` function needs to use pattern matching to //! determine whether the box has a value (i.e. it is `Some(...)`) or @@ -80,14 +80,14 @@ //! } //! ``` //! -//! This usage of `Option` to create safe nullable pointers is so +//! This usage of `Option` to create safe nullable pointers is so //! common that Rust does special optimizations to make the //! representation of `Option>` a single pointer. Optional pointers //! in Rust are stored as efficiently as any other pointer type. //! //! # Examples //! -//! Basic pattern matching on `Option`: +//! Basic pattern matching on `Option`: //! //! ``` //! let msg = Some("howdy"); @@ -98,7 +98,7 @@ //! None => () //! } //! -//! // Remove the contained string, destroying the Option +//! // Remove the contained string, destroying the `Option` //! let unwrapped_msg = match msg { //! Some(m) => m, //! None => "default message" @@ -160,9 +160,9 @@ use slice; // 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`. +// which basically means it must be `Option`. -/// The `Option` type. See [the module level documentation](../index.html) for more. +/// The `Option` type. See [the module level documentation](../index.html) for more. #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub enum Option { @@ -183,7 +183,7 @@ impl Option { // Querying the contained values ///////////////////////////////////////////////////////////////////////// - /// Returns `true` if the option is a `Some` value + /// Returns `true` if the `Option` is a `Some(v)` value. /// /// # Examples /// @@ -203,7 +203,7 @@ impl Option { } } - /// Returns `true` if the option is a `None` value + /// Returns `true` if the `Option` is a `None` value. /// /// # Examples /// @@ -224,13 +224,13 @@ impl Option { // Adapter for working with references ///////////////////////////////////////////////////////////////////////// - /// Convert from `Option` to `Option<&T>` + /// Convert from `Option` to `Option<&T>`. /// /// # Examples /// /// Convert an `Option` into an `Option`, preserving the original. /// The `map` method takes the `self` argument by value, consuming the original, - /// so this technique uses `as_ref` to first take an `Option` to a reference + /// so this technique uses `as_ref` to first take an `Option` to a reference /// to the value inside the original. /// /// ``` @@ -249,7 +249,7 @@ impl Option { } } - /// Convert from `Option` to `Option<&mut T>` + /// Convert from `Option` to `Option<&mut T>`. /// /// # Examples /// @@ -270,7 +270,7 @@ impl Option { } } - /// Convert from `Option` to `&mut [T]` (without copying) + /// Convert from `Option` to `&mut [T]` (without copying). /// /// # Examples /// @@ -304,7 +304,8 @@ impl Option { // Getting to contained values ///////////////////////////////////////////////////////////////////////// - /// Unwraps an option, yielding the content of a `Some` + /// If the `Option` is `Some(v)`, moves the value `v` out of the `Option`. + /// Otherwise panics with the provided message `msg`. /// /// # Panics /// @@ -363,7 +364,8 @@ impl Option { } } - /// Returns the contained value or a default. + /// Moves the value `v` out of the `Option` if the content of the + /// `Option` is a `Some(v)` or returns the value of the argument `def`. /// /// # Examples /// @@ -380,7 +382,8 @@ impl Option { } } - /// Returns the contained value or computes it from a closure. + /// Moves the value `v` out of the `Option` if the content of the + /// `Option` is a `Some(v)` or computes it from a closure. /// /// # Examples /// @@ -402,7 +405,10 @@ impl Option { // Transforming contained values ///////////////////////////////////////////////////////////////////////// - /// Maps an `Option` to `Option` by applying a function to a contained value + /// Transforms the `Option` into a new `Option`. + /// If the `Option` is `None` then `f` is not invoked and `None` is returned. + /// Otherwise the result `u` of evaluating `f(v)` is returned as a `Some(u)` + /// where `v` is the value of the content `Some(v)` on the `Option`. /// /// # Examples /// @@ -422,7 +428,10 @@ impl Option { } } - /// Applies a function to the contained value or returns a default. + /// Transforms the `Option` into a new `Option`. + /// If the `Option` is `None` then `f` is not invoked and the `def` value + /// is returned. Otherwise the result of evaluating `f(v)` is returned where + /// `v` is the value of the content `Some(v)` on the `Option`. /// /// # Examples /// @@ -442,7 +451,10 @@ impl Option { } } - /// Applies a function to the contained value or computes a default. + /// Transforms the `Option` into a value of of type `U`. + /// If the `Option` is `None` then the result of evaluating `def()` is returned. + /// Otherwise the result of evaluating `f(v)` is returned where `v` is the + /// value of the content `Some(v)` on the `Option`. /// /// # Examples /// @@ -602,8 +614,8 @@ impl Option { } } - /// Returns `None` if the option is `None`, otherwise calls `f` with the - /// wrapped value and returns the result. + /// Returns `None` if the `Option` is `None`, otherwise calls `f` with the + /// wrapped value `v` of the `Some(v)` and returns the result. /// /// Some languages call this operation flatmap. /// @@ -627,7 +639,8 @@ impl Option { } } - /// Returns the option if it contains a value, otherwise returns `optb`. + /// Returns the `Option` if it contains a `Some(v)` value, otherwise + /// returns the argument value `optb`. /// /// # Examples /// @@ -657,8 +670,8 @@ impl Option { } } - /// Returns the option if it contains a value, otherwise calls `f` and - /// returns the result. + /// Returns the `Option` if it contains a `Some(v)` value, otherwise calls + /// `f` and returns the result. /// /// # Examples /// @@ -683,17 +696,18 @@ impl Option { // Misc ///////////////////////////////////////////////////////////////////////// - /// Takes the value out of the option, leaving a `None` in its place. + /// Returns the current `Option` object and replaces its memory location + /// with `None`. /// /// # Examples /// /// ``` /// let mut x = Some(2); - /// x.take(); + /// assert_eq!(x.take(), Some(2)); /// assert_eq!(x, None); /// /// let mut x: Option = None; - /// x.take(); + /// assert_eq!(x.take(), None); /// assert_eq!(x, None); /// ``` #[inline] @@ -892,7 +906,7 @@ impl ExactSizeIterator for IntoIter {} impl> FromIterator> for Option { /// 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. + /// container with the values of each `Option` is returned. /// /// Here is an example which increments every integer in a vector, /// checking for overflow: