Skip to content

Commit a885c50

Browse files
committed
Improve range docs
* Mention that `RangeFull` is a ZST and thus a singleton * Improve code formatting and legibility * Various other readability improvements
1 parent f3ab6f0 commit a885c50

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

library/core/src/ops/range.rs

+52-40
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ use crate::hash::Hash;
1919
///
2020
/// ```compile_fail,E0277
2121
/// for i in .. {
22-
/// // ...
22+
/// // ...
2323
/// }
2424
/// ```
2525
///
2626
/// Used as a [slicing index], `RangeFull` produces the full array as a slice.
2727
///
2828
/// ```
2929
/// let arr = [0, 1, 2, 3, 4];
30-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
31-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
32-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
33-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
34-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
35-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
30+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); // This is the `RangeFull`
31+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
32+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
33+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
34+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
35+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
3636
/// ```
3737
///
3838
/// [slicing index]: crate::slice::SliceIndex
@@ -52,22 +52,26 @@ impl fmt::Debug for RangeFull {
5252
/// A (half-open) range bounded inclusively below and exclusively above
5353
/// (`start..end`).
5454
///
55-
/// The `Range` `start..end` contains all values with `x >= start` and
56-
/// `x < end`. It is empty unless `start < end`.
55+
/// The range `start..end` contains all values with `start <= x < end`.
56+
/// It is empty if `start >= end`.
5757
///
5858
/// # Examples
5959
///
60+
/// The `start..end` syntax is a `Range`:
61+
///
6062
/// ```
6163
/// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
6264
/// assert_eq!(3 + 4 + 5, (3..6).sum());
65+
/// ```
6366
///
67+
/// ```
6468
/// let arr = [0, 1, 2, 3, 4];
65-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
66-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
67-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
68-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
69-
/// assert_eq!(arr[1.. 3], [ 1,2 ]); // Range
70-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
69+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
70+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
71+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
72+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
73+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]); // This is a `Range`
74+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
7175
/// ```
7276
#[lang = "Range"]
7377
#[doc(alias = "..")]
@@ -160,17 +164,21 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
160164
///
161165
/// # Examples
162166
///
167+
/// The `start..` syntax is a `RangeFrom`:
168+
///
163169
/// ```
164170
/// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
165171
/// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
172+
/// ```
166173
///
174+
/// ```
167175
/// let arr = [0, 1, 2, 3, 4];
168-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
169-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
170-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
171-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
172-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
173-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
176+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
177+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
178+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
179+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); // This is a `RangeFrom`
180+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
181+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
174182
/// ```
175183
#[lang = "RangeFrom"]
176184
#[doc(alias = "..")]
@@ -244,12 +252,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
244252
///
245253
/// ```
246254
/// let arr = [0, 1, 2, 3, 4];
247-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
248-
/// assert_eq!(arr[ .. 3], [0,1,2 ]); // RangeTo
249-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
250-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
251-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
252-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
255+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
256+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]); // This is a `RangeTo`
257+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
258+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
259+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
260+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
253261
/// ```
254262
///
255263
/// [slicing index]: crate::slice::SliceIndex
@@ -310,17 +318,21 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
310318
///
311319
/// # Examples
312320
///
321+
/// The `start..=end` syntax is a `RangeInclusive`:
322+
///
313323
/// ```
314324
/// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
315325
/// assert_eq!(3 + 4 + 5, (3..=5).sum());
326+
/// ```
316327
///
328+
/// ```
317329
/// let arr = [0, 1, 2, 3, 4];
318-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
319-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
320-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
321-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
322-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
323-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]); // RangeInclusive
330+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
331+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
332+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
333+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
334+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
335+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); // This is a `RangeInclusive`
324336
/// ```
325337
#[lang = "RangeInclusive"]
326338
#[doc(alias = "..=")]
@@ -534,12 +546,12 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
534546
///
535547
/// ```
536548
/// let arr = [0, 1, 2, 3, 4];
537-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
538-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
539-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]); // RangeToInclusive
540-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
541-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
542-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
549+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
550+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
551+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); // This is a `RangeToInclusive`
552+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
553+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
554+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
543555
/// ```
544556
///
545557
/// [slicing index]: crate::slice::SliceIndex
@@ -661,9 +673,9 @@ impl<T: Clone> Bound<&T> {
661673
}
662674
}
663675

664-
#[stable(feature = "collections_range", since = "1.28.0")]
665676
/// `RangeBounds` is implemented by Rust's built-in range types, produced
666677
/// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
678+
#[stable(feature = "collections_range", since = "1.28.0")]
667679
pub trait RangeBounds<T: ?Sized> {
668680
/// Start index bound.
669681
///

0 commit comments

Comments
 (0)