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


#1

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.


#2

I’ll save you the trouble: https://github.com/rust-lang/rand/pull/9

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


#3

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.


#4

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


#5

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