Introducing all_asserts 2.1.0

Published on Jun 25, 2020 | Updated on Feb 4, 2022

The crate all_asserts has just reached a new release 🎉. It introduces a new macro assert_range! which enables you to find out if a value (which implements the PartialOrd trait), is within a certain range.

You can view the source code for this crate here.

First add this to your Cargo.toml (if you haven’t already):

all_asserts = "2.1.0"

Let us go through a few trivial examples:

use all_asserts::assert_range;
fn main() {
    assert_range!((10..20), 20);
}

This outputs something like:

thread 'main' panicked at 'assertion failed: 
`20 is not in range of 10..20` - it should have been in this range, src/main.rs:292:5

You can even use decimal ranges! For example:

use all_asserts::{assert_range, assert_nrange};
fn main() {
  assert_range!((1.0..=2.0), 1.5);
  // You can also add a debug message if the assertion fails
  assert_nrange!(
    (1.0..=2.0), 1.5, "Oops! 1.5 is in the interval [1.0,2.0]"
  );
}

This outputs:

thread 'main' panicked at 'assertion failed: `1.5 is in range of 1.0..2.0` - it should not have been in this range: Oops! 1.5 is in the interval [1.0,2.0)', src/main.rs:295:5

With these new macros, you can write more advanced tests. And, that’s all that I’ve got for this release! Stay tuned!