3 ms·
> In order to guarantee that an addition will not overflow you need to check that the integer is below a certain value, for example. Or to be sure that indexing
by kibwen 2y ago
> In order to guarantee that an addition will not overflow you need to check that the integer is below a certain value, for example. Or to be sure that indexing an array will not be out of bounds you need to check the index value. For this last case Rust enables out of bounds access checks for Vecs in debug builds, but disables them for release builds.
This is backwards. In Rust, using the indexing operator on an array is a bounds-checked operation; the alternative is the unsafe .get_unchecked method. This is true regardless of debug mode or release mode, and there isn't any flag or configuration variable to override this.
It's integer overflow whose behavior (currently) changes between debug and release mode, where in debug mode it panics and in release mode it's defined to wrap around (and this can be configured as you please via a build flag).