ndarray for Rust: The Complete Guide to N-Dimensional Arrays
1. What Is ndarray and Why Use It?
The ndarray library is the go-to extension in the Rust ecosystem for working with multi-dimensional arrays (N-dimensional tensors). It provides powerful, type-safe, and high-performance data structures for numerical computing, linear algebra, signal processing, machine learning, and scientific calculations. If you're familiar with NumPy in Python, ndarray is its direct counterpart in Rust — but with the added benefits of strict typing, no garbage collector, and full memory control.
Why do you need it? Rust doesn't have built-in support for multi-dimensional arrays with an arbitrary number of axes. Standard vectors (Vec<T>) and slices (&[T]) only work with one-dimensional data. For tasks requiring matrix algebra, convolutions, tensor operations, or image processing, a specialized library is essential. ndarray solves these problems by providing:
- Typed N-dimensional arrays (
ArrayBase) with fixed or dynamic numbers of axes. - Efficient slicing, indexing, and iteration.
- Vectorized arithmetic operations (broadcasting).
- Integration with BLAS/LAPACK via
ndarray-linalgfor high-performance linear algebra. - Support for parallel computing through
rayon.
The library is actively used in projects like RustML, Linfa (a machine learning framework), Image (image processing), and many others. It's a cornerstone of scientific computing in Rust.
2. Installation
To install ndarray, use the Cargo package manager that comes with Rust. Add the dependency to your project's Cargo.toml file:
[dependencies]
ndarray = "0.16"
Then run the following command in your terminal:
cargo build
For linear algebra operations (matrix multiplication, eigenvalues), add ndarray-linalg and choose a BLAS backend (e.g., OpenBLAS or Intel MKL):
[dependencies]
ndarray = "0.16"
ndarray-linalg = { version = "0.16", features = ["openblas"] }
For parallel iteration, use ndarray-parallel:
[dependencies]
ndarray = "0.16"
ndarray-parallel = "0.16"
3. Quick Start — A Minimal Working Example
Let's create a one-dimensional array, perform element-wise addition, and print the result. Save the following code to src/main.rs:
use ndarray::Array;
fn main() {
// Create a 1D array with 5 elements
let a = Array::from_vec(vec![1, 2, 3, 4, 5]);
let b = Array::from_vec(vec![10, 20, 30, 40, 50]);
// Element-wise addition
let c = &a + &b;
println!("a = {:?}", a);
println!("b = {:?}", b);
println!("a + b = {:?}", c);
}
Run the program:
cargo run
Output:
a = [1, 2, 3, 4, 5], shape=[5], strides=[1], layout=CFcf (0x1)
b = [10, 20, 30, 40, 50], shape=[5], strides=[1], layout=CFcf (0x1)
a + b = [11, 22, 33, 44, 55], shape=[5], strides=[1], layout=CFcf (0x1)
4. Core Methods and Functions (8+)
4.1. Array::from_shape_vec
Signature: pub f
Also in library
C# (.NET) Application Configuration: Microsoft.Extensions.Configuration
Internationalization (i18n) in Java: A Complete Guide with Examples
Redis client for C# (.NET): StackExchange.Redis — connection and examples
Bogus for C# (.NET): Complete Guide with Examples