inline_const

Macro const_array

Source
macro_rules! const_array {
    ( $($params:tt)* ) => { ... };
}
Expand description

Macro to generate an array by repeating a constant N times.

Since recently, [C; N] works for any constant C, even for non-Copy types. However, without inline consts, this is somewhat annoying to use. The const_array! macro helps with that situation:

use inline_const::const_array;
 
fn make_i32_vecs() -> [Vec<i32>; 5] {
    // [Vec::new(); 5]: rejected since `Vec::new` is not a constant.
    const_array![ [Vec<i32>] Vec::new(); 5]
}
fn make_vecs<T>() -> [Vec<T>; 5] {
    // Generic parameters used in the const expression must be explicitly specified:
    const_array![<T> [Vec<T>] Vec::new(); 5]
}