Macro fluent_comparisons_macros::none_of [−][src]
macro_rules! none_of { ( {$($lh_sides:expr),+} $operator:tt $rhs:expr) => { ... }; }
Compare all values in a set to a common right hand side and decide whether the comparison returns true
for none of the values in the set.
Lazy Evaluation
If we write let cond = none_of!({a,b}<c)
, this is equivalent to the hand coded let cond = (a<c) && (b<c)
. That means that the comparisons are
evaluated lazily from left to right. Once
the truth value of the expression can be determined, the evaluation stops. That means that e.g. for an expression none_of!({1,some_func()}<5)
,
the function some_func()
is not invoked.
Macro Syntax and Examples
The macro is called as none_of!({/*list of expressions*/} operator rhs)
, where operator can be any of the binary comparison operators, i.e.
==
, !=
, <=
, <
, >
, and >=
. The list of expressions on the left hand side is comma separated without a trailing comma. The right hand side
is an expression as well. The list of expressions can have a variadic number of elements but must have at least one. It must always be enclosed in
curly braces. The expressions on the left hand side need not be of the same type, but the comparison with the right hand side must be valid. In particular,
the expressions need not be numeric.
Examples
The following examples show how to use the macro.
let square = |val|val*val; // the following assertion holds assert!(none_of!({4+4+1,square(7*2),120_i32.pow(2)}<0)); let v = vec![1, 2,3,4,5]; // the following assertion holds assert!(none_of!( {square(2),v.len() as i32,2,1+1,"hello".len() as i32} > v.len() as i32));