Assert_eq
The Rust assert_eq! macro compares two expressions and panics when their evaluated values are unequal, reporting the compared values.
Learn Rust in a Month of Lunches
assert_eq! is a Rust macro that panics when two expressions evaluate to unequal values and reports both values.
- How it is used
- Uses equality assertions to encode guarantees in ordinary code, documentation examples, and automated tests.
- How deep it goes
- Explains comparison, panic behavior, custom messages, collection checks, and test diagnostics without detailing macro expansion internals.
- Examples
- Demonstrates even-number validation, Vec mutations, custom messages, and failed tests comparing 2 with 3.
- What you can do after
- Apply it to verify equal values and diagnose mismatches in Rust programs and tests.
Read how Learn Rust in a Month of Lunches explains assert_eq →
Refactoring to Rust
The Rust assert_eq! macro compares two values for equality and panics when they differ.
- How it is used
- Uses equality assertions to validate expected results in Rust tests and other Rust code.
- How deep it goes
- Explains comparison, panic behavior, captured failures, and Debug output, but does not cover macro internals.
- Examples
- Demonstrates 2 + 2 equaling 4, deliberately failing against 5, and inspecting output from passing and failing tests.
- What you can do after
- Apply it via Rust’s test framework to compare values and diagnose failures from reported left and right values.
Related concepts
- testuses · A test is a procedure or check that evaluates behavior using specified inputs or evidence against expected results or criteria.
- documentationused by · Documentation is recorded explanatory, instructional, or reference information that describes a subject, system, or process to support its understanding, use, or maintenance.
- paniccontrasts with · A panic is a runtime failure mechanism that abruptly stops ordinary execution when an unrecoverable error or violated assumption prevents safe continuation.
- arrayuses · An array is an indexed, ordered collection of elements arranged in one or more dimensions, with elements accessed by position.
- println! macroalternative to · The println! macro is an output operation that formats arguments and writes the resulting text followed by a line terminator to standard output.
- assertionalternative to · A software assertion is an executable check of an expected condition or property that signals failure when the check does not hold.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

