
在易用性与原始性能之间搭建桥梁的指南。
大多数情况下,Python 已经足够快——尤其是当你依赖 NumPy、Polars 或其他用 C 等编译语言编写的调优库时。但偶尔,你会遇到一个无法向量化的热点循环:也许你在遍历字符串列表进行清理,或者在解析每个字符都很重要的混乱文本。你分析了性能,确认了瓶颈所在,然后盯着一个吃掉一半运行时间的 for 循环。这就是 Rust 大显身手的时刻。
Rust 提供了可预测的性能、对内存的严格控制和无畏并发——而且不需要手动管理内存。如果你想——“又一门要学的语言!”——好消息是你不需要放弃 Python 来使用 Rust。你可以保留你的协调逻辑、笔记本、测试——只将微小的、无聊的内循环移到 Rust 中,将 Rust 的学习曲线降至最低。
本文将演示如何从 Python 调用 Rust,并比较纯 Python 与 Python/Rust 组合的性能差异。
你可能会想:如果我会 Rust,为什么还要与 Python 集成——直接用 Rust 编程不就好了?
原因有三:
1. Rust 并不适合所有场景对于 ML、AI、脚本和 Web 后端等许多系统,Python 已经是首选语言。
2. 大多数代码不是性能关键的对于那些性能关键的部分,你通常只需要 Rust 的一个非常小的子集就能产生实质性影响,所以一点点 Rust 知识可以大有作为。
3. Python 的生态系统难以替代即使你很了解 Rust,Python 也能让你立即使用以下工具:
Rust 可能更快,但 Python 在生态系统广度和开发便利性上往往更胜一筹。
对于我们的用例,需要两样东西:Rust 和一个名为 maturin 的工具。
大多数人了解 Rust——一种近年来崛起的快速编译语言。但你可能没听说过 maturin。
Maturin 是一个用于构建和打包用 Rust 编写的 Python 扩展(使用 PyO3 或 rust-cpython)的工具,主要功能包括:
| 构建 Python 模块 | .pyd,Linux 上的 .so,macOS 上的 .dylib) |
| 打包 wheel 分发包 | |
| 发布到 PyPI | |
| 集成到 Python 打包 | pyproject.toml,支持 PEP 517,使 pip install 即开即用 |
$ uv init pyrust
$ cd pyrust
$ uv venv pyrust
$ source pyrust/bin/activate
(pyrust) $
(pyrust) $ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
选择默认安装选项后,验证安装:
(pyrust) $ rustc --version
rustc 1.89.0 (29483883e 2025-08-04)
创建一个新子文件夹并添加以下三个文件:
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.25", features = ["extension-module"] }
[build-system]
requires = ["maturin>=1.5,<2"]
build-backend = "maturin"
[project]
name = "hello_rust"
version = "0.1.0"
requires-python = ">=3.9"
use pyo3::prelude::*;
/// 将暴露给 Python 的简单函数
#[pyfunction]
fngreet(name: &str) -> PyResult<String> {
Ok(format!("Hello, {} from Rust!", name))
}
/// 模块定义
#[pymodule]
fnhello_rust(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(greet, m)?)?;
Ok(())
}
运行结果:
(pyrust) $ python -c "import hello_rust as hr; print(hr.greet('world'))"
# 输出
Hello, world from Rust!
说明:我们把 Rust 代码放在
src/lib.rs中是遵循 Rust 惯例,库代码放在此处,而不是src/main.rs(保留给独立的 Rust 可执行代码)。Maturin + PyO3 会在src/lib.rs中查找#[pymodule]函数来注册供 Python 调用的 Rust 函数。
考虑一个刻意平凡但具有代表性的场景:你有一个句子列表,需要对它们进行规范化处理——转换为小写、去除标点符号、分词。这很难高效向量化,因为逻辑在每个字符上都有分支。
defprocess_one_py(text: str) -> list[str]:
word = []
out = []
for c in text:
if c.isalnum():
word.append(c.lower())
else:
if word:
out.append("".join(word))
word = []
if word:
out.append("".join(word))
return out
defbatch_process_py(texts: list[str]) -> list[list[str]]:
return [process_one_py(t) for t in texts]
示例:
batch_process_py(["Hello, World! 123", "This is a test"])
# 输出:[['hello', 'world', '123'], ['this', 'is', 'a', 'test']]
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
/// 处理单个字符串:转小写 + 删除标点 + 按空白分割
fnprocess_one(text: &str) ->Vec<String> {
letmut out = Vec::new();
letmut word = String::new();
forcin text.chars() {
if c.is_alphanumeric() {
word.push(c.to_ascii_lowercase());
} elseif c.is_whitespace() {
if !word.is_empty() {
out.push(std::mem::take(&mut word));
}
}
// 完全忽略标点
}
if !word.is_empty() {
out.push(word);
}
out
}
#[pyfunction]
fnbatch_process(texts: Vec<String>) -> PyResult<Vec<Vec<String>>> {
Ok(texts.iter().map(|t| process_one(t)).collect())
}
#[pymodule]
fnrust_text(_py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(batch_process, m)?)?;
Ok(())
}
(pyrust) $ maturin develop --release
(pyrust) $ python benchmark.py
--- Benchmark ---
Python median: 5.159 s | throughput: 96,919 texts/s
Rust 1-thread median: 3.024 s | throughput: 165,343 texts/s
在处理 500,000 个文本时,Rust 版本比纯 Python 快约 1.7 倍!而且不需要太多额外工作。
Rust 有一个名为 Rayon 的并行化库,可以轻松地在多个 CPU 核心间分散代码:
iter() 替换为并行迭代器 par_iter()只需对 Rust 代码做三处微小更改:
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
// 变更 1:添加这行
use rayon::prelude::*;
fnprocess_one(text: &str) ->Vec<String> {
letmut out = Vec::new();
letmut word = String::new();
forcin text.chars() {
if c.is_alphanumeric() {
word.push(c.to_ascii_lowercase());
} elseif c.is_whitespace() {
if !word.is_empty() {
out.push(std::mem::take(&mut word));
}
}
}
if !word.is_empty() {
out.push(word);
}
out
}
#[pyfunction]
fnbatch_process(texts: Vec<String>) -> PyResult<Vec<Vec<String>>> {
Ok(texts.iter().map(|t| process_one(t)).collect())
}
// 变更 2:添加并行版本函数
#[pyfunction]
fnbatch_process_parallel(texts: Vec<String>) -> PyResult<Vec<Vec<String>>> {
Ok(texts.par_iter().map(|t| process_one(t)).collect())
}
#[pymodule]
fnrust_text(_py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(batch_process, m)?)?;
// 变更 3:注册并行函数
m.add_function(wrap_pyfunction!(batch_process_parallel, m)?)?;
Ok(())
}
--- Benchmark ---
Python median: 5.171 s | throughput: 96,694 texts/s
Rust 1-thread median: 3.091 s | throughput: 161,755 texts/s
Rust Rayon median: 2.223 s | throughput: 224,914 texts/s
| +133% |
并行化的 Rust 代码比非并行化的 Rust 时间缩短了约 27%,比纯 Python 代码快两倍以上!
Python 对于大多数任务来说通常足够快。但如果性能分析显示有一个无法向量化的慢点,你不必放弃 Python 或重写整个项目。相反,只需将性能关键部分移到 Rust,其余代码保持不变。
使用 PyO3 和 maturin,你可以将 Rust 代码编译成与现有库无缝协作的 Python 模块,同时保留大部分 Python 代码、测试、打包和工作流,在最需要的地方获得 Rust 的速度、内存安全和并发优势。
这里简单的示例和基准测试表明,只需将代码的一小部分用 Rust 重写,就能使 Python 快得多。添加 Rayon 进行并行化可以进一步提升性能,而代码变化极少,也不需要复杂的工具。这是一种在不将整个项目切换到 Rust 的情况下加速 Python 工作负载的实用且简单的方法。
往期推荐
高效长上下文 RAG 的 5 种技术
如何使用 Gemma 4 和 Python 实现工具调用
从4周到45分钟:为4700多份PDF设计文档提取系统
掌握智能体 AI 系统中记忆的 7 个步骤
原文标题:How to Call Rust from Python
作者:Thomas Reid
发布时间:2026年4月21日
来源:Towards Data Science