macro_rules! array { ($([$([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*) => { ... }; ($([$([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*) => { ... }; ($([$([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*) => { ... }; ($([$([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*]),+ $(,)*) => { ... }; ($([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*) => { ... }; ($([$($x:expr),* $(,)*]),+ $(,)*) => { ... }; ($($x:expr),* $(,)*) => { ... }; }
Expand description
Create an Array
with one, two, three, four, five, or six dimensions.
use ndarray::array;
let a1 = array![1, 2, 3, 4];
let a2 = array![[1, 2],
[3, 4]];
let a3 = array![[[1, 2], [3, 4]],
[[5, 6], [7, 8]]];
let a4 = array![[[[1, 2, 3, 4]]]];
let a5 = array![[[[[1, 2, 3, 4, 5]]]]];
let a6 = array![[[[[[1, 2, 3, 4, 5, 6]]]]]];
assert_eq!(a1.shape(), &[4]);
assert_eq!(a2.shape(), &[2, 2]);
assert_eq!(a3.shape(), &[2, 2, 2]);
assert_eq!(a4.shape(), &[1, 1, 1, 4]);
assert_eq!(a5.shape(), &[1, 1, 1, 1, 5]);
assert_eq!(a6.shape(), &[1, 1, 1, 1, 1, 6]);
This macro uses vec![]
, and has the same ownership semantics;
elements are moved into the resulting Array
.
Use array![...].into_shared()
to create an ArcArray
.
Attempts to crate 7D+ arrays with this macro will lead to
a compiler error, since the difference between a 7D array
of i32 and a 6D array of [i32; 3]
is ambiguous. Higher-dim
arrays can be created with ArrayD
.
ⓘ
use ndarray::array;
let a7 = array![[[[[[[1, 2, 3]]]]]]];
// error: Arrays of 7 dimensions or more (or ndarrays of Rust arrays) cannot be constructed with the array! macro.