-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Remove panic from to_digit and from_digit #30138
Comments
I think the original reasoning for this is that supplying a radix greater than 36 is a programmer error. These methods would always yield |
@apasel422 I think always yielding If these weren't stable, it might be possible to avoid programming errors with an "alphanumeric" enum ranging from My concern is with radices that originate from the program's input, since there's no statically verifiable way to avoid calling It's also worth noting that the names are backwards, since "digits" are used to represent numbers in a positional numeral system of a particular-radix, and thus our |
This also changes well-documented semantics of a stable method, we can't really do this. |
@steveklabnik Why not? They're dangerous semantics, and no one relies on them anyway. |
This is the burden of stability. We've made a promise to our users, and now we have to keep it. |
:\ well here's hoping for Rust 2.0 |
The panics in
from_digit
andto_digit
are rather arbitrary:Both functions already return a
None
when the number above the radix, and should also returnNone
rather than crashing the program when the radix is greater than 36. If a crash is still desired, theOption<char>
can simply be unwrapped.This change is very simple to make. Although both functions are stable, this panic is not encoded in their signatures, and nothing in the standard library relies on this hidden panic; in fact
from_digit
is alwaysunwrap
d, whileto_digit
is either unwrapped or matched a way compatible with the elimination of this panic (though an error message may need to be tweaked). A cursory search of GitHub suggests this is in fact the case everywhere else, with every invocation I could find either passing 2, 10, 16, or 36 as the radix, unwrapping the result, or manually checking whether the radix was greater than 36 and explicitly panicking if so.Since libcore is being stabilized right now, I'd offer that panicking in the core library should be minimized, since every possible panic reduces Rust's applicability and usefulness to baremetal systems where safety is a concern.
The text was updated successfully, but these errors were encountered: