#array #box #multidimensional-array

boxarray

Safe way to allocate and initialize nested arrays directly on the heap in Rust

6 stable releases

1.3.0 Oct 16, 2023
1.2.1 Oct 9, 2023
1.2.0 Sep 30, 2023
1.1.0 Sep 25, 2023
1.0.1 Sep 24, 2023

#541 in Memory management

44 downloads per month

MIT license

10KB
127 lines

boxarray

GitHub Workflow Status Crates.io Docs.rs

Safe way to allocate and initialize nested arrays directly on the heap in Rust.

Usage

To use boxarray in your Rust project, simply add it as a dependency in your Cargo.toml:

[dependencies]
boxarray = "1.3.0"

Then import and use it in your project:

use boxarray::boxarray;
use boxarray::boxarray_;

fn main() {
  let v = 7.0;
  let a: Box<[[[f64; 3]; 2]; 4]> = boxarray(v);
  println!("{a:?}");

  let f = |((((), i), j), k)| (i+j*k) as usize;
  let a: Box<[[[usize; 3]; 2]; 4]> = boxarray_(f);
  println!("{a:?}");
}

lib.rs:

Safe way to allocate and initialize nested arrays directly on the heap inside a Box.

Usage

In order to initialize a Boxed nested-array, simply call the boxarray function and give it the value (here v) to initialize with:

  let v = 7.0;
  let a: Box<[[[f64; 3]; 2]; 4]> = boxarray::boxarray(v);

The initialization can also be done with a function that takes the coordinates in nested tuples as arguments by using boxarray_ instead:

  let f = |((((), i), j), k)| (i+j*k) as usize;
  let a: Box<[[[usize; 3]; 2]; 4]> = boxarray::boxarray_(f);

No runtime deps