Skip to content

Commit 3d421ad

Browse files
committed
Properly document tuples
Fixes #29339
1 parent 6630a08 commit 3d421ad

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

src/libcore/tuple.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! A finite heterogeneous sequence, `(T, U, ..)`
12-
//!
13-
//! To access a single element of a tuple one can use the `.0`
14-
//! field access syntax.
15-
//!
16-
//! Indexing starts from zero, so `.0` returns first value, `.1`
17-
//! returns second value, and so on. In general, a tuple with *N*
18-
//! elements has field accessors from 0 to *N* - 1.
19-
//!
20-
//! If every type inside a tuple implements one of the following
21-
//! traits, then a tuple itself also implements it.
22-
//!
23-
//! * `Clone`
24-
//! * `PartialEq`
25-
//! * `Eq`
26-
//! * `PartialOrd`
27-
//! * `Ord`
28-
//! * `Default`
11+
// See src/libstd/primitive_docs.rs for documentation.
2912

3013
use clone::Clone;
3114
use cmp::*;

src/libstd/primitive_docs.rs

+65-27
Original file line numberDiff line numberDiff line change
@@ -357,50 +357,88 @@ mod prim_str { }
357357
//
358358
/// A finite heterogeneous sequence, `(T, U, ..)`.
359359
///
360-
/// To access the _N_-th element of a tuple one can use `N` itself
361-
/// as a field of the tuple.
360+
/// Let's cover each of those in turn:
362361
///
363-
/// Indexing starts from zero, so `0` returns first value, `1`
364-
/// returns second value, and so on. In general, a tuple with _S_
365-
/// elements provides aforementioned fields from `0` to `S-1`.
362+
/// Tuples are *finite*. In other words, a tuple has a length. Here's a tuple
363+
/// of length `3`:
364+
///
365+
/// ```
366+
/// ("hello", 5, 'c');
367+
/// ```
368+
///
369+
/// Tuples are *heterogeneous*. This means that each element of the tuple can
370+
/// have a different type. In that tuple above, it has the type:
371+
///
372+
/// ```rust,ignore
373+
/// (&'static str, i32, char)
374+
/// ```
375+
///
376+
/// Tuples are a *sequence*. This means that they can be accessed by position;
377+
/// this is called 'tuple indexing', and it looks like this:
378+
///
379+
/// ```rust
380+
/// let tuple = ("hello", 5, 'c');
381+
///
382+
/// assert_eq!(tuple.0, "hello");
383+
/// assert_eq!(tuple.1, 5);
384+
/// assert_eq!(tuple.2, 'c');
385+
/// ```
386+
///
387+
/// For more about tuples, see [the book](../../book/primitive-types.html#tuples).
388+
///
389+
/// # Trait implementations
366390
///
367391
/// If every type inside a tuple implements one of the following
368392
/// traits, then a tuple itself also implements it.
369393
///
370-
/// * `Clone`
371-
/// * `PartialEq`
372-
/// * `Eq`
373-
/// * `PartialOrd`
374-
/// * `Ord`
375-
/// * `Debug`
376-
/// * `Default`
377-
/// * `Hash`
394+
/// * [`Clone`]
395+
/// * [`PartialEq`]
396+
/// * [`Eq`]
397+
/// * [`PartialOrd`]
398+
/// * [`Ord`]
399+
/// * [`Debug`]
400+
/// * [`Default`]
401+
/// * [`Hash`]
402+
///
403+
/// [`Clone`]: ../clone/trait.Clone.html
404+
/// [`PartialEq`]: ../cmp/trait.PartialEq.html
405+
/// [`Eq`]: ../cmp/trait.Eq.html
406+
/// [`PartialOrd`]: ../cmp/trait.PartialOrd.html
407+
/// [`Ord`]: ../cmp/trait.Ord.html
408+
/// [`Debug`]: ../fmt/trait.Debug.html
409+
/// [`Default`]: ../default/trait.Default.html
410+
/// [`Hash`]: ../hash/trait.Hash.html
378411
///
379412
/// # Examples
380413
///
381-
/// Accessing elements of a tuple at specified indices:
414+
/// Basic usage:
382415
///
383416
/// ```
384-
/// let x = ("colorless", "green", "ideas", "sleep", "furiously");
385-
/// assert_eq!(x.3, "sleep");
417+
/// let tuple = ("hello", 5, 'c');
386418
///
387-
/// let v = (3, 3);
388-
/// let u = (1, -5);
389-
/// assert_eq!(v.0 * u.0 + v.1 * u.1, -12);
419+
/// assert_eq!(tuple.0, "hello");
390420
/// ```
391421
///
392-
/// Using traits implemented for tuples:
422+
/// Tuples are often used as a return type when you want to return more than
423+
/// one value:
393424
///
394425
/// ```
395-
/// let a = (1, 2);
396-
/// let b = (3, 4);
397-
/// assert!(a != b);
426+
/// fn calculate_point() -> (i32, i32) {
427+
/// // Don't do a calculation, that's not the point of the example
428+
/// (4, 5)
429+
/// }
430+
///
431+
/// let point = calculate_point();
432+
///
433+
/// assert_eq!(point.0, 4);
434+
/// assert_eq!(point.1, 5);
435+
///
436+
/// // Combining this with patterns can be nicer.
398437
///
399-
/// let c = b.clone();
400-
/// assert!(b == c);
438+
/// let (x, y) = calcualte_point();
401439
///
402-
/// let d : (u32, f32) = Default::default();
403-
/// assert_eq!(d, (0, 0.0f32));
440+
/// assert_eq!(x, 4);
441+
/// assert_eq!(y, 5);
404442
/// ```
405443
///
406444
mod prim_tuple { }

0 commit comments

Comments
 (0)