Skip to content

Commit 3c2386b

Browse files
authored
Rollup merge of rust-lang#35759 - matthew-piziak:refactor-range-examples, 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.
2 parents 80571b8 + 711333f commit 3c2386b

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

src/libcore/ops.rs

+50-20
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@
6868
//! ```
6969
//!
7070
//! See the documentation for each trait for an example implementation.
71+
//!
72+
//! This example shows the behavior of the various `Range*` structs.
73+
//!
74+
//! ```rust
75+
//! #![feature(inclusive_range_syntax)]
76+
//! fn main() {
77+
//! let arr = [0, 1, 2, 3, 4];
78+
//!
79+
//! assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
80+
//! assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
81+
//! assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
82+
//! assert_eq!(arr[1..3], [ 1,2 ]); // Range
83+
//!
84+
//! assert_eq!(arr[ ...3], [0,1,2,3 ]); // RangeToIncusive
85+
//! assert_eq!(arr[1...3], [ 1,2,3 ]); // RangeInclusive
86+
//! }
87+
//! ```
88+
//!
89+
//! Note: whitespace alignment is not idiomatic Rust. An exception is made in
90+
//! this case to facilitate comparison.
7191
7292
#![stable(feature = "rust1", since = "1.0.0")]
7393

@@ -1738,11 +1758,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
17381758
///
17391759
/// ```
17401760
/// let arr = [0, 1, 2, 3];
1741-
/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
1742-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1743-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1744-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1761+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
17451762
/// ```
1763+
///
1764+
/// See the [module examples] for the behavior of other range structs.
1765+
///
1766+
/// [module examples]: ../#Examples
17461767
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
17471768
#[stable(feature = "rust1", since = "1.0.0")]
17481769
pub struct RangeFull;
@@ -1767,12 +1788,13 @@ impl fmt::Debug for RangeFull {
17671788
/// assert_eq!(3+4+5, (3..6).sum());
17681789
///
17691790
/// let arr = [0, 1, 2, 3];
1770-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1771-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1772-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1773-
/// assert_eq!(arr[1..3], [ 1,2 ]); // Range
1791+
/// assert_eq!(arr[1..3], [1, 2]);
17741792
/// }
17751793
/// ```
1794+
///
1795+
/// See the [module examples] for the behavior of other range structs.
1796+
///
1797+
/// [module examples]: ../#Examples
17761798
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
17771799
#[stable(feature = "rust1", since = "1.0.0")]
17781800
pub struct Range<Idx> {
@@ -1830,12 +1852,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
18301852
/// assert_eq!(2+3+4, (2..).take(3).sum());
18311853
///
18321854
/// let arr = [0, 1, 2, 3];
1833-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1834-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1835-
/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
1836-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1855+
/// assert_eq!(arr[1.. ], [1, 2, 3]);
18371856
/// }
18381857
/// ```
1858+
///
1859+
/// See the [module examples] for the behavior of other range structs.
1860+
///
1861+
/// [module examples]: ../#Examples
18391862
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
18401863
#[stable(feature = "rust1", since = "1.0.0")]
18411864
pub struct RangeFrom<Idx> {
@@ -1880,12 +1903,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
18801903
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
18811904
///
18821905
/// let arr = [0, 1, 2, 3];
1883-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1884-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1885-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1886-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1906+
/// assert_eq!(arr[ ..3], [0, 1, 2]);
18871907
/// }
18881908
/// ```
1909+
///
1910+
/// See the [module examples] for the behavior of other range structs.
1911+
///
1912+
/// [module examples]: ../#Examples
18891913
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
18901914
#[stable(feature = "rust1", since = "1.0.0")]
18911915
pub struct RangeTo<Idx> {
@@ -1932,10 +1956,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
19321956
/// assert_eq!(3+4+5, (3...5).sum());
19331957
///
19341958
/// let arr = [0, 1, 2, 3];
1935-
/// assert_eq!(arr[ ...2], [0,1,2 ]);
1936-
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
1959+
/// assert_eq!(arr[1...2], [1, 2]);
19371960
/// }
19381961
/// ```
1962+
///
1963+
/// See the [module examples] for the behavior of other range structs.
1964+
///
1965+
/// [module examples]: ../#Examples
19391966
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
19401967
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
19411968
pub enum RangeInclusive<Idx> {
@@ -2019,10 +2046,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
20192046
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
20202047
///
20212048
/// let arr = [0, 1, 2, 3];
2022-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2023-
/// assert_eq!(arr[1...2], [ 1,2 ]);
2049+
/// assert_eq!(arr[ ...2], [0, 1, 2]);
20242050
/// }
20252051
/// ```
2052+
///
2053+
/// See the [module examples] for the behavior of other range structs.
2054+
///
2055+
/// [module examples]: ../#Examples
20262056
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20272057
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
20282058
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)