Skip to content

stabilize new RangeFrom type and iterator#153380

Open
pitaj wants to merge 2 commits intorust-lang:mainfrom
pitaj:stabilize-new_range_from_api
Open

stabilize new RangeFrom type and iterator#153380
pitaj wants to merge 2 commits intorust-lang:mainfrom
pitaj:stabilize-new_range_from_api

Conversation

@pitaj
Copy link
Contributor

@pitaj pitaj commented Mar 4, 2026

// in core and std
pub mod range;

// in core::range

pub struct RangeFrom<Idx> {
    pub start: Idx,
}

impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> { /* ... */ }

impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
    pub const fn contains<U>(&self, item: &U) -> bool
    where
        Idx: [const] PartialOrd<U>,
        U: ?Sized + [const] PartialOrd<Idx>;
}

impl<Idx: Step> RangeFrom<Idx> {
    pub fn iter(&self) -> RangeFromIter<Idx>;
}

impl<T> const RangeBounds<T> for RangeFrom<T> { /* ... */ }
impl<T> const RangeBounds<T> for RangeFrom<&T> { /* ... */ }

impl<T> const From<RangeFrom<T>> for legacy::RangeFrom<T> { /* ... */ }
impl<T> const From<legacy::RangeFrom<T>> for RangeFrom<T> { /* ... */ }

pub struct RangeFromIter<A>(/* ... */);

impl<A: Step> RangeFromIter<A> {
    pub fn remainder(self) -> RangeFrom<A>;
}

impl<A: Step> Iterator for RangeFromIter<A> {
    type Item = A;
    /* ... */
}

impl<A: Step> FusedIterator for RangeFromIter<A> { }
impl<A: Step> IntoIterator for RangeFrom<A> {
    type Item = A;
    type IntoIter = RangeFromIter<A>;
    /* ... */
}

unsafe impl<T> const SliceIndex<[T]> for range::RangeFrom<usize> {
    type Output = [T];
    /* ... */
}
unsafe impl const SliceIndex<str> for range::RangeFrom<usize> {
    type Output = str;
    /* ... */
}

impl ops::Index<range::RangeFrom<usize>> for CStr {
    type Output = CStr;
    /* ... */
}

Tracking issue: #125687

r? tgross

stabilizes `core::range::RangeFrom`
stabilizes `core::range::RangeFromIter`
@rustbot

This comment was marked as resolved.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 4, 2026
Comment on lines -419 to +428
/// *Note*: Overflow in the [`Iterator`] implementation (when the contained
/// *Note*: Overflow in the [`IntoIterator`] implementation (when the contained
/// data type reaches its numerical limit) is allowed to panic, wrap, or
/// saturate. This behavior is defined by the implementation of the [`Step`]
/// trait. For primitive integers, this follows the normal rules, and respects
/// the overflow checks profile (panic in debug, wrap in release). Note also
/// that overflow happens earlier than you might assume: the overflow happens
/// in the call to `next` that yields the maximum value, as the range must be
/// set to a state to yield the next value.
/// the overflow checks profile (panic in debug, wrap in release). Unlike
/// its legacy counterpart, the iterator will only panic after yielding the
/// maximum value when overflow checks are enabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 307 to 311
/// Returns the remainder of the range being iterated over.
#[inline]
#[rustc_inherit_overflow_checks]
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_from_api", since = "CURRENT_RUSTC_VERSION")]
pub fn remainder(self) -> RangeFrom<A> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This could probably use an example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added examples for RangeInclusive::remainder, RangeFrom::remainder, and Range::remainder

@tgross35
Copy link
Contributor

tgross35 commented Mar 4, 2026

It doesn't look like RangeFromIter::remainder was listed on the tracking issue and that's new compared to the existing RangeFrom, so nominating for @rust-lang/libs-api. If you'd prefer to drop it here then I'm happy to approve.

@rustbot label +I-libs-api-nominated

The current RangeFrom has Index<RangeFrom<usize>> for ByteString and IndexMut<RangeFrom<usize>> for ByteString which should get added, those are unstable so it can happen here or later.

@rustbot rustbot added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Mar 4, 2026
@tgross35
Copy link
Contributor

tgross35 commented Mar 4, 2026

Noticing that RangeInclusiveIter::remainder is in the same boat, and is stable on nightly since #150522.

@pitaj
Copy link
Contributor Author

pitaj commented Mar 7, 2026

remainder was in the original RFC: https://github.com/rust-lang/rfcs/blob/master/text/3550-new-range.md#iterator-types
So I don't think this need further discussion by T-libs-api?

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 7, 2026

☔ The latest upstream changes (presumably #153544) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants