-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decrementing range behaviour is very confusing when used with unsigned integers #14020
Comments
It's not a decrementing range. The type of |
@thestinger This does expose an interesting problem with the range_step function, though. As far as I'm concerned, the primary usecase is to do reverse iteration (which seems to have been deemed "confusing" for the range function to support), for which you need range_step(,,-1). Thus, all of the arguments have to be signed. However, if I want to iterate over unsigned integers, I can't reasonably always convert them to signed integers. One would reasonably expect to be able to iterate from uint::MAX to uint::MIN, but as it stands, none of the range functions seem to permit this. Or am I mistaken? Edit: I mean, you can just use range().rev() to accomplish this, but it's odd that that's necessary. |
@gankro: Reverse iteration with a step of 1 can be done with |
fix: Fix assoc item search finding unrelated definitions Fixes rust-lang/rust-analyzer#14014
) Closes rust-lang#14020 Closes rust-lang#14290 Closes rust-lang#14091 Add checks for unstable const traits. changelog: [`missing_const_for_fn`] fix FP on unstable const traits
Range behaviour differs significantly and unexpectedly from expected behaviour if the range start is an unsigned integer.
This prints 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.
(range_start is a uint, not an int)
This prints '10', and stops. This is not expected.
This appears to be because the type signature for range_step uses the same generic parameter for all three argument. Thus, if the start is a uint then the others are all interpreted as uints, the negative numbers are interpreted as very large ints, the range increments by 2^64-1 up to a max of 2^64-1, rather than decrementing by -1, and the range immediately finishes.
This seems like a relatively common operation, and it'd be nice if the rust compiler could catch the incorrect types in this code, and/or the range_step method could properly handle mixing signed and unsigned parameters to the method.
(N.b. I am relatively new to rust, sorry if there's something obvious I'm missing that invalidates this entirely)
The text was updated successfully, but these errors were encountered: