Skip to content

Implement the Iterator trait for RangeTo for consistency #20587

@aochagavia

Description

@aochagavia

Currently, Range and RangeFrom implement the Iterator trait, butRangeTo doesn't.

This leads to the following situation:

// Works.
for _ in 0..10 {

}

// Works. Note that this is an infinite loop
for _ in 0.. {

}

// Doesn't work. RangeTo does not implement the Iterator trait
for _ in ..10 {

}

The problem of RangeTo is that there is no way to know which value is considered to be zero (the old Zero trait was removed a couple of months ago, so we cannot add it as a bound of the ).

A first solution that comes to my mind is something like

// This can be trivially implemented for the integer types
trait Enumerable : Step {
    fn min() -> Self;
    fn zero() -> Self;
    fn max() -> Self;
}

impl<Idx: Clone + Enumerable> Iterator for RangeFrom<Idx> {
    type Item = Idx;

    #[inline]
    fn next(&mut self) -> Option<Idx> {
        let zero: Idx = Enumerable::zero();
        // etc
    }
}

However, this may require an RFC and it is possible that someone has a simpler solution (I hope it).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions