Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rust-numpy

> 编程语言
Open source

PyO3-based Rust bindings of the NumPy C-API

1.4K stars0 likes0 views
WebsiteGitHub

About

PyO3-based Rust bindings of the NumPy C-API

rust-numpy

Rust bindings for the NumPy C-API.

API documentation

  • Latest release
  • Current main

Requirements

  • Rust >= 1.83.0
    • Basically, our MSRV follows the one of PyO3
  • Python >= 3.9
    • Python 3.7 support was dropped from 0.29
    • Python 3.8 support was dropped from 0.30
  • Some Rust libraries
    • ndarray for Rust-side matrix library
    • PyO3 for Python bindings
    • And more (see Cargo.toml)
  • numpy installed in your Python environments (e.g., via pip install numpy)
    • We recommend numpy >= 1.16.0, though older versions may work

Example

Write a Python module in Rust

Please see the simple example for how to get started.

There are also examples using ndarray-linalg and rayon.

toml
[lib]
name = "rust_ext"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.29" }
numpy = "0.29"
…

Execute a Python program from Rust and get results

toml
[package]
name = "numpy-test"

[dependencies]
pyo3 = { version = "0.29", features = ["auto-initialize"] }
numpy = "0.29"
rust
use numpy::{PyArray1, PyArrayMethods};
use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python};

fn main() -> PyResult<()> {
    Python::attach(|py| {
        let np = py.import("numpy")?;
        let locals = [("np", np)].into_py_dict(py)?;

        let pyarray = py
            .eval(c"np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&locals), None)?
            .cast_into::<PyArray1<i32>>()?;

        let readonly = pyarray.readonly();
        let slice = readonly.as_slice()?;
        assert_eq!(slice, &[1, 2, 3]);

        Ok(())
    })
}

Dependency on ndarray

This crate uses types from ndarray in its public API. ndarray is re-exported in the crate root so that you do not need to specify it as a direct dependency.

Furthermore, this crate is compatible with multiple versions of ndarray and therefore depends on a range of semver-incompatible versions, currently >= 0.15, <= 0.17. Cargo does not automatically choose a single version of ndarray by itself if you depend directly or indirectly on anything but that exact range. It can therefore be necessary to manually unify these dependencies.

For example, if you specify the following dependencies

toml
numpy = "0.29"
ndarray = "0.15"

this will currently depend on both version 0.15.6 and 0.16.1 of ndarray by default even though 0.15.6 is within the range >= 0.15, <= 0.17. To fix this, you can run

bash
cargo update --package ndarray:0.16.1 --precise 0.15.6

to achieve a single dependency on version 0.15.6 of ndarray.

Contributing

We welcome issues and pull requests.

PyO3's Contributing.md is a nice guide for starting.

Also, we have a Gitter channel for communicating.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustndarraynumpynumpy-capirust-bindings

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言