3 ms·
These sorts of bugs seem like an inherent side effect of implicitness, in this case of implicit type casting in C/C++. Rust mostly solves this specific class of
by smaddox 8y ago
These sorts of bugs seem like an inherent side effect of implicitness, in this case of implicit type casting in C/C++. Rust mostly solves this specific class of bugs by requiring explicit type casts. As a consequence, in Rust, unlike in C/C++, it is idiomatic to use an unsigned integer when negative values are semantically invalid.
Rust also makes it fairly easy to define a `newtype` wrapper, for when you need semantics different from the primitive types. You can, for example, define an integer wrapper that always panics on overlow (as opposed to only in debug builds, which is the default for Rust). Recently, I've used this pattern to define a networking "sequence number" type that does not implement ordering operators (less than, greater than, etc.), but instead provides `older_than`, `newer_than`, etc. I made the switch after correcting multiple bugs caused by implicit and incorrect assumptions of total ordering; making the switch uncovered several more of such bugs . Of course, such a pattern is possible in C/C++ as well, but it is not as ergonomic and thus not as common.
- mcguire 8y ago"If you want to catch overflows on unsigned variables, use unsigned Z3 operations instead of signed, and do zero extend instead of sign extend."
- OpenBSD-reich2 8y agoSPARK exists, we don't need Rust. Most evangelized/shilled technology since systemd tbh.
- giornogiovanna 8y agoYou can (and in my opinion, should) take this further with range types, such as those implemented in Ada.
- smaddox 8y agoYeah, Rust doesn't have built-in range types, but it's fairly straightforward to write a macro to generate them. I've done this for array indices being passed over the network, in order to automate validation and minimize their bit-size on the wire.