Macro fluent_comparisons_macros::any_of[][src]

macro_rules! any_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 any of the values in the set.

Lazy Evaluation

If we write let cond = any_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 any_of!({1,some_func()}<5), the function some_func() is not invoked.

Macro Syntax and Examples

The macro is called as any_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.

use rand::prelude::*;

let square = |val|val*val;
// the following assertion holds
assert!(any_of!({4+4+1,square(7*2),120_i32.pow(2)}>8));

let v = vec![1, 2,3];
let mut rng = rand::thread_rng();
// the following assertion holds
assert!(any_of!( {rng.gen::<usize>(),v.len(),2,1+1,"hello world".len()} == v.len()));