Skip to content

Commit 31b7ff4

Browse files
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. add feature for inclusive_range_syntax fix incorrectly closed code fences
1 parent 4901896 commit 31b7ff4

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

src/libcore/ops.rs

+46-20
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@
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+
//!
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+
//! ```
7187
7288
#![stable(feature = "rust1", since = "1.0.0")]
7389

@@ -1594,11 +1610,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
15941610
///
15951611
/// ```
15961612
/// let arr = [0, 1, 2, 3];
1597-
/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
1598-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1599-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1600-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1613+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
16011614
/// ```
1615+
///
1616+
/// See the [module examples] for the behavior of other range structs.
1617+
///
1618+
/// [module examples]: ../#Examples
16021619
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
16031620
#[stable(feature = "rust1", since = "1.0.0")]
16041621
pub struct RangeFull;
@@ -1623,12 +1640,13 @@ impl fmt::Debug for RangeFull {
16231640
/// assert_eq!(3+4+5, (3..6).sum());
16241641
///
16251642
/// let arr = [0, 1, 2, 3];
1626-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1627-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1628-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1629-
/// assert_eq!(arr[1..3], [ 1,2 ]); // Range
1643+
/// assert_eq!(arr[1..3], [1, 2]);
16301644
/// }
16311645
/// ```
1646+
///
1647+
/// See the [module examples] for the behavior of other range structs.
1648+
///
1649+
/// [module examples]: ../#Examples
16321650
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
16331651
#[stable(feature = "rust1", since = "1.0.0")]
16341652
pub struct Range<Idx> {
@@ -1686,12 +1704,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
16861704
/// assert_eq!(2+3+4, (2..).take(3).sum());
16871705
///
16881706
/// let arr = [0, 1, 2, 3];
1689-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1690-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1691-
/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
1692-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1707+
/// assert_eq!(arr[1.. ], [1, 2, 3]);
16931708
/// }
16941709
/// ```
1710+
///
1711+
/// See the [module examples] for the behavior of other range structs.
1712+
///
1713+
/// [module examples]: ../#Examples
16951714
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
16961715
#[stable(feature = "rust1", since = "1.0.0")]
16971716
pub struct RangeFrom<Idx> {
@@ -1736,12 +1755,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
17361755
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
17371756
///
17381757
/// let arr = [0, 1, 2, 3];
1739-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1740-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1741-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1742-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1758+
/// assert_eq!(arr[ ..3], [0, 1, 2]);
17431759
/// }
17441760
/// ```
1761+
///
1762+
/// See the [module examples] for the behavior of other range structs.
1763+
///
1764+
/// [module examples]: ../#Examples
17451765
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
17461766
#[stable(feature = "rust1", since = "1.0.0")]
17471767
pub struct RangeTo<Idx> {
@@ -1788,10 +1808,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
17881808
/// assert_eq!(3+4+5, (3...5).sum());
17891809
///
17901810
/// let arr = [0, 1, 2, 3];
1791-
/// assert_eq!(arr[ ...2], [0,1,2 ]);
1792-
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
1811+
/// assert_eq!(arr[1...2], [1, 2]);
17931812
/// }
17941813
/// ```
1814+
///
1815+
/// See the [module examples] for the behavior of other range structs.
1816+
///
1817+
/// [module examples]: ../#Examples
17951818
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
17961819
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
17971820
pub enum RangeInclusive<Idx> {
@@ -1875,10 +1898,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
18751898
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
18761899
///
18771900
/// let arr = [0, 1, 2, 3];
1878-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
1879-
/// assert_eq!(arr[1...2], [ 1,2 ]);
1901+
/// assert_eq!(arr[ ...2], [0, 1, 2]);
18801902
/// }
18811903
/// ```
1904+
///
1905+
/// See the [module examples] for the behavior of other range structs.
1906+
///
1907+
/// [module examples]: ../#Examples
18821908
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
18831909
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
18841910
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)