1
Fork 0

Properly document tuples

Fixes #29339
This commit is contained in:
Steve Klabnik 2016-02-09 12:54:53 -05:00
parent 6630a08195
commit 4ebc47bad2
2 changed files with 66 additions and 45 deletions

View file

@ -8,24 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
//! A finite heterogeneous sequence, `(T, U, ..)` // See src/libstd/primitive_docs.rs for documentation.
//!
//! To access a single element of a tuple one can use the `.0`
//! field access syntax.
//!
//! Indexing starts from zero, so `.0` returns first value, `.1`
//! returns second value, and so on. In general, a tuple with *N*
//! elements has field accessors from 0 to *N* - 1.
//!
//! If every type inside a tuple implements one of the following
//! traits, then a tuple itself also implements it.
//!
//! * `Clone`
//! * `PartialEq`
//! * `Eq`
//! * `PartialOrd`
//! * `Ord`
//! * `Default`
use clone::Clone; use clone::Clone;
use cmp::*; use cmp::*;

View file

@ -357,50 +357,88 @@ mod prim_str { }
// //
/// A finite heterogeneous sequence, `(T, U, ..)`. /// A finite heterogeneous sequence, `(T, U, ..)`.
/// ///
/// To access the _N_-th element of a tuple one can use `N` itself /// Let's cover each of those in turn:
/// as a field of the tuple.
/// ///
/// Indexing starts from zero, so `0` returns first value, `1` /// Tuples are *finite*. In other words, a tuple has a length. Here's a tuple
/// returns second value, and so on. In general, a tuple with _S_ /// of length `3`:
/// elements provides aforementioned fields from `0` to `S-1`. ///
/// ```
/// ("hello", 5, 'c');
/// ```
///
/// Tuples are *heterogeneous*. This means that each element of the tuple can
/// have a different type. In that tuple above, it has the type:
///
/// ```rust,ignore
/// (&'static str, i32, char)
/// ```
///
/// Tuples are a *sequence*. This means that they can be accessed by position;
/// this is called 'tuple indexing', and it looks like this:
///
/// ```rust
/// let tuple = ("hello", 5, 'c');
///
/// assert_eq!(tuple.0, "hello");
/// assert_eq!(tuple.1, 5);
/// assert_eq!(tuple.2, 'c');
/// ```
///
/// For more about tuples, see [the book](../../book/primitive-types.html#tuples).
///
/// # Trait implementations
/// ///
/// If every type inside a tuple implements one of the following /// If every type inside a tuple implements one of the following
/// traits, then a tuple itself also implements it. /// traits, then a tuple itself also implements it.
/// ///
/// * `Clone` /// * [`Clone`]
/// * `PartialEq` /// * [`PartialEq`]
/// * `Eq` /// * [`Eq`]
/// * `PartialOrd` /// * [`PartialOrd`]
/// * `Ord` /// * [`Ord`]
/// * `Debug` /// * [`Debug`]
/// * `Default` /// * [`Default`]
/// * `Hash` /// * [`Hash`]
///
/// [`Clone`]: ../clone/trait.Clone.html
/// [`PartialEq`]: ../cmp/trait.PartialEq.html
/// [`Eq`]: ../cmp/trait.Eq.html
/// [`PartialOrd`]: ../cmp/trait.PartialOrd.html
/// [`Ord`]: ../cmp/trait.Ord.html
/// [`Debug`]: ../fmt/trait.Debug.html
/// [`Default`]: ../default/trait.Default.html
/// [`Hash`]: ../hash/trait.Hash.html
/// ///
/// # Examples /// # Examples
/// ///
/// Accessing elements of a tuple at specified indices: /// Basic usage:
/// ///
/// ``` /// ```
/// let x = ("colorless", "green", "ideas", "sleep", "furiously"); /// let tuple = ("hello", 5, 'c');
/// assert_eq!(x.3, "sleep");
/// ///
/// let v = (3, 3); /// assert_eq!(tuple.0, "hello");
/// let u = (1, -5);
/// assert_eq!(v.0 * u.0 + v.1 * u.1, -12);
/// ``` /// ```
/// ///
/// Using traits implemented for tuples: /// Tuples are often used as a return type when you want to return more than
/// one value:
/// ///
/// ``` /// ```
/// let a = (1, 2); /// fn calculate_point() -> (i32, i32) {
/// let b = (3, 4); /// // Don't do a calculation, that's not the point of the example
/// assert!(a != b); /// (4, 5)
/// }
/// ///
/// let c = b.clone(); /// let point = calculate_point();
/// assert!(b == c);
/// ///
/// let d : (u32, f32) = Default::default(); /// assert_eq!(point.0, 4);
/// assert_eq!(d, (0, 0.0f32)); /// assert_eq!(point.1, 5);
///
/// // Combining this with patterns can be nicer.
///
/// let (x, y) = calculate_point();
///
/// assert_eq!(x, 4);
/// assert_eq!(y, 5);
/// ``` /// ```
/// ///
mod prim_tuple { } mod prim_tuple { }