Skip to content

Commit

Permalink
refactor range examples
Browse files Browse the repository at this point in the history
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.

add feature for inclusive_range_syntax

fix incorrectly closed code fences
  • Loading branch information
matthew-piziak committed Aug 22, 2016
1 parent 4901896 commit 31b7ff4
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@
//! ```
//!
//! See the documentation for each trait for an example implementation.
//!
//! This example shows the behavior of the various `Range*` structs.
//!
//! ```rust
//! #![feature(inclusive_range_syntax)]
//!
//! 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
//! ```
#![stable(feature = "rust1", since = "1.0.0")]

Expand Down Expand Up @@ -1594,11 +1610,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 @@ -1623,12 +1640,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 @@ -1686,12 +1704,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 @@ -1736,12 +1755,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
///
/// 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 @@ -1788,10 +1808,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 @@ -1875,10 +1898,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
///
/// 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 31b7ff4

Please sign in to comment.