#const #init

macro no-std array-const-fn-init

Initializes an array with constant values calculated by a const fn

2 releases

0.1.1 May 20, 2020
0.1.0 May 20, 2020

#1147 in Data structures

Download history 660/week @ 2024-07-25 19530/week @ 2024-08-01 7954/week @ 2024-08-08 3257/week @ 2024-08-15 2135/week @ 2024-08-22 1649/week @ 2024-08-29 1005/week @ 2024-09-05 965/week @ 2024-09-12 637/week @ 2024-09-19 753/week @ 2024-09-26 719/week @ 2024-10-03 614/week @ 2024-10-10 697/week @ 2024-10-17 1191/week @ 2024-10-24 2064/week @ 2024-10-31 630/week @ 2024-11-07

4,717 downloads per month
Used in 27 crates (9 directly)

MIT/Apache

7KB
54 lines

array-const-fn-init

Initializes an array with constant values calculated by a const fn (usize) -> T

Requires Rust >= 1.45

Example

use array_const_fn_init::array_const_fn_init;

const fn const_double_it(i: usize) -> usize {
    i * 2
}
const ARRAY: [usize; 10] = array_const_fn_init![const_double_it; 10];
assert_eq!(ARRAY, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
use array_const_fn_init::array_const_fn_init;

const fn const_vecs(i: usize) -> (u8, u8, u8) {
    (i as u8, i as u8, i as u8)
}
const ARRAY: [(u8, u8, u8); 4] = array_const_fn_init![const_vecs; 4];
assert_eq!(ARRAY, [(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3)]);

lib.rs:

Initializes an array with constant values calculated by a const fn

Examples

use array_const_fn_init::array_const_fn_init;

const fn const_double_it(i: usize) -> usize {
    i * 2
}
const ARRAY: [usize; 10] = array_const_fn_init![const_double_it; 10];
assert_eq!(ARRAY, [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
use array_const_fn_init::array_const_fn_init;

const fn const_vecs(i: usize) -> (u8, u8, u8) {
    (i as u8, i as u8, i as u8)
}
const ARRAY: [(u8, u8, u8); 4] = array_const_fn_init![const_vecs; 4];
assert_eq!(ARRAY, [(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3)]);

No runtime deps