Inherent Unchecked Integer Methods in Rust
Core Problem
The unchecked_* methods on integers have been available since Rust's stable version, but there are still unresolved questions and debates about their usage and implications.
Solution & Analysis
Steps / History
- [x] #69659 added
unchecked_{add,sub,mul} - [x] #85096
- [x] #85703
- [x] #103456
- [x] #115626 added
unchecked_neg - [x] #122520
Unresolved Questions
- Should the other unchecked intrinsics like
exact_divalso get inherent versions? - From #85703, consider if distinguishing the different kinds of ub could be useful (UB from overflow like
MIN/-1orMAX+1, LLVM'sn[us]w; UB from input range likex/0orx << -1; UB from lossy like2/4or3 >> 1, LLVM'sexact)
Resolved unresolved questions:
IsWe stabilisedunchecked_*the best naming for these?unchecked_{add,sub,mul}already, so, yes.
Final commenting period (FCP) and Stabilization PR
Once the feature has gone through a few release cycles and there are no unresolved questions left, the feature might be ready for stabilization.
Conclusion
The inherent unchecked integer methods provide an additional layer of performance in certain cases. However, their usage should be carefully considered to avoid potential issues like overflowing or underflowing values.