Skip to content

Commit 7782838

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 4473130 commit 7782838

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

src/libcore/ops.rs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@
140140
//!
141141
//! // `consume_and_return_x` can no longer be invoked at this point
142142
//! ```
143+
//!
144+
//! This example shows the behavior of the various `Range*` structs.
145+
//!
146+
//! ```rust
147+
//! #![feature(inclusive_range_syntax)]
148+
//!
149+
//! let arr = [0, 1, 2, 3, 4];
150+
//!
151+
//! assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
152+
//! assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
153+
//! assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
154+
//! assert_eq!(arr[1..3], [ 1,2 ]); // Range
155+
//!
156+
//! assert_eq!(arr[ ...3], [0,1,2,3 ]); // RangeToIncusive
157+
//! assert_eq!(arr[1...3], [ 1,2,3 ]); // RangeInclusive
158+
//! ```
143159
144160
#![stable(feature = "rust1", since = "1.0.0")]
145161

@@ -1881,11 +1897,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
18811897
///
18821898
/// ```
18831899
/// let arr = [0, 1, 2, 3];
1884-
/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
1885-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1886-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1887-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1900+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
18881901
/// ```
1902+
///
1903+
/// See the [module examples] for the behavior of other range structs.
1904+
///
1905+
/// [module examples]: ../#Examples
18891906
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
18901907
#[stable(feature = "rust1", since = "1.0.0")]
18911908
pub struct RangeFull;
@@ -1910,12 +1927,13 @@ impl fmt::Debug for RangeFull {
19101927
/// assert_eq!(3+4+5, (3..6).sum());
19111928
///
19121929
/// let arr = [0, 1, 2, 3];
1913-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1914-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1915-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1916-
/// assert_eq!(arr[1..3], [ 1,2 ]); // Range
1930+
/// assert_eq!(arr[1..3], [1, 2]);
19171931
/// }
19181932
/// ```
1933+
///
1934+
/// See the [module examples] for the behavior of other range structs.
1935+
///
1936+
/// [module examples]: ../#Examples
19191937
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
19201938
#[stable(feature = "rust1", since = "1.0.0")]
19211939
pub struct Range<Idx> {
@@ -1973,12 +1991,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
19731991
/// assert_eq!(2+3+4, (2..).take(3).sum());
19741992
///
19751993
/// let arr = [0, 1, 2, 3];
1976-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
1977-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1978-
/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
1979-
/// assert_eq!(arr[1..3], [ 1,2 ]);
1994+
/// assert_eq!(arr[1.. ], [1, 2, 3]);
19801995
/// }
19811996
/// ```
1997+
///
1998+
/// See the [module examples] for the behavior of other range structs.
1999+
///
2000+
/// [module examples]: ../#Examples
19822001
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
19832002
#[stable(feature = "rust1", since = "1.0.0")]
19842003
pub struct RangeFrom<Idx> {
@@ -2040,11 +2059,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
20402059
///
20412060
/// ```
20422061
/// let arr = [0, 1, 2, 3];
2043-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
2044-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
2045-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
2046-
/// assert_eq!(arr[1..3], [ 1,2 ]);
2062+
/// assert_eq!(arr[ ..3], [0, 1, 2]);
20472063
/// ```
2064+
///
2065+
/// See the [module examples] for the behavior of other range structs.
2066+
///
2067+
/// [module examples]: ../#Examples
20482068
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20492069
#[stable(feature = "rust1", since = "1.0.0")]
20502070
pub struct RangeTo<Idx> {
@@ -2091,10 +2111,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
20912111
/// assert_eq!(3+4+5, (3...5).sum());
20922112
///
20932113
/// let arr = [0, 1, 2, 3];
2094-
/// assert_eq!(arr[ ...2], [0,1,2 ]);
2095-
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
2114+
/// assert_eq!(arr[1...2], [1, 2]);
20962115
/// }
20972116
/// ```
2117+
///
2118+
/// See the [module examples] for the behavior of other range structs.
2119+
///
2120+
/// [module examples]: ../#Examples
20982121
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
20992122
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
21002123
pub enum RangeInclusive<Idx> {
@@ -2192,11 +2215,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
21922215
/// array elements up to and including the index indicated by `end`.
21932216
///
21942217
/// ```
2195-
/// #![feature(inclusive_range_syntax)]
21962218
/// let arr = [0, 1, 2, 3];
2197-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2198-
/// assert_eq!(arr[1...2], [ 1,2 ]);
2219+
/// assert_eq!(arr[ ...2], [0, 1, 2]);
21992220
/// ```
2221+
///
2222+
/// See the [module examples] for the behavior of other range structs.
2223+
///
2224+
/// [module examples]: ../#Examples
22002225
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
22012226
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
22022227
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)