Skip to content

make it clear that Range cannot represent arbitrary ranges - #162110

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:range
Sep 1, 2026
Merged

make it clear that Range cannot represent arbitrary ranges#162110
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:range

Conversation

@RalfJung

@RalfJung RalfJung commented Sep 1, 2026

Copy link
Copy Markdown
Member

This seems like a useful clarification, though it is already heavily implied by the first sentence in the docs.

What is less clear to me is what type one should use to represent arbitrary ranges. Should we recommend (Bound<T>, Bound<T>) for that purpose? It seems best-stuied, and is already used for that purpose by the btree collections and in #136903. However, I suspect it is also common that one wants to represent an arbitrary range that's bounded on both sides. RangeInclusive can represent all those (though representing the empty range is a bit awkward), Range cannot.

Nominating for t-libs to get their general vibe on what we want to recommend here, if anything.

@RalfJung RalfJung added the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 1, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 1, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 1, 2026
@rustbot

rustbot commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

r? @nia-e

rustbot has assigned @nia-e.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, joboet, nia-e

Comment thread library/core/src/ops/range.rs Outdated
@maxdexh

maxdexh commented Sep 1, 2026

Copy link
Copy Markdown
Member

Technically RangeInclusive cannot represent the empty range for singleton types, such as PhantomData, [T; 0] or Option<!> (which are still PartialOrd and support is_empty)

@maxdexh

maxdexh commented Sep 1, 2026

Copy link
Copy Markdown
Member

And (Bound<!>, Bound<!>) has to represent the empty range as (Unbounded, Unbounded) 🙃

@RalfJung

RalfJung commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

With something like #136903, it may actually make a lot of sense to recommend (Bound<T>, Bound<T>) for the use-case of "I want to represent some arbitrary range".

@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 1, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

Vibeck from libs meeting was: all good, there are various changes to be made in the future like maybe having a discoverable type alias for (Bound<T>, Bound<T>), but ultimately that's not needed right now.

One potential addition that could be helpful is clarifying that this only prevents you from including the very last element for a type, and in cases where you know that can't happen (e.g. slice bounds, which are explicitly limited to isize::MAX as usize) it is still preferable to use Range for its performance.

@nia-e

nia-e commented Sep 1, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit baf382f has been approved by nia-e

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 1, 2026
@nia-e

This comment was marked as off-topic.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 1, 2026
make it clear that Range cannot represent arbitrary ranges

This seems like a useful clarification, though it is already heavily implied by the first sentence in the docs.

What is less clear to me is what type one *should* use to represent arbitrary ranges. Should we recommend `(Bound<T>, Bound<T>)` for that purpose? It seems best-stuied, and is already used for that purpose by the btree collections and in rust-lang#136903. However, I suspect it is also common that one wants to represent an arbitrary range that's bounded on both sides. `RangeInclusive` can represent all those (though representing the empty range is a bit awkward), `Range` cannot.

Nominating for t-libs to get their general vibe on what we want to recommend here, if anything.
rust-bors Bot pushed a commit that referenced this pull request Sep 1, 2026
…uwer

Rollup of 11 pull requests

Successful merges:

 - #151618 (rustdoc: add `--print` option)
 - #161287 (Update `icu_list` dependency to 2.3)
 - #161767 (change DEFAULT_STACK_SIZE to be 32MB on s390x)
 - #161968 (Diverse offload fixes)
 - #161971 (Remove -Zsaturating-float-casts flag)
 - #162071 (Fix ICE of getting item name from RPITIT)
 - #161209 (Rework `next_power_of_two` to always be `1 << …`)
 - #162073 (Change some `Infallible` to `!` in std)
 - #162086 (Remove `gate_check` from `AttributeStability::Unstable`)
 - #162102 (`alloc` crate: shrink undocumented `unsafe` blocks)
 - #162110 (make it clear that Range cannot represent arbitrary ranges)
@rust-bors
rust-bors Bot merged commit 92f9694 into rust-lang:main Sep 1, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 1, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 1, 2026
Rollup merge of #162110 - RalfJung:range, r=nia-e

make it clear that Range cannot represent arbitrary ranges

This seems like a useful clarification, though it is already heavily implied by the first sentence in the docs.

What is less clear to me is what type one *should* use to represent arbitrary ranges. Should we recommend `(Bound<T>, Bound<T>)` for that purpose? It seems best-stuied, and is already used for that purpose by the btree collections and in #136903. However, I suspect it is also common that one wants to represent an arbitrary range that's bounded on both sides. `RangeInclusive` can represent all those (though representing the empty range is a bit awkward), `Range` cannot.

Nominating for t-libs to get their general vibe on what we want to recommend here, if anything.
@RalfJung
RalfJung deleted the range branch September 4, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants