rust-numpy
PyO3-based Rust bindings to the NumPy C-API for sharing array data between Rust and Python.
Repository Health
Technical Analysis
rust-numpy (published as the numpy crate) provides Rust bindings to the NumPy C-API on top of PyO3. It lets Rust code create, read, and mutate NumPy arrays directly, and bridges Rust’s ndarray types with Python’s NumPy arrays so numeric data can cross the language boundary without copying.
It is the standard way to write high-performance native Python extensions in Rust that operate on NumPy arrays: you accept PyArray inputs, work with them as ndarray views, and return results back to Python. The crate handles dtype mapping, borrow checking against Python’s aliasing rules, and safe conversions, and it tracks PyO3 and NumPy releases closely.
What You Get
PyArray/PyArrayDyntypes wrapping NumPy arrays with compile-time or runtime dimensionality- Zero-copy interop with the
ndarraycrate via readonly and read-write views - A borrow-checking layer that upholds NumPy’s aliasing rules from Rust
- dtype mapping between Rust element types and NumPy dtypes, including datetime and string support
- Helpers for element-wise conversion, sum/product reductions, and array construction
Common Use Cases
- Writing fast Python extension functions in Rust that take and return NumPy arrays
- Sharing large numeric buffers between Rust and Python without copying
- Porting performance-critical NumPy code paths to native Rust
- Building PyO3 modules that expose Rust numeric algorithms to Python users
Under The Hood
Architecture - The crate is organized under src/ around a typed array core (array.rs, untyped_array.rs, dtype.rs) with a borrow/ module implementing runtime borrow tracking, convert.rs for element conversions, and npyffi/ holding the raw FFI declarations against NumPy’s C-API. Public PyArray/PyReadonlyArray/PyReadwriteArray types sit on top of these and expose safe ndarray views.
Tech Stack - Pure Rust (MSRV 1.83, tracking PyO3), depending on PyO3 for Python bindings and the ndarray crate for the Rust-side matrix representation. Requires a Python environment with NumPy installed at runtime.
Code Quality - The repo carries a focused integration test suite (tests/array.rs, borrow.rs, sum_products.rs, to_py.rs, array_like.rs) plus codecov coverage and CI. Modules are small and single-purpose, and the unsafe FFI surface is isolated in npyffi behind safe wrappers.
API Design - The API mirrors NumPy and ndarray idioms so Rust users feel at home: readonly vs read-write view types make aliasing intent explicit, and docs.rs documentation plus examples keep the learning path reasonable despite the inherent complexity of cross-language numeric FFI.