macro arr_macro_impl

Private impl crate for arr_macro

6 releases

0.2.1 Jan 5, 2023
0.2.0 Jan 5, 2023
0.1.3 Oct 29, 2019
0.1.2 May 23, 2019
0.1.0 Dec 23, 2018
Download history 23026/week @ 2024-07-19 30011/week @ 2024-07-26 24469/week @ 2024-08-02 24222/week @ 2024-08-09 30535/week @ 2024-08-16 32592/week @ 2024-08-23 29861/week @ 2024-08-30 27261/week @ 2024-09-06 22661/week @ 2024-09-13 17664/week @ 2024-09-20 20919/week @ 2024-09-27 23083/week @ 2024-10-04 20230/week @ 2024-10-11 23535/week @ 2024-10-18 22625/week @ 2024-10-25 19478/week @ 2024-11-01

91,390 downloads per month
Used in 59 crates (via arr_macro)

MIT/Apache

6KB

Array Macro Build Status crates.io badge

Array macro helps initialize arrays. It is useful when initializing large arrays (greater than 32 elements), or arrays of types which do not implement the copy or default traits.

Array macro is implemented in 100% safe Rust.

For further background on the motivation behind this crate, check out this blog post.

Usage

use arr_macro::arr;

fn main() {
    let x: [Option<String>; 3] = arr![None; 3];
    assert_eq!(
        [None, None, None],
        x
    );

    // works with all enum types (and impl copy is not required)
    #[allow(dead_code)]
    enum MyEnum {
        A,
        B
    }
    let _: [MyEnum; 33] = arr![MyEnum::A; 33];

    // Vec::new()
    let _: [Vec<String>; 33] = arr![Vec::new(); 33];

    // or your own struct type
    // and you can even use a counter to behave differently based on the array index
    #[derive(Debug)]
    struct MyStruct {
        member: u16,
    }
    impl MyStruct {
        fn new(member: u16) -> Self {
            MyStruct { member }
        }
    }
    let mut i = 0u16;
    let x: [MyStruct; 33] = arr![MyStruct::new({i += 1; i - 1}); 33];

    assert_eq!(0, x[0].member);
    assert_eq!(1, x[1].member);
    assert_eq!(2, x[2].member);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.5MB
~37K SLoC