Checking preconditions and postconditions

When testing, and even in our everyday code, we probably want to have some preconditions to our functions, or we might want to check some postconditions. We use assertions for this. They come in two variants, debug assertions and normal assertions.

Normal assertions will always be checked, but will slow down your production code since they need to run every time. Debug assertions will only run when compiling in debug mode, so you will be able to catch errors then, and the production code will run without performance issues.

In general, you should use all the debug assertions you can, and use normal assertions only in places where you receive output from the user or another crate (if building a library). ...

Get Rust High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.