check 1.0.0

Convenience assert!-like macros which return instead of panicking
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented4 out of 4 items with examples
  • Size
  • Source code size: 7.66 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 646.22 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jkarns275 numero-744

Check

Convenience assert!-like macros which immediately return None or Err(...) instead of panicking.

In a function returning an Option<T>, invoke the macro with just enough parameters to get a condition to check.

check!(a < n);
check_eq!(a, b);

This will expand to:

if !(a < n) {
  return None;
}
if a != b {
  return None;
}

In a function returning a Result<T, E>, invoke the macro with an extra argument, which is the error to return if the check fails (and must have type E), just like you can add arguments to choose a panic message with assert!.

check!(a < n, MyError::TooBig);
check_eq!(a, b, MyError::NotEqual);

This will expand to:

if !(a < n) {
  return Err(MyError::TooBig);
}
if a != b {
  return Err(MyError::NotEqual);
}