Skip to content

Improve range docs #77874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 52 additions & 40 deletions library/core/src/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ use crate::hash::Hash;
///
/// ```compile_fail,E0277
/// for i in .. {
/// // ...
/// // ...
/// }
/// ```
///
/// Used as a [slicing index], `RangeFull` produces the full array as a slice.
///
/// ```
/// let arr = [0, 1, 2, 3, 4];
/// assert_eq!(arr[ .. ], [0,1,2,3,4]); // RangeFull
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]); // This is the `RangeFull`
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
/// ```
///
/// [slicing index]: crate::slice::SliceIndex
Expand All @@ -52,22 +52,26 @@ impl fmt::Debug for RangeFull {
/// A (half-open) range bounded inclusively below and exclusively above
/// (`start..end`).
///
/// The `Range` `start..end` contains all values with `x >= start` and
/// `x < end`. It is empty unless `start < end`.
/// The range `start..end` contains all values with `start <= x < end`.
/// It is empty if `start >= end`.
///
/// # Examples
///
/// The `start..end` syntax is a `Range`:
///
/// ```
/// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
/// assert_eq!(3 + 4 + 5, (3..6).sum());
/// ```
///
/// ```
/// let arr = [0, 1, 2, 3, 4];
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
/// assert_eq!(arr[1.. 3], [ 1,2 ]); // Range
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why the comment // Range was here; it didn't make sense to me, so I removed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these must have been copy-pasted to all the Range* types? So the comment is probably to emphasize that that particular line is the one for this type.

Seems like this example should be maybe somewhere where we reference it from all the types, and call-out Range, RangeTo, etc on every line. I guess there's not a core::range module to put it on...

Maybe this example could go on RangeBounds, and all the Range* types could link over there as a way to jump between them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the comment is probably to emphasize that that particular line is the one for this type.

Ah, I see; thanks for clearing that up :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I just updated all the examples to make them consistent.

/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
/// assert_eq!(arr[1.. 3], [ 1, 2 ]); // This is a `Range`
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
/// ```
#[lang = "Range"]
#[doc(alias = "..")]
Expand Down Expand Up @@ -160,17 +164,21 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
///
/// # Examples
///
/// The `start..` syntax is a `RangeFrom`:
///
/// ```
/// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
/// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
/// ```
///
/// ```
/// let arr = [0, 1, 2, 3, 4];
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3,4]); // RangeFrom
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]); // This is a `RangeFrom`
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
/// ```
#[lang = "RangeFrom"]
#[doc(alias = "..")]
Expand Down Expand Up @@ -244,12 +252,12 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
///
/// ```
/// let arr = [0, 1, 2, 3, 4];
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
/// assert_eq!(arr[ .. 3], [0,1,2 ]); // RangeTo
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]); // This is a `RangeTo`
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
/// ```
///
/// [slicing index]: crate::slice::SliceIndex
Expand Down Expand Up @@ -310,17 +318,21 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
///
/// # Examples
///
/// The `start..=end` syntax is a `RangeInclusive`:
///
/// ```
/// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
/// assert_eq!(3 + 4 + 5, (3..=5).sum());
/// ```
///
/// ```
/// let arr = [0, 1, 2, 3, 4];
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]);
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
/// assert_eq!(arr[1..=3], [ 1,2,3 ]); // RangeInclusive
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]);
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]); // This is a `RangeInclusive`
/// ```
#[lang = "RangeInclusive"]
#[doc(alias = "..=")]
Expand Down Expand Up @@ -534,12 +546,12 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
///
/// ```
/// let arr = [0, 1, 2, 3, 4];
/// assert_eq!(arr[ .. ], [0,1,2,3,4]);
/// assert_eq!(arr[ .. 3], [0,1,2 ]);
/// assert_eq!(arr[ ..=3], [0,1,2,3 ]); // RangeToInclusive
/// assert_eq!(arr[1.. ], [ 1,2,3,4]);
/// assert_eq!(arr[1.. 3], [ 1,2 ]);
/// assert_eq!(arr[1..=3], [ 1,2,3 ]);
/// assert_eq!(arr[ .. ], [0, 1, 2, 3, 4]);
/// assert_eq!(arr[ .. 3], [0, 1, 2 ]);
/// assert_eq!(arr[ ..=3], [0, 1, 2, 3 ]); // This is a `RangeToInclusive`
/// assert_eq!(arr[1.. ], [ 1, 2, 3, 4]);
/// assert_eq!(arr[1.. 3], [ 1, 2 ]);
/// assert_eq!(arr[1..=3], [ 1, 2, 3 ]);
/// ```
///
/// [slicing index]: crate::slice::SliceIndex
Expand Down Expand Up @@ -661,9 +673,9 @@ impl<T: Clone> Bound<&T> {
}
}

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