Skip to content

Commit e42cbe8

Browse files
committed
Auto merge of #77874 - camelid:range-docs-readability, r=scottmcm
Improve range docs * Improve code formatting and legibility * Various other readability improvements
2 parents b1496c6 + a885c50 commit e42cbe8

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
@@ -23,20 +23,20 @@ use crate::slice::index::{
2323
///
2424
/// ```compile_fail,E0277
2525
/// for i in .. {
26-
/// // ...
26+
/// // ...
2727
/// }
2828
/// ```
2929
///
3030
/// Used as a [slicing index], `RangeFull` produces the full array as a slice.
3131
///
3232
/// ```
3333
/// let arr = [0, 1, 2, 3, 4];
34-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
35-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
36-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
37-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
38-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
39-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
34+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); // This is the `RangeFull`
35+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
36+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
37+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
38+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
39+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
4040
/// ```
4141
///
4242
/// [slicing index]: crate::slice::SliceIndex
@@ -56,22 +56,26 @@ impl fmt::Debug for RangeFull {
5656
/// A (half-open) range bounded inclusively below and exclusively above
5757
/// (`start..end`).
5858
///
59-
/// The `Range` `start..end` contains all values with `x >= start` and
60-
/// `x < end`. It is empty unless `start < end`.
59+
/// The range `start..end` contains all values with `start <= x < end`.
60+
/// It is empty if `start >= end`.
6161
///
6262
/// # Examples
6363
///
64+
/// The `start..end` syntax is a `Range`:
65+
///
6466
/// ```
6567
/// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
6668
/// assert_eq!(3 + 4 + 5, (3..6).sum());
69+
/// ```
6770
///
71+
/// ```
6872
/// let arr = [0, 1, 2, 3, 4];
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 ]); // Range
74-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
73+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
74+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
75+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
76+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
77+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]); // This is a `Range`
78+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
7579
/// ```
7680
#[lang = "Range"]
7781
#[doc(alias = "..")]
@@ -164,17 +168,21 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
164168
///
165169
/// # Examples
166170
///
171+
/// The `start..` syntax is a `RangeFrom`:
172+
///
167173
/// ```
168174
/// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
169175
/// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
176+
/// ```
170177
///
178+
/// ```
171179
/// let arr = [0, 1, 2, 3, 4];
172-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
173-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
174-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
175-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
176-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
177-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
180+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
181+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
182+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
183+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); // This is a `RangeFrom`
184+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
185+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
178186
/// ```
179187
#[lang = "RangeFrom"]
180188
#[doc(alias = "..")]
@@ -248,12 +256,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
248256
///
249257
/// ```
250258
/// let arr = [0, 1, 2, 3, 4];
251-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
252-
/// assert_eq!(arr[ .. 3], [0,1,2 ]); // RangeTo
253-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
254-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
255-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
256-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
259+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
260+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]); // This is a `RangeTo`
261+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
262+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
263+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
264+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
257265
/// ```
258266
///
259267
/// [slicing index]: crate::slice::SliceIndex
@@ -314,17 +322,21 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
314322
///
315323
/// # Examples
316324
///
325+
/// The `start..=end` syntax is a `RangeInclusive`:
326+
///
317327
/// ```
318328
/// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
319329
/// assert_eq!(3 + 4 + 5, (3..=5).sum());
330+
/// ```
320331
///
332+
/// ```
321333
/// let arr = [0, 1, 2, 3, 4];
322-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
323-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
324-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
325-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
326-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
327-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]); // RangeInclusive
334+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
335+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
336+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
337+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
338+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
339+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); // This is a `RangeInclusive`
328340
/// ```
329341
#[lang = "RangeInclusive"]
330342
#[doc(alias = "..=")]
@@ -538,12 +550,12 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
538550
///
539551
/// ```
540552
/// let arr = [0, 1, 2, 3, 4];
541-
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
542-
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
543-
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]); // RangeToInclusive
544-
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
545-
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
546-
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
553+
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
554+
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
555+
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); // This is a `RangeToInclusive`
556+
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
557+
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
558+
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
547559
/// ```
548560
///
549561
/// [slicing index]: crate::slice::SliceIndex
@@ -665,9 +677,9 @@ impl<T: Clone> Bound<&T> {
665677
}
666678
}
667679

668-
#[stable(feature = "collections_range", since = "1.28.0")]
669680
/// `RangeBounds` is implemented by Rust's built-in range types, produced
670681
/// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
682+
#[stable(feature = "collections_range", since = "1.28.0")]
671683
pub trait RangeBounds<T: ?Sized> {
672684
/// Start index bound.
673685
///

0 commit comments

Comments
 (0)