liveBook · Manning

Assertion

As explained in Refactoring to Rust

An assertion is a program check that verifies a condition and reports failure when the condition is false.

An assertion checks a condition during program execution and reports failure when the condition is false. Rust’s assert! macro validates a Boolean condition; a failed check panics the running thread. section 7.1

Checking a calculation

A test can calculate a result and verify it with assert_eq!(result, 4). If the values differ, the test fails and reports both values. Use assert! when the condition itself is Boolean, such as assert!(condition). section 7.1

How Refactoring to Rust covers it

How deep it goes
Explains Boolean and equality checks, panic behavior, failure output, and debugging values without covering assertion implementation internals.
Examples
Demonstrates 2 + 2 assertions, a deliberately failing equality check, and Cargo test failure output.
What you can do after
Apply assertions in Rust tests to validate conditions and inspect reported values when checks fail.
How it connects
Connects assertions with automated testing, panics, test failures, and debugging output.

Also explained in

Compare how 15 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.