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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of kinds

kinds

Photo of Rust

Rust

Photo of better code

better code

Related news:

News photo

Verify the Rust's Standard Library's 7,500 Unsafe Functions - and Win 'Financial Rewards'

News photo

Kibi – A text editor in less than 1024 lines of code, written in Rust

News photo

Rust's Sneaky Deadlock With `if let` Blocks