Skip to content

Commit fc7c26f

Browse files
author
Jonathan Turner
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 a6cfc58 + 34be21d commit fc7c26f

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

src/libcore/ops.rs

+50-21
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@
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+
//! fn main() {
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+
//! }
159+
//! ```
160+
//!
161+
//! Note: whitespace alignment is not idiomatic Rust. An exception is made in
162+
//! this case to facilitate comparison.
143163
144164
#![stable(feature = "rust1", since = "1.0.0")]
145165

@@ -1986,11 +2006,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
19862006
///
19872007
/// ```
19882008
/// let arr = [0, 1, 2, 3];
1989-
/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
1990-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
1991-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
1992-
/// assert_eq!(arr[1..3], [ 1,2 ]);
2009+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
19932010
/// ```
2011+
///
2012+
/// See the [module examples] for the behavior of other range structs.
2013+
///
2014+
/// [module examples]: ../#Examples
19942015
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
19952016
#[stable(feature = "rust1", since = "1.0.0")]
19962017
pub struct RangeFull;
@@ -2015,12 +2036,13 @@ impl fmt::Debug for RangeFull {
20152036
/// assert_eq!(3+4+5, (3..6).sum());
20162037
///
20172038
/// let arr = [0, 1, 2, 3];
2018-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
2019-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
2020-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
2021-
/// assert_eq!(arr[1..3], [ 1,2 ]); // Range
2039+
/// assert_eq!(arr[1..3], [1, 2]);
20222040
/// }
20232041
/// ```
2042+
///
2043+
/// See the [module examples] for the behavior of other range structs.
2044+
///
2045+
/// [module examples]: ../#Examples
20242046
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
20252047
#[stable(feature = "rust1", since = "1.0.0")]
20262048
pub struct Range<Idx> {
@@ -2078,12 +2100,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
20782100
/// assert_eq!(2+3+4, (2..).take(3).sum());
20792101
///
20802102
/// let arr = [0, 1, 2, 3];
2081-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
2082-
/// assert_eq!(arr[ ..3], [0,1,2 ]);
2083-
/// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
2084-
/// assert_eq!(arr[1..3], [ 1,2 ]);
2103+
/// assert_eq!(arr[1.. ], [1, 2, 3]);
20852104
/// }
20862105
/// ```
2106+
///
2107+
/// See the [module examples] for the behavior of other range structs.
2108+
///
2109+
/// [module examples]: ../#Examples
20872110
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
20882111
#[stable(feature = "rust1", since = "1.0.0")]
20892112
pub struct RangeFrom<Idx> {
@@ -2145,11 +2168,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
21452168
///
21462169
/// ```
21472170
/// let arr = [0, 1, 2, 3];
2148-
/// assert_eq!(arr[ .. ], [0,1,2,3]);
2149-
/// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
2150-
/// assert_eq!(arr[1.. ], [ 1,2,3]);
2151-
/// assert_eq!(arr[1..3], [ 1,2 ]);
2171+
/// assert_eq!(arr[ ..3], [0, 1, 2]);
21522172
/// ```
2173+
///
2174+
/// See the [module examples] for the behavior of other range structs.
2175+
///
2176+
/// [module examples]: ../#Examples
21532177
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
21542178
#[stable(feature = "rust1", since = "1.0.0")]
21552179
pub struct RangeTo<Idx> {
@@ -2196,10 +2220,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
21962220
/// assert_eq!(3+4+5, (3...5).sum());
21972221
///
21982222
/// let arr = [0, 1, 2, 3];
2199-
/// assert_eq!(arr[ ...2], [0,1,2 ]);
2200-
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
2223+
/// assert_eq!(arr[1...2], [1, 2]);
22012224
/// }
22022225
/// ```
2226+
///
2227+
/// See the [module examples] for the behavior of other range structs.
2228+
///
2229+
/// [module examples]: ../#Examples
22032230
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
22042231
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
22052232
pub enum RangeInclusive<Idx> {
@@ -2297,11 +2324,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
22972324
/// array elements up to and including the index indicated by `end`.
22982325
///
22992326
/// ```
2300-
/// #![feature(inclusive_range_syntax)]
23012327
/// let arr = [0, 1, 2, 3];
2302-
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2303-
/// assert_eq!(arr[1...2], [ 1,2 ]);
2328+
/// assert_eq!(arr[ ...2], [0, 1, 2]);
23042329
/// ```
2330+
///
2331+
/// See the [module examples] for the behavior of other range structs.
2332+
///
2333+
/// [module examples]: ../#Examples
23052334
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
23062335
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
23072336
pub struct RangeToInclusive<Idx> {

0 commit comments

Comments
 (0)