# Construct ndarray::ArrayView from multiple dimensional array

**URL:** <https://users.rust-lang.org/t/construct-ndarray-arrayview-from-multiple-dimensional-array/83094>\
**Category:** help\
**Created:** [October 22, 2022, 4:28am UTC](https://users.rust-lang.org/t/construct-ndarray-arrayview-from-multiple-dimensional-array/83094 "2022-10-22T04:28:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wangjia184](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wangjia184/32/12386_2.png) [@wangjia184](https://users.rust-lang.org/u/wangjia184)\
**Post date:** [October 22, 2022, 4:28am UTC](https://users.rust-lang.org/t/construct-ndarray-arrayview-from-multiple-dimensional-array/83094/1 "2022-10-22T04:28:37Z")

</div>

```rust
pub type SquareMatrix<T = f32> = [[T; BOARD_SIZE]; BOARD_SIZE];
pub type StateTensor<T = f32> = [SquareMatrix<T>; CHANNELS];

pub trait StateTensorExtension {
    fn shape(self: &Self) -> [usize; 3];
}
impl<T> StateTensorExtension for StateTensor<T> {
    fn shape(self: &Self) -> [usize; 3] {
        return [CHANNELS, BOARD_SIZE, BOARD_SIZE];
    }
}

```

I am using a quite ugly style to construct an array view from the array

```rust
    let state_tensor: StateTensor<f32> = // ...;
    let dims = state_tensor.shape();
    let x: ArrayBase<ViewRepr<&f32>, _> = unsafe {
        ArrayView::from_shape_ptr(dims, bytemuck::cast_slice(&state_tensor).as_ptr())
    };

```

The above code assumes multiple dimensional arrays are tightly packed in Rust. Would that work always?  
What is the best way to construct ndarray::ArrayView from multiple dimensional array in my case? Or should I created a owned array instead?

---

<div class="post-metadata">

**Author:** ![RedDocMD](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/reddocmd/32/18512_2.png) [@RedDocMD](https://users.rust-lang.org/u/RedDocMD)\
**Post date:** [October 22, 2022, 4:37am UTC](https://users.rust-lang.org/t/construct-ndarray-arrayview-from-multiple-dimensional-array/83094/2 "2022-10-22T04:37:51Z")

</div>

> [@wangjia184](#):
>
> The above code assumes multiple dimensional arrays are tightly packed in Rust. Would that work always?

Yes. Look at the [array layout reference](https://doc.rust-lang.org/reference/type-layout.html#array-layout). It says that, essentially, arrays are always tightly packed and that they don't carry around extra meta-data. This means that multi-dimensional arrays are both tightly packed and stored in row-major order.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@H2CO3](https://users.rust-lang.org/u/H2CO3)\
**Post date:** [October 22, 2022, 10:56am UTC](https://users.rust-lang.org/t/construct-ndarray-arrayview-from-multiple-dimensional-array/83094/3 "2022-10-22T10:56:14Z")

</div>

**You shouldn't use `unsafe` for this!** Why don't you simply use the `from_shape()` method of `ArrayView`?

```rust
let x = ArrayView::<f32, _>::from_shape(
    dims.into_dimension(),
    bytemuck::cast_slice::<_, f32>(&state_tensor)
);

```

[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f8b649da47df7f3fe07ec68f012281b5)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [January 20, 2023, 10:57am UTC](https://users.rust-lang.org/t/construct-ndarray-arrayview-from-multiple-dimensional-array/83094/4 "2023-01-20T10:57:12Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
