Macro none_arr

Source
macro_rules! none_arr {
    ($t:ty; $n:expr) => { ... };
}
Expand description

Creates an array of type [Option<T>; N] and default-initializes it to None for every element.

§Macro Arguments

  • $t - The type to wrap into an Option<T>.
  • $n - The number of elements for the array.

§Example

When dealing with non-Copy/Clone types, arrays of type [Option<T>; N] cannot be created even though the default type None could be applied:

// This type does not implement Copy/Clone.
struct Complicated;

// Fails with "the trait `Copy` is not implemented for `Complicated`":
let arr: [Option<Complicated>; 10] = [None; 10];

This crate simplifies array creation of these cases through the none_arr macro:

let arr = none_arr![Complicated; 10];

// The result is an array of Option<Complicated>.
let arr: [Option<Complicated>; 10] = arr;

assert_eq!(arr.len(), 10);
for item in arr.into_iter() {
    assert!(item.is_none());
}