@@ -357,50 +357,94 @@ mod prim_str { }
357
357
//
358
358
/// A finite heterogeneous sequence, `(T, U, ..)`.
359
359
///
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:
362
361
///
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
366
393
///
367
394
/// If every type inside a tuple implements one of the following
368
395
/// traits, then a tuple itself also implements it.
369
396
///
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.
378
417
///
379
418
/// # Examples
380
419
///
381
- /// Accessing elements of a tuple at specified indices :
420
+ /// Basic usage :
382
421
///
383
422
/// ```
384
- /// let x = ("colorless", "green", "ideas", "sleep", "furiously");
385
- /// assert_eq!(x.3, "sleep");
423
+ /// let tuple = ("hello", 5, 'c');
386
424
///
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");
390
426
/// ```
391
427
///
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:
393
430
///
394
431
/// ```
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.
398
443
///
399
- /// let c = b.clone();
400
- /// assert!(b == c);
444
+ /// let (x, y) = calcualte_point();
401
445
///
402
- /// let d : (u32, f32) = Default::default( );
403
- /// assert_eq!(d, (0, 0.0f32) );
446
+ /// assert_eq!(x, 4 );
447
+ /// assert_eq!(y, 5 );
404
448
/// ```
405
449
///
406
450
mod prim_tuple { }
0 commit comments