140
140
//!
141
141
//! // `consume_and_return_x` can no longer be invoked at this point
142
142
//! ```
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.
143
163
144
164
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
145
165
@@ -1986,11 +2006,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
1986
2006
///
1987
2007
/// ```
1988
2008
/// 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]);
1993
2010
/// ```
2011
+ ///
2012
+ /// See the [module examples] for the behavior of other range structs.
2013
+ ///
2014
+ /// [module examples]: ../#Examples
1994
2015
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
1995
2016
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1996
2017
pub struct RangeFull ;
@@ -2015,12 +2036,13 @@ impl fmt::Debug for RangeFull {
2015
2036
/// assert_eq!(3+4+5, (3..6).sum());
2016
2037
///
2017
2038
/// 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]);
2022
2040
/// }
2023
2041
/// ```
2042
+ ///
2043
+ /// See the [module examples] for the behavior of other range structs.
2044
+ ///
2045
+ /// [module examples]: ../#Examples
2024
2046
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
2025
2047
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2026
2048
pub struct Range < Idx > {
@@ -2078,12 +2100,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
2078
2100
/// assert_eq!(2+3+4, (2..).take(3).sum());
2079
2101
///
2080
2102
/// 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]);
2085
2104
/// }
2086
2105
/// ```
2106
+ ///
2107
+ /// See the [module examples] for the behavior of other range structs.
2108
+ ///
2109
+ /// [module examples]: ../#Examples
2087
2110
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
2088
2111
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2089
2112
pub struct RangeFrom < Idx > {
@@ -2145,11 +2168,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
2145
2168
///
2146
2169
/// ```
2147
2170
/// 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]);
2152
2172
/// ```
2173
+ ///
2174
+ /// See the [module examples] for the behavior of other range structs.
2175
+ ///
2176
+ /// [module examples]: ../#Examples
2153
2177
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
2154
2178
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2155
2179
pub struct RangeTo < Idx > {
@@ -2196,10 +2220,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
2196
2220
/// assert_eq!(3+4+5, (3...5).sum());
2197
2221
///
2198
2222
/// 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]);
2201
2224
/// }
2202
2225
/// ```
2226
+ ///
2227
+ /// See the [module examples] for the behavior of other range structs.
2228
+ ///
2229
+ /// [module examples]: ../#Examples
2203
2230
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
2204
2231
#[ unstable( feature = "inclusive_range" , reason = "recently added, follows RFC" , issue = "28237" ) ]
2205
2232
pub enum RangeInclusive < Idx > {
@@ -2297,11 +2324,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
2297
2324
/// array elements up to and including the index indicated by `end`.
2298
2325
///
2299
2326
/// ```
2300
- /// #![feature(inclusive_range_syntax)]
2301
2327
/// 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]);
2304
2329
/// ```
2330
+ ///
2331
+ /// See the [module examples] for the behavior of other range structs.
2332
+ ///
2333
+ /// [module examples]: ../#Examples
2305
2334
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
2306
2335
#[ unstable( feature = "inclusive_range" , reason = "recently added, follows RFC" , issue = "28237" ) ]
2307
2336
pub struct RangeToInclusive < Idx > {
0 commit comments