# How to slice a 3-dimensional array

**URL:** <https://users.rust-lang.org/t/how-to-slice-a-3-dimensional-array/142994>\
**Category:** help\
**Created:** [October 13, 2026, 1:27am UTC](https://users.rust-lang.org/t/how-to-slice-a-3-dimensional-array/142994 "2026-10-13T01:27:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NA-ori](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/na-ori/32/54296_2.png) [@NA-ori](https://users.rust-lang.org/u/NA-ori)\
**Post date:** [October 13, 2026, 1:27am UTC](https://users.rust-lang.org/t/how-to-slice-a-3-dimensional-array/142994/1 "2026-10-13T01:27:13Z")

</div>

I have a three-dimensional array like this:

```rust
const XDIM = 60; const YDIM = 60; const ZDIM = 2;
let mut world = [[[0.0; ZDIM] ; YDIM]; XDIM];

```

How would I extract all the X and Y contents for a single Z-value, giving me something like a variable of the type

```rust
[[f64; YDIM] ; XDIM]

```

Would it be easier/possible to reorganize the array so the z-axis is on the outside and I can slice it like

```rust
let mut world = [[[0.0; YDIM] ; XDIM]; ZDIM];
let z = 0;
let slice = world[z];

```

Thanks! :3

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [October 13, 2026, 2:23am UTC](https://users.rust-lang.org/t/how-to-slice-a-3-dimensional-array/142994/2 "2026-10-13T02:23:23Z")

</div>

in computers, there's no real multi-dimension arrays, all data are placed in memory in a one-dimension address space. \[1\]

so when you slice a "muti-dimension" array, you only get contiguous range in certain special cases, i.e. all "inner" dimensions are fully covered.

a more general solution would be some scattered data structure, typically represented with a base address and some "stride" information for the dimensions, and the address for individual elements can be easily calculated in constant time.

> [@NA-ori](#):
>
> Would it be easier/possible to reorganize the array so the z-axis is on the outside and I can slice it like

yes.

if this kind of slicing operation is the common one for your use case, changing the data layout in this way is a good optimization, it will give you contiguous memory regions for the slices.

small suggestion for the code though, this line

```rust
let slice = world[z];

```

can be changed to

```rust
let slice = &world[z];

```

you code create a copy of the subarray, which might not be desirable when the data is large and you don't need mutability anyway. but I don't know your exact use case, so it's just a suggestion.

* * *

1. physically, the DRAM hardware actually does decode the address and activate the columns in an hierarchical manner, sort of multi-dimensional, but software are hidden from the physical layout of memory cells, for good reasons like compatibility and security etc, and that's how most computer architectures are designed, but IMO it is a waste of hardware potentials in some way
