[][src]Macro common_macros::const_expr_count

macro_rules! const_expr_count {
    () => { ... };
    ($e:expr) => { ... };
    ($e:expr; $($other_e:expr);*) => { ... };
    ($e:expr; $($other_e:expr);* ; ) => { ... };
}

Counts the number of ; separated expressions passed in to the macro at compiler time.

This does count expressions containing other expressions just as one expression.

The macro can be used to e.g. get the right value for a .with_capacity() constructor at compiler time, i.e. it's normally used only by other macros.

Examples

use common_macros::const_expr_count;

macro_rules! my_vec {
    ($($item:expr),*) => ({
        let mut vec = Vec::with_capacity(const_expr_count! {$($item);*});
        $(
            //let's forget to insert the values
            if false {
                vec.push($item);
            }
        )*
        vec
    });
}

fn main() {
    let vec = my_vec![1u8,2,3,4];
    assert!(vec.capacity() >= 4);
}