Skip to content

Commit 34ce63c

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 c79ad3c + 711333f commit 34ce63c

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

@@ -1736,11 +1756,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
17361756
///
17371757
/// ```
17381758
/// let arr = [0, 1, 2, 3];
1739-
/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
1740-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1741-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1742-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1759+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
17431760
/// ```
1761+
///
1762+
/// See the [module examples] for the behavior of other range structs.
1763+
///
1764+
/// [module examples]: ../#Examples
17441765
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
17451766
#[stable(feature = "rust1", since = "1.0.0")]
17461767
pub struct RangeFull;
@@ -1765,12 +1786,13 @@ impl fmt::Debug for RangeFull {
17651786
/// assert_eq!(3+4+5, (3..6).sum());
17661787
///
17671788
/// let arr = [0, 1, 2, 3];
1768-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1769-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1770-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1771-
/// assert_eq!(arr[1..3], [ 1,2 ]); // Range
1789+
/// assert_eq!(arr[1..3], [1, 2]);
17721790
/// }
17731791
/// ```
1792+
///
1793+
/// See the [module examples] for the behavior of other range structs.
1794+
///
1795+
/// [module examples]: ../#Examples
17741796
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
17751797
#[stable(feature = "rust1", since = "1.0.0")]
17761798
pub struct Range<Idx> {
@@ -1828,12 +1850,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
18281850
/// assert_eq!(2+3+4, (2..).take(3).sum());
18291851
///
18301852
/// let arr = [0, 1, 2, 3];
1831-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1832-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1833-
/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
1834-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1853+
/// assert_eq!(arr[1.. ], [1, 2, 3]);
18351854
/// }
18361855
/// ```
1856+
///
1857+
/// See the [module examples] for the behavior of other range structs.
1858+
///
1859+
/// [module examples]: ../#Examples
18371860
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
18381861
#[stable(feature = "rust1", since = "1.0.0")]
18391862
pub struct RangeFrom<Idx> {
@@ -1878,12 +1901,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
18781901
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
18791902
///
18801903
/// let arr = [0, 1, 2, 3];
1881-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1882-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1883-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1884-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1904+
/// assert_eq!(arr[ ..3], [0, 1, 2]);
18851905
/// }
18861906
/// ```
1907+
///
1908+
/// See the [module examples] for the behavior of other range structs.
1909+
///
1910+
/// [module examples]: ../#Examples
18871911
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
18881912
#[stable(feature = "rust1", since = "1.0.0")]
18891913
pub struct RangeTo<Idx> {
@@ -1930,10 +1954,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
19301954
/// assert_eq!(3+4+5, (3...5).sum());
19311955
///
19321956
/// let arr = [0, 1, 2, 3];
1933-
/// assert_eq!(arr[ ...2], [0,1,2 ]);
1934-
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
1957+
/// assert_eq!(arr[1...2], [1, 2]);
19351958
/// }
19361959
/// ```
1960+
///
1961+
/// See the [module examples] for the behavior of other range structs.
1962+
///
1963+
/// [module examples]: ../#Examples
19371964
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
19381965
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
19391966
pub enum RangeInclusive<Idx> {
@@ -2017,10 +2044,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
20172044
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
20182045
///
20192046
/// let arr = [0, 1, 2, 3];
2020-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2021-
/// assert_eq!(arr[1...2], [ 1,2 ]);
2047+
/// assert_eq!(arr[ ...2], [0, 1, 2]);
20222048
/// }
20232049
/// ```
2050+
///
2051+
/// See the [module examples] for the behavior of other range structs.
2052+
///
2053+
/// [module examples]: ../#Examples
20242054
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20252055
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
20262056
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)