Get the latest tech news
Rust's Two Kinds of 'Assert' Make for Better Code
Daniel Lemire’s recent post “runtime asserts are not free” looks at the run-time cost of assert statements in C and shows that a simple assert in a frequently executed loop can cause significant overhead. My own opinion on assertions has shifted over the years, from “I don’t see the point” to “use them sparingly” to “use them as much as possible”.
An assertion is thus particularly helpful for future programmers, especially when they’re refactoring code, to give them greater confidence that they haven’t broken the program in subtle ways. I long considered this inevitable, but when I moved to Rust several years back, I slowly realised that I now had access to two kinds of assert statement. Each time I write assert I think about the possible performance impact, and also about whether there’s a way I can increase my confidence in the assumption to the point that I can downgrade it to a debug_assert.
Or read this on Hacker News