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