Repository navigation
Open
Labels
A-iteratorsArea: IteratorsArea: IteratorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-design-proposalStatus: This needs a clear design proposal and then a meeting with the team.Status: This needs a clear design proposal and then a meeting with the team.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Split off from #27741 because the stabilization path for step_by has moved to being on iterators (#41439), and thus not using the Step trait.
- Remove
step,steps_between, andis_negativeonce Range::step_by is deleted - Replace
replace_zeroandreplace_onewith something more useful (some options: Make RangeInclusive just a two-field struct (amend 1192) rfcs#1980 (comment)) - Change
steps_between_by_oneso thatRange<u128>can beTrustedLen(rather than it only working well with types that fit inusize) - Make a decision on how
steps_betweenshould work Step::steps_between does not distinguish overflow and unimplemented (unstable) #48117 - Should this live in
core::iterorcore::range?
(and probably more)
Reacted by Gurwinder Singh, Chris de Claverie, Jaanus Varus, bb010g, Jacob Pratt, Hans Christian Schmitz, Jeff Parsons, Cecile Tonglet, Igor Strebz, Jacob McCollum and 33 more
Reacted by Felipe S. S. Schneider, Alex Povel and jasper310899
Reacted by Felipe S. S. Schneider, Alex Povel, dzmitry-lahoda, Gabriel Bjørnager Jensen and jasper310899
Reacted by Felipe S. S. Schneider, Alex Povel and jasper310899
Reacted by Luo Jia / Zhouqi Jiang, Anders Evensen, Mateusz S. Szczygieł, robinhundt, cyqsimon, Eric Shimizu Karbstein, Heraclito-Q-Saldanha, Denis Varlakov, Forrest Li, MhhhxX and 7 more
Metadata
Metadata
Assignees
Labels
A-iteratorsArea: IteratorsArea: IteratorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-design-proposalStatus: This needs a clear design proposal and then a meeting with the team.Status: This needs a clear design proposal and then a meeting with the team.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
Rollup merge of rust-lang#42169 - scottmcm:new-step-trait-issue, r=al…
Rollup merge of rust-lang#42169 - scottmcm:new-step-trait-issue, r=al…
Rollup merge of rust-lang#42169 - scottmcm:new-step-trait-issue, r=al…
Rollup merge of rust-lang#42169 - scottmcm:new-step-trait-issue, r=al…
Rollup merge of rust-lang#42169 - scottmcm:new-step-trait-issue, r=al…
Rollup merge of rust-lang#42169 - scottmcm:new-step-trait-issue, r=al…
scottmcm commentedon Jun 9, 2017
Some progress on this in #42534
SimonSapin commentedon Jul 5, 2017
PR #43077 does items 1
and 3in the original message of this issue.SimonSapin commentedon Jul 6, 2017
I’ve removed the i128/u128 stuff from my PR because I suspect my mixed-signdeness mixed-width interger arithmetic was buggy. I also massaged the
Steptrait some more and came up with this:How does it look?
I’m a bit uncertain of the exact impls for integers, there is up to 6 cases to consider: {smaller, same width, larger} than usize/isize × {signed, unsigned}.
Auto merge of #43077 - SimonSapin:ranges, r=alexcrichton
SimonSapin commentedon Jul 8, 2017
I’ve tweaked this design some more and opened #43127. I believe that PR fixes every issue I know of around the
Steptrait.72 remaining items
Rollup merge of rust-lang#154825 - Lars-Schumann:non-zero-const-step,…
Unrolled build for #154825
Rollup merge of #154825 - Lars-Schumann:non-zero-const-step, r=jhpratt
Rollup merge of rust-lang#156629 - tgross35:range-module-stabilizatio…
Rollup merge of rust-lang#156629 - tgross35:range-module-stabilizatio…
Unrolled build for #156629
Auto merge of #153821 - Lars-Schumann:const-step, r=Mark-Simulacrum
RalfJung commentedon Sep 1, 2026
Do we really want to allow
forwardto saturate? I think that is very confusing and semantically wrong. There is no sense in which 255 is the successor of 255 (onNonZero<u8>). There is a sense in which 0 is the successor of 255 (onu8), so wrapping makes sense -- but saturating does not.If 255 has a successor on
NonZero<u8>, it would be 1 (that'd be my interpretation of "wrapping" for this type). That would be strictly better than saturating, IMO. But really I thinkforwardshould just always panic for such cases where there's no "good" successor: we should allow panic and wrapping, but nothing else. Asking for the successor of 255 onNonZero<u8>is clearly buggy code (it panics in debug builds after all). Falling back to wrapping arithmetic is an accepted trade-off when the performance cost of detecting the bug is too high, but falling back to pretty much nonsensical behavior (such as saturation) is so much worse that I don't think this is a good trade-off.Techcable commentedon Sep 5, 2026
I was worried about the performance impact of this, but at least on x86, wrapping behavior takes the same number of instructions as saturating behavior (and both are branchless):
See on godbolt: https://godbolt.org/z/qbxdfoGWG
khoover commentedon Sep 5, 2026
I think it's overly strict on the allowed behaviours when given an invalid increment. To me, it's a safe
forward_uncheckedmore than an unwrappedforward_checked, where you replace "UB" in the definition offorward_uncheckedwith "panics or returns an arbitrary value."In defense of saturation, though, for
NonZero<u8>theaddcworks if the increment is at most 256. But it can be anyusize, what happens when it wraps two or three times? Rather than figuring outmod 255, ifforwardalways saturates it doesn't matter how many times your increment would wrap, the answer is always 255. This holds for other bounded integer types too, e.g. aGridIndex(u8)that validates the input number is in[0, 99]; rather than calculatemod 100, can just saturate at 99 with asubandcmov.RalfJung commentedon Sep 6, 2026
"returns an arbitrary value" is a great way to break programs. That feels like a gross violation of the usual principles of API design that the standard library follows.
Lucky enough, the spec is nowhere near as permissive as that. But it allows "saturation", and that's already going too far IMO.