Skip to content

Step::steps_between does not distinguish overflow and unimplemented (unstable) #48117

Open

Description

@scottmcm

It returns Option<size>: https://doc.rust-lang.org/std/iter/trait.Step.html#tymethod.steps_between

And the comment says it uses None for overflow: https://doc.rust-lang.org/src/core/iter/range.rs.html#29

But it also uses None for unimplemented https://doc.rust-lang.org/src/core/iter/range.rs.html#155

And as a result, Range needs to return (0, None) when it gets None: https://doc.rust-lang.org/src/core/iter/range.rs.html#235-240

It would be nice to either

  • Require that the method is implemented accurately (easy for fundamental integers; harder if Step is expected to support graph walks, but maybe not harder than PartialOrd)
  • Change the type to something else, maybe Option<Option<usize>> or be more hint-like with (usize, Option<usize>)
  • Something else

cc #42168

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-libs-api[DEPRECATED; DO NOT USE]
    on Feb 10, 2018
  2. CAD97 commented on May 15, 2020

    @CAD97
    Contributor

    The links appear to have lost their original position.

    Does the original concern still stand? (Does it stand with #69659?)

  3. scottmcm commented on Aug 4, 2020

    @scottmcm
    MemberAuthor

    @CAD97 Yes, I think there's still essentially the same problem -- the Invariants section still has None for both unreachable (steps_between(1, 0)) and overflow (steps_between(0_u128, 1<< 127)).

    So if this is used for size_hint, then getting None back from steps_between means you're stuck returning (0, None), when you'd rather be able to return either (0, Some(0)) or (usize::MAX, None).

  4. scottmcm commented on Jul 12, 2026

    @scottmcm
    MemberAuthor

    Looks like this was updated, but I realized it's unclear, at least to me, what to do if it's actually unreachable -- should that be marked distinctly? Step only requires PartialOrd, not Ord, so a and b might both have successors and predecessors but neither is reachable from the other.

    Or if (0, None) is always a legal response, shouldn't it be a provided implementation like Iterator::step_by?

  5. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions