Principle:Avhz RustQuant Python Integration
| Knowledge Sources | |
|---|---|
| Domains | Software_Engineering, Interoperability |
| Last Updated | 2026-02-07 21:00 GMT |
Overview
PyO3-based Python bindings exposing RustQuant's quantitative finance functionality to the Python ecosystem.
Description
Python Integration in RustQuant is achieved through PyO3, a Rust crate that provides seamless interoperability between Rust and Python. The bindings are organized as a Python package named RustQuant with hierarchical submodules that mirror the Rust crate structure.
The top-level #[pymodule] function registers three submodules:
- RustQuant.data -- Exposes the
Curve,CurveType, andInterpolationMethodclasses for working with financial curves (yield curves, discount curves, etc.). These types are annotated with#[pyclass]and#[pymethods]in the Rust source. - RustQuant.instruments -- A placeholder module for financial instrument classes (currently under development).
- RustQuant.time -- Exposes the
CalendarandMarketclasses for date handling, holiday calendars, and market conventions.
Each submodule is registered with Python's sys.modules dictionary, enabling standard Python import semantics such as from RustQuant.data import Curve. The bindings project uses maturin for building and packaging, as configured in a pyproject.toml.
Usage
Use the Python Integration principle when you need to access RustQuant's high-performance quantitative finance tools from Python code. This is particularly useful for data scientists and quantitative analysts who work primarily in Python but want the performance benefits of Rust for compute-intensive operations like curve fitting, option pricing, and calendar calculations.
Theoretical Basis
The integration follows the Foreign Function Interface (FFI) pattern, where Rust functions are compiled into a shared library (.so or .pyd) that Python can load as a native extension module. PyO3 handles the type conversion between Rust and Python types automatically, including:
- Rust
f64maps to Pythonfloat - Rust
Stringmaps to Pythonstr - Rust
Vec<T>maps to Pythonlist - Rust structs annotated with
#[pyclass]become Python classes
The module registration pattern uses nested PyModule objects and the sys.modules dictionary to enable dotted import paths:
#[pymodule]
fn RustQuant(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
register_data_module(py, m)?;
register_instruments_module(py, m)?;
register_time_module(py, m)?;
Ok(())
}