Skip to content

Commit f8cae71

Browse files
committed
Rollup merge of #31516 - steveklabnik:doc_tuples, r=brson
Fixes #29339
2 parents d390d63 + 55ff2b9 commit f8cae71

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-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

+71-27
Original file line numberDiff line numberDiff line change
@@ -357,50 +357,94 @@ 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+
/// 'Length' is also sometimes called 'arity' here; each tuple of a different
370+
/// length is a different, distinct type.
371+
///
372+
/// Tuples are *heterogeneous*. This means that each element of the tuple can
373+
/// have a different type. In that tuple above, it has the type:
374+
///
375+
/// ```rust,ignore
376+
/// (&'static str, i32, char)
377+
/// ```
378+
///
379+
/// Tuples are a *sequence*. This means that they can be accessed by position;
380+
/// this is called 'tuple indexing', and it looks like this:
381+
///
382+
/// ```rust
383+
/// let tuple = ("hello", 5, 'c');
384+
///
385+
/// assert_eq!(tuple.0, "hello");
386+
/// assert_eq!(tuple.1, 5);
387+
/// assert_eq!(tuple.2, 'c');
388+
/// ```
389+
///
390+
/// For more about tuples, see [the book](../../book/primitive-types.html#tuples).
391+
///
392+
/// # Trait implementations
366393
///
367394
/// If every type inside a tuple implements one of the following
368395
/// traits, then a tuple itself also implements it.
369396
///
370-
/// * `Clone`
371-
/// * `PartialEq`
372-
/// * `Eq`
373-
/// * `PartialOrd`
374-
/// * `Ord`
375-
/// * `Debug`
376-
/// * `Default`
377-
/// * `Hash`
397+
/// * [`Clone`]
398+
/// * [`PartialEq`]
399+
/// * [`Eq`]
400+
/// * [`PartialOrd`]
401+
/// * [`Ord`]
402+
/// * [`Debug`]
403+
/// * [`Default`]
404+
/// * [`Hash`]
405+
///
406+
/// [`Clone`]: ../clone/trait.Clone.html
407+
/// [`PartialEq`]: ../cmp/trait.PartialEq.html
408+
/// [`Eq`]: ../cmp/trait.Eq.html
409+
/// [`PartialOrd`]: ../cmp/trait.PartialOrd.html
410+
/// [`Ord`]: ../cmp/trait.Ord.html
411+
/// [`Debug`]: ../fmt/trait.Debug.html
412+
/// [`Default`]: ../default/trait.Default.html
413+
/// [`Hash`]: ../hash/trait.Hash.html
414+
///
415+
/// Due to a temporary restriction in Rust's type system, these traits are only
416+
/// implemented on tuples of arity 32 or less. In the future, this may change.
378417
///
379418
/// # Examples
380419
///
381-
/// Accessing elements of a tuple at specified indices:
420+
/// Basic usage:
382421
///
383422
/// ```
384-
/// let x = ("colorless", "green", "ideas", "sleep", "furiously");
385-
/// assert_eq!(x.3, "sleep");
423+
/// let tuple = ("hello", 5, 'c');
386424
///
387-
/// let v = (3, 3);
388-
/// let u = (1, -5);
389-
/// assert_eq!(v.0 * u.0 + v.1 * u.1, -12);
425+
/// assert_eq!(tuple.0, "hello");
390426
/// ```
391427
///
392-
/// Using traits implemented for tuples:
428+
/// Tuples are often used as a return type when you want to return more than
429+
/// one value:
393430
///
394431
/// ```
395-
/// let a = (1, 2);
396-
/// let b = (3, 4);
397-
/// assert!(a != b);
432+
/// fn calculate_point() -> (i32, i32) {
433+
/// // Don't do a calculation, that's not the point of the example
434+
/// (4, 5)
435+
/// }
436+
///
437+
/// let point = calculate_point();
438+
///
439+
/// assert_eq!(point.0, 4);
440+
/// assert_eq!(point.1, 5);
441+
///
442+
/// // Combining this with patterns can be nicer.
398443
///
399-
/// let c = b.clone();
400-
/// assert!(b == c);
444+
/// let (x, y) = calcualte_point();
401445
///
402-
/// let d : (u32, f32) = Default::default();
403-
/// assert_eq!(d, (0, 0.0f32));
446+
/// assert_eq!(x, 4);
447+
/// assert_eq!(y, 5);
404448
/// ```
405449
///
406450
mod prim_tuple { }

0 commit comments

Comments
 (0)