Mastodon Feed: Post

Mastodon Feed

Boosted by rust@mas.to ("Rust tips"):
hatchet@infosec.exchange ("Jacob Lindahl ☕️") wrote:

#RustProTip №38: The debug_assert! macro

The debug_assert! macro creates an assertion only in debug builds. It is useful for confirming assumptions during development without risking performance degradation in the release build.

Playground: https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=c07b46949bdedf702cb4649b3504776c
Docs: https://doc.rust-lang.org/std/macro.debug%5Fassert.html

All Rust Pro Tips: https://geeklaunch.io/blog/rust-pro-tips-collection/

#rust #rustlang #programming

A screenshot of the following Rust code: rust impl Rectangle {     pub fn new(width: u32, height: u32) -> Self {         debug_assert!(             u32::MAX / width >= height,             "area calculation will overflow",         );         Self { width, height }     }     pub fn area(&self) -> u32 {         self.width * self.height     } }