O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Assert macros

The basic set has just two assertion macros for unit tests: assert! and assert_eq!. Both of these are quite simple. The assert! macro has two forms:

assert!(a==b); assert!(a==b, "{} was not equal to {}", a, b); 

The first form takes just a single Boolean value. If the value is false, the test run panics and shows the line where the failure happened.

The second form additionally takes in a format string and the corresponding number of variables. If the test fails, the test run panics, and in addition to showing the line number, displays the formatted text.

Even though assert! alone would be enough, comparing that two values are equal is such a typical case in unit tests. The assert_eq! macro does simply that: it takes two values ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required