Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor range examples #35759

Closed
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1881,11 +1901,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 @@ -1910,12 +1931,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 @@ -1973,12 +1995,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 @@ -2040,11 +2063,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 @@ -2091,10 +2115,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 @@ -2192,11 +2219,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