3 ms·
That's... exactly right, it's the entire point. Assertions, check if a condition is true, and if it is false print an error message and exit the program. Asser
by gpm 7y ago
That's... exactly right, it's the entire point.
Assertions, check if a condition is true, and if it is false print an error message and exit the program. Assertions in rust are written `assert!(expr, optional_formatting_stuff)`.
Debug assertions do the same, but they are only executed when the `debug_assertions` flag is passed to the compiler, typically for performance reasons. In C terms, a debug assert is basically `#ifdef DEBUG_ASSERRTIONS assert(foo) #endif`. Debug assertiosn in rust are written `debug_assert!(expression, optional_formatting_stuff)`.
"Formally", arithmetic overflow in rust is defined to either wrap (as per 2s complement arithmetic) or panic.
In todays compilers that means they perform 2s complement artihmetic when debug assertions are turned off, and they panic when it is turned on. If performance of checking for overflows improves in the future, it will be considered an acceptable (backwards compatible) change to always panic instead, but that would have too large a performance impact today.