[Pre-RFC] Change the Range struct to support inclusive ranges

Regardless of how range syntax will eventually work, the underlying data structure needs to support both inclusive and exclusive ranges. Therefore, I propose (as others have, IIRC) that the structure be changed to:

pub enum Bound<T> {
    Inclusive(T),
    Exclusive(T),
}
pub struct Range<T> {
    pub start: Bound<T>,
    pub end: Bound<T>,
}
pub struct RangeFrom<T> {
    pub start: Bound<T>,
}
pub struct RangeTo<T> {
    pub end: Bound<T>,
}
pub struct RangeFull;

My motivation is that Range is a very useful primitive and I don’t want to end up in a situation where libraries start defining their own ranges because they need inclusive boundaries.

My specific use case is that I would like to replace the rand crate’s custom Range with a standard Range.

1 Like

I'll save you the trouble: Range syntax distribution by oli-obk · Pull Request #9 · rust-random/rand · GitHub

I fully agree that something needs to be done about the ranges though. The other thread kind of died.

The current ranges support both Range<i32> and Range<Bound<i32>>, where Bound is an enum like that. Isn’t that flexibility superior? I understand it costs an inconvenience to use the explicit bounded form.

A prime use case is BTreeMap::range, it doesn’t use any kind of range values today.

No. That flexibility means there are two different ways to write the same range.

Submitted https://github.com/rust-lang/rfcs/pull/952

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.