Skip to content

Commit

Permalink
Rollup merge of rust-lang#35759 - matthew-piziak:refactor-range-examp…
Browse files Browse the repository at this point in the history
…les, r=steveklabnik

refactor range examples

This pull request adds a module-level example of how all the range operators work. It also slims down struct-level examples in lieu of a link to module examples.
  • Loading branch information
Jonathan Turner committed Sep 2, 2016
2 parents a6cfc58 + 34be21d commit fc7c26f
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@
//!
//! // `consume_and_return_x` can no longer be invoked at this point
//! ```
//!
//! This example shows the behavior of the various `Range*` structs.
//!
//! ```rust
//! #![feature(inclusive_range_syntax)]
//! fn main() {
//! let arr = [0, 1, 2, 3, 4];
//!
//! assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
//! assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
//! assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
//! assert_eq!(arr[1..3], [ 1,2 ]); // Range
//!
//! assert_eq!(arr[ ...3], [0,1,2,3 ]); // RangeToIncusive
//! assert_eq!(arr[1...3], [ 1,2,3 ]); // RangeInclusive
//! }
//! ```
//!
//! Note: whitespace alignment is not idiomatic Rust. An exception is made in
//! this case to facilitate comparison.

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down Expand Up @@ -1986,11 +2006,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
///
/// ```
/// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
/// assert_eq!(arr[ ..3], [0,1,2 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3]);
/// assert_eq!(arr[1..3], [ 1,2 ]);
/// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
/// ```
///
/// See the [module examples] for the behavior of other range structs.
///
/// [module examples]: ../#Examples
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RangeFull;
Expand All @@ -2015,12 +2036,13 @@ impl fmt::Debug for RangeFull {
/// assert_eq!(3+4+5, (3..6).sum());
///
/// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ .. ], [0,1,2,3]);
/// assert_eq!(arr[ ..3], [0,1,2 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3]);
/// assert_eq!(arr[1..3], [ 1,2 ]); // Range
/// assert_eq!(arr[1..3], [1, 2]);
/// }
/// ```
///
/// See the [module examples] for the behavior of other range structs.
///
/// [module examples]: ../#Examples
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Range<Idx> {
Expand Down Expand Up @@ -2078,12 +2100,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// assert_eq!(2+3+4, (2..).take(3).sum());
///
/// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ .. ], [0,1,2,3]);
/// assert_eq!(arr[ ..3], [0,1,2 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
/// assert_eq!(arr[1..3], [ 1,2 ]);
/// assert_eq!(arr[1.. ], [1, 2, 3]);
/// }
/// ```
///
/// See the [module examples] for the behavior of other range structs.
///
/// [module examples]: ../#Examples
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RangeFrom<Idx> {
Expand Down Expand Up @@ -2145,11 +2168,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
///
/// ```
/// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ .. ], [0,1,2,3]);
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
/// assert_eq!(arr[1.. ], [ 1,2,3]);
/// assert_eq!(arr[1..3], [ 1,2 ]);
/// assert_eq!(arr[ ..3], [0, 1, 2]);
/// ```
///
/// See the [module examples] for the behavior of other range structs.
///
/// [module examples]: ../#Examples
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RangeTo<Idx> {
Expand Down Expand Up @@ -2196,10 +2220,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
/// assert_eq!(3+4+5, (3...5).sum());
///
/// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ ...2], [0,1,2 ]);
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
/// assert_eq!(arr[1...2], [1, 2]);
/// }
/// ```
///
/// See the [module examples] for the behavior of other range structs.
///
/// [module examples]: ../#Examples
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
pub enum RangeInclusive<Idx> {
Expand Down Expand Up @@ -2297,11 +2324,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// array elements up to and including the index indicated by `end`.
///
/// ```
/// #![feature(inclusive_range_syntax)]
/// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
/// assert_eq!(arr[1...2], [ 1,2 ]);
/// assert_eq!(arr[ ...2], [0, 1, 2]);
/// ```
///
/// See the [module examples] for the behavior of other range structs.
///
/// [module examples]: ../#Examples
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
pub struct RangeToInclusive<Idx> {
Expand Down

0 comments on commit fc7c26f

Please sign in to comment.