Join our community of software engineering leaders and aspirational developers. Always
stay in-the-know by getting the most important news and exclusive content delivered
fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter
in the past. Click the button below to open the re-subscribe form
in a new tab. When you're done, simply close that tab and continue
with this form to complete your subscription.
The New Stack does not sell your information or share it with
unaffiliated third parties. By continuing, you agree to our
Terms of Use and
Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!
We’re so glad you’re here. You can expect all the best TNS content to arrive
Monday through Friday to keep you on top of the news and at the top of your game.
What’s next?
Check your inbox for a confirmation email where you can adjust your preferences
and even join additional groups.
Follow TNS on your favorite social media networks.
Python powers most AI and machine learning (ML). With its rich ecosystem — from TensorFlow and PyTorch to scikit-learn and Hugging Face Transformers — Python has become the go-to language for researchers, data scientists and engineers. But Python has a well-known limitation: speed. Its global interpreter lock (GIL) restricts concurrency, while its interpreted nature makes it orders of magnitude slower than compiled languages like C++ or Rust.
On the other side of the spectrum is Rust: a systems programming language that delivers C++-level performance, memory safety without garbage collection and modern developer ergonomics. Rust is designed to handle high-performance, concurrent workloads — exactly the kind of workloads AI applications commonly demand in production.
So, why not use the best of both worlds?
Prototype and train models in Python, leveraging its mature ML ecosystem.
Push performance-critical components (data processing, inference kernels, parallel workloads) to Rust and call them seamlessly from Python.
This hybrid approach isn’t just theoretical, it already powers some of the most popular AI libraries today:
Hugging Face Tokenizers are written in Rust for blazing speed with Python bindings for usability.
Polars, a Rust-powered DataFrame library, routinely outperforms pandas while keeping a familiar Python interface.
How to integrate Rust into Python with tools like PyO3 and Maturin.
Practical examples of writing Rust functions, exposing them as Python modules and using them in AI workflows.
Real-world case studies signalling the future of hybrid AI development.
By the end, you’ll see how Rust can help overcome Python’s performance bottlenecks — without giving up the flexibility and ecosystem that make Python indispensable.
Python has earned its dominance in AI and ML because of its simplicity and vast ecosystem. From NumPy to PyTorch and scikit-learn, most cutting-edge models and research code start in Python. But as projects transition from research to production, Python’s weaknesses start to show.
This is where Rust shines. Let’s break down the complementarity. First and foremost: why Rust complements Python in AI/ML.
1. Performance at Scale
Python is interpreted, and even with tools like NumPy or Cython, it struggles with raw computational throughput.
Rust compiles to native machine code and offers C++-level performance with modern tooling.
Heavy numerical kernels, matrix operations or custom ML layers can be implemented in Rust and called from Python, delivering massive speedups without rewriting the entire pipeline.
Example: Hugging Face’s `tokenizers` library achieved significantly greater performance improvements than its pure Python counterpart by rewriting the core in Rust.
2. Concurrency Without the Global Interpreter Lock
Python’s GIL prevents true, multithreaded execution of Python bytecode.
This is a bottleneck when processing large datasets or running parallel inference workloads.
Rust has fearless concurrency: Its ownership and borrowing system ensures memory safety across threads, enabling efficient multithreaded data loaders, parallel preprocessing or distributed workloads — things Python alone struggles with.
3. Memory Safety Without Garbage Collection
C++ is traditionally used for speed, but it comes with risks, like segmentation faults and memory leaks.
Rust guarantees memory safety at compile time with zero-cost abstractions — no runtime overhead, no dangling pointers, no null dereferences.
For AI systems running 24/7 in production (think cloud inference services or edge devices), this reliability is critical.
4. Ecosystem Synergy
Python has mature AI/ML libraries, but Rust’s ecosystem is growing in complementary areas, including:
Polars (DataFrames) for high-performance data processing.
Burn (deep learning framework in Rust).
tch-rs (bindings to LibTorch for training and inference).
Many Rust libraries provide Python bindings out of the box, letting developers integrate them without leaving Python’s comfort zone.
5. Production-Grade AI Services
Training is usually done in Python. However, serving models at scale demands speed, stability and efficiency.
Rust is increasingly used to build inference servers and APIs (via Axum, Actix-web or gRPC).
This allows teams to keep training pipelines in Python while deploying Rust-backed services that are lean, safe and fast.
How To Integrate Rust Into Python With PyO3 and Maturin
There are several ways to connect Rust and Python (FFI, cffi, ctypes, etc.), but the most developer-friendly approach today is using:
Import it with `import my_rust_module` just like any normal Python package.
Step 1: Install Dependencies
Make sure you have:
Rust (latest stable):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Python (≥3.8 recommended).
Maturin (install via pip):
pip install maturin
Step 2: Create a New Rust Project
Make a new Rust library project:
cargo new --lib rust_python_demo
cd rust_python_demo
Next, update `Cargo.toml` to include PyO3:
[package]
name = "rust_python_demo"
version = "0.1.0"
edition = "2021"
[lib]
name = "rust_python_demo"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.22", features = ["extension-module"] }
Step 3: Write Rust Code (With Python Bindings)
Open src/lib.rs and replace its contents:
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
/// A simple function to add two numbers.
#[pyfunction]
fn add_numbers(a: i32, b: i32) -> i32 {
a + b
}
/// A function that computes dot product of two vectors.
#[pyfunction]
fn dot_product(vec1: Vec<f64>, vec2: Vec<f64>) -> PyResult<f64> {
if vec1.len() != vec2.len() {
return Err(pyo3::exceptions::PyValueError::new_err(
"Vectors must be of the same length",
));
}
Ok(vec1.iter().zip(vec2.iter()).map(|(x, y)| x * y).sum())
}
/// Define the Python module
#[pymodule]
fn rust_python_demo(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(add_numbers, m)?)?;
m.add_function(wrap_pyfunction!(dot_product, m)?)?;
Ok(())
}
Step 4: Build the Python Package
Run Maturin in develop mode (so you can import locally):
maturin develop
This compiles the Rust code into a Python module (rust_python_demo) and installs it into your current Python environment.
It works just like any other Python module, but the core logic is running at Rust speed.
Practical Example: Rust Functions in Python AI Workflows
Fast Data Preprocessing with Rust
Data preprocessing is often a bottleneck in ML pipelines. To normalize a dataset (scale values between 0 and 1) in Python, this would be written with loops or NumPy. Here’s how to implement it in Rust and call it from Python.
Rust (src/lib.rs):
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
/// Normalize a list of floats between 0 and 1
#[pyfunction]
fn normalize(data: Vec<f64>) -> PyResult<Vec<f64>> {
if data.is_empty() {
return Ok(vec![]);
}
let min = data.iter().cloned().fold(f64::INFINITY, f64::min);
let max = data.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
if (max - min).abs() < f64::EPSILON {
return Ok(vec![0.0; data.len()]); // all values the same
}
Ok(data.iter().map(|x| (x - min) / (max - min)).collect())
}
#[pymodule]
fn rust_python_demo(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(normalize, m)?)?;
Ok(())
}
Python:
import rust_python_demo
import numpy as np
data = np.random.rand(1_000_000).tolist()
normalized = rust_python_demo.normalize(data)
print(f"First 5 normalized values: {normalized[:5]}")
With large datasets, the Rust version is significantly faster than pure Python loops.
Real-World Use Case Studies
This hybrid approach is already proven in production:
Hugging Face Tokenizers.
Originally in Python, too slow for large-scale natural language processing (NLP) preprocessing.
Rewritten in Rust with Python bindings.
Achieved significant speedups.
Polars DataFrame
Rust core + Python bindings.
Outperforms pandas in many data manipulation tasks.
Researchers implement custom tensor operations in C++ for performance.
Rust bindings (`tch-rs`) are opening new doors for safer, modern low-level ops.
The Future of Hybrid AI Development
We’re seeing a clear trend:
Python remains the interface language for research, prototyping and orchestration.
Rust is emerging as the performance layer in AI systems for data handling, inference and deployment.
New Rust-native ML frameworks like Burn and Linfa show that Rust might eventually compete head-to-head with Python libraries.
In the near future, expect:
More Rust-backed Python libraries (following the Hugging Face / Polars model).
Increased use of Rust for production inference servers, while training stays in Python.
AI edge devices and WebAssembly deployments relying heavily on Rust’s portability and efficiency.
The bottom line: AI thrives on Python’s flexibility and vast ecosystem of libraries. But as we’ve seen, Python alone struggles with performance bottlenecks, concurrency limitations and the demands of production-grade systems. This is where Rust becomes the perfect companion.
By integrating Rust into Python workflows:
You gain near-C++ performance while keeping the expressiveness and ecosystem of Python.
You overcome the GIL with Rust’s fearless concurrency.
You deploy safer, more reliable AI services that can run at scale without memory leaks or runtime crashes.
These practical examples — from data normalization to dot product benchmarking — show how easy it is to expose Rust functions as Python modules using PyO3 and maturin. These aren’t just academic exercises; they mirror real-world use cases already adopted by industry leaders. Hugging Face, Polars and others are proving that the hybrid Rust + Python model works in the real world.
Looking ahead, we’re likely to see:
More Rust-backed Python libraries that keep Python at the forefront of research but quietly replace slow Python cores with blazing-fast Rust implementations.
Growing adoption of Rust in production inference services, particularly for edge devices and real-time AI.
A gradual rise of Rust-native ML frameworks that may one day rival TensorFlow and PyTorch.
The future of AI development is not Python or Rust. It’s Python and Rust together, a partnership that combines the best of both worlds: Python’s ease of use with Rust’s uncompromising performance and safety.
For developers and teams, the message is clear: You don’t need to abandon Python to build high-performance AI. Instead, embrace Rust where it matters most — in the performance-critical, parallel and safety-sensitive layers of your stack.
Andela provides the world’s largest private marketplace for global remote tech talent driven by an AI-powered platform to manage the complete contract hiring lifecycle. Andela helps companies scale teams & deliver projects faster via specialized areas: App Engineering, AI, Cloud, Data & Analytics.