rust-numpy

PyO3-based Rust bindings to the NumPy C-API for sharing array data between Rust and Python.

Library
Cargo
v0.29.0
1,384stars
BSD-2-Clause

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
68/100Good
Development Activity48
Maintenance48
Community76
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
80/100Excellent
Architecture86
Code Quality88
Innovation82
Learning Curve65

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/PyArrayDyn types wrapping NumPy arrays with compile-time or runtime dimensionality
  • Zero-copy interop with the ndarray crate 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search