68
68
//! ```
69
69
//!
70
70
//! 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.
71
91
72
92
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
73
93
@@ -1738,11 +1758,12 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
1738
1758
///
1739
1759
/// ```
1740
1760
/// let arr = [0, 1, 2, 3];
1741
- /// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull
1742
- /// assert_eq!(arr[ ..3], [0,1,2 ]);
1743
- /// assert_eq!(arr[1.. ], [ 1,2,3]);
1744
- /// assert_eq!(arr[1..3], [ 1,2 ]);
1761
+ /// assert_eq!(arr[ .. ], [0, 1, 2, 3]);
1745
1762
/// ```
1763
+ ///
1764
+ /// See the [module examples] for the behavior of other range structs.
1765
+ ///
1766
+ /// [module examples]: ../#Examples
1746
1767
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
1747
1768
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1748
1769
pub struct RangeFull ;
@@ -1767,12 +1788,13 @@ impl fmt::Debug for RangeFull {
1767
1788
/// assert_eq!(3+4+5, (3..6).sum());
1768
1789
///
1769
1790
/// let arr = [0, 1, 2, 3];
1770
- /// assert_eq!(arr[ .. ], [0,1,2,3]);
1771
- /// assert_eq!(arr[ ..3], [0,1,2 ]);
1772
- /// assert_eq!(arr[1.. ], [ 1,2,3]);
1773
- /// assert_eq!(arr[1..3], [ 1,2 ]); // Range
1791
+ /// assert_eq!(arr[1..3], [1, 2]);
1774
1792
/// }
1775
1793
/// ```
1794
+ ///
1795
+ /// See the [module examples] for the behavior of other range structs.
1796
+ ///
1797
+ /// [module examples]: ../#Examples
1776
1798
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
1777
1799
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1778
1800
pub struct Range < Idx > {
@@ -1830,12 +1852,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
1830
1852
/// assert_eq!(2+3+4, (2..).take(3).sum());
1831
1853
///
1832
1854
/// let arr = [0, 1, 2, 3];
1833
- /// assert_eq!(arr[ .. ], [0,1,2,3]);
1834
- /// assert_eq!(arr[ ..3], [0,1,2 ]);
1835
- /// assert_eq!(arr[1.. ], [ 1,2,3]); // RangeFrom
1836
- /// assert_eq!(arr[1..3], [ 1,2 ]);
1855
+ /// assert_eq!(arr[1.. ], [1, 2, 3]);
1837
1856
/// }
1838
1857
/// ```
1858
+ ///
1859
+ /// See the [module examples] for the behavior of other range structs.
1860
+ ///
1861
+ /// [module examples]: ../#Examples
1839
1862
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
1840
1863
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1841
1864
pub struct RangeFrom < Idx > {
@@ -1880,12 +1903,13 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
1880
1903
/// assert_eq!((..5), std::ops::RangeTo{ end: 5 });
1881
1904
///
1882
1905
/// let arr = [0, 1, 2, 3];
1883
- /// assert_eq!(arr[ .. ], [0,1,2,3]);
1884
- /// assert_eq!(arr[ ..3], [0,1,2 ]); // RangeTo
1885
- /// assert_eq!(arr[1.. ], [ 1,2,3]);
1886
- /// assert_eq!(arr[1..3], [ 1,2 ]);
1906
+ /// assert_eq!(arr[ ..3], [0, 1, 2]);
1887
1907
/// }
1888
1908
/// ```
1909
+ ///
1910
+ /// See the [module examples] for the behavior of other range structs.
1911
+ ///
1912
+ /// [module examples]: ../#Examples
1889
1913
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
1890
1914
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1891
1915
pub struct RangeTo < Idx > {
@@ -1932,10 +1956,13 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
1932
1956
/// assert_eq!(3+4+5, (3...5).sum());
1933
1957
///
1934
1958
/// let arr = [0, 1, 2, 3];
1935
- /// assert_eq!(arr[ ...2], [0,1,2 ]);
1936
- /// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
1959
+ /// assert_eq!(arr[1...2], [1, 2]);
1937
1960
/// }
1938
1961
/// ```
1962
+ ///
1963
+ /// See the [module examples] for the behavior of other range structs.
1964
+ ///
1965
+ /// [module examples]: ../#Examples
1939
1966
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
1940
1967
#[ unstable( feature = "inclusive_range" , reason = "recently added, follows RFC" , issue = "28237" ) ]
1941
1968
pub enum RangeInclusive < Idx > {
@@ -2019,10 +2046,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
2019
2046
/// assert_eq!((...5), std::ops::RangeToInclusive{ end: 5 });
2020
2047
///
2021
2048
/// let arr = [0, 1, 2, 3];
2022
- /// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
2023
- /// assert_eq!(arr[1...2], [ 1,2 ]);
2049
+ /// assert_eq!(arr[ ...2], [0, 1, 2]);
2024
2050
/// }
2025
2051
/// ```
2052
+ ///
2053
+ /// See the [module examples] for the behavior of other range structs.
2054
+ ///
2055
+ /// [module examples]: ../#Examples
2026
2056
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
2027
2057
#[ unstable( feature = "inclusive_range" , reason = "recently added, follows RFC" , issue = "28237" ) ]
2028
2058
pub struct RangeToInclusive < Idx > {
0 commit comments