You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe that the step_by(_) method of ranges should panic! if the argument is zero. Currently it loops forever, which is a footgun. At the very least, iterating over such a StepBy should panic! on debug builds. However, I think the earlier the error appears, the better.
Note that this may require us to specialize step_by to types whose Add implementation have a known identity element – at the moment that would mean bounding by the num::Zero trait, which is unstable, also it would probably reduce the usefulness of ranges to numeric types. However, special casing numeric ranges could solve this.
I have also filed an issue at rust-clippy to create a lint for cases where it can be statically detected. Still, that lint cannot detect cases where the argument is calculated at runtime.
The documentation of .step_by(_) should at least warn of the possibility of looping forever or panic once it is implemented.
The text was updated successfully, but these errors were encountered:
There is nothing unsafe about looping forever, so I strongly oppose a panic!(). As a user I would expect (a..b).step_by(c).take(n) to work for all c, without having to special case for c == 0..
@Thiez: Agreed, this is not inherently unsafe. So maybe panic!() on building the StepBy is not the right option. Still, there are enough options to loop forever already, and as I said, the docs should at least remind the reader that supplying 0 as an argument creates an infinite range.
It may also be useful to be able to issue a warning on debug builds iff a `StepBy { step_by: 0, .. } is actually iterated; perhaps as an additional Debug option.
This is currently intended behavior, and for now I'm going to close this in favor of the discussion in stabilizing step_by as this is definitely a useful data point, but perhaps not big enough to have its own issue right now.
I believe that the step_by(_) method of ranges should
panic!
if the argument is zero. Currently it loops forever, which is a footgun. At the very least, iterating over such a StepBy shouldpanic!
on debug builds. However, I think the earlier the error appears, the better.Note that this may require us to specialize
step_by
to types whoseAdd
implementation have a known identity element – at the moment that would mean bounding by thenum::Zero
trait, which is unstable, also it would probably reduce the usefulness of ranges to numeric types. However, special casing numeric ranges could solve this.I have also filed an issue at rust-clippy to create a lint for cases where it can be statically detected. Still, that lint cannot detect cases where the argument is calculated at runtime.
The documentation of
.step_by(_)
should at least warn of the possibility of looping forever or panic once it is implemented.The text was updated successfully, but these errors were encountered: