Crates.io | chain-cmp |
lib.rs | chain-cmp |
version | 0.2.0 |
source | src |
created_at | 2020-09-10 18:17:44.762968 |
updated_at | 2020-09-10 18:17:44.762968 |
description | Chain comparison operators easily |
homepage | |
repository | https://github.com/isaacthefallenapple/chain_cmp |
max_upload_size | |
id | 287100 |
size | 9,306 |
Use the chmp!
macro to chain comparison operators like you can in Python, for example.
You can use all of these operators <
, <=
, >
, >=
, ==
, !=
inside a chmp!
invocation.
use chain_cmp::chmp;
let (a, b, c) = (1, 2, 3);
let verbose = a < b && b <= c;
let concise = chmp!(a < b <= c);
assert_eq!(concise, verbose);
// You can use equality operators as well:
assert!(chmp!(a != b != c));
// And you can even chain more than three operators:
assert!(chmp!(a != b != c != a)); // making sure these values are pairwise distinct
// And of course mix and match operators:
assert!(chmp!(a < b <= c != a == a));
chmp!
will short-circuit to evaluate the fewest expressions
possible.
fn panics() -> i32 {
panic!();
}
assert!(!chmp!(i32::MAX < i32::MIN < panics())); // this **won't** panic
As long as the comparison operators have the lowest precedence,
chmp!
will evaluate any expression, like variables, blocks,
function calls, etc.
const ANSWER: u32 = 42;
assert!(chmp!({
println!("Life, the Universe, and Everything");
ANSWER
} != 6 * 9 == 54));