Skip to content

Arithmetic assertions

Arithmetic assertions are those that compare two values together.

Equality

These assertions compare values that implement the PartialEq trait:

// Ensure that the values are equal
assert_eq!(x, y);

// Ensure that the values are inequal
assert_ne!(x, y);

Ordering

These assertions compare values that implement the PartialOrd trait:

// Ensure that x is less than y
assert_lt!(x, y);

// Ensure that x is less than or equal to y
assert_le!(x, y);

// Ensure that x is greater than y
assert_gt!(x, y);

// Ensure that x is greater than or equal to y
assert_ge!(x, y);

Details (advanced)

The two values do not have to be of the same type. If the first value is of type T and the second of type U, then the following traits must be implemented:

// for the equality assertions to work
impl PartialEq<U> for T { ... }

// for the order assertions to work
impl PartialOrd<U> for T { ... }

Both types, additionally, must implement the Debug trait.

Assertion Panic condition
assert_eq !x.eq(y)
assert_ne !x.ne(y)
assert_lt !x.lt(y)
assert_le !x.le(y)
assert_gt !x.gt(y)
assert_ge !x.ge(y)