
Redazione RHC : 19 November 2025 21:26
Two CPython developers have proposed adding the Rust programming language to the Python codebase . Emma Smith and a colleague have published a Preliminary Python Enhancement Proposal (Pre-PEP) justifying this step. Currently, Rust is only intended for writing optional extension modules, but it may become a required dependency in the future.
The main reason for adopting Rust is to improve memory safety . The language prevents entire classes of compile-time errors: out-of-bounds array accesses, accesses to freed memory, and data conflicts in multithreaded code.
This is especially relevant for Python, which uses free threads, where thread safety is paramount. The RustBelt project has even formally demonstrated the correctness of Rust’s safety guarantees for code free of unsafe constructs.
CPython regularly encounters bugs due to improper memory management. The authors of the proposal believe that Rust will radically reduce these problems. Even if some code remains unsafe due to interactions with the C API, the core logic of the modules will be written in Rust, a safe language.
Rust is already actively used by large C and C++ projects. The Linux kernel , Android , Firefox , and many other systems are adopting the language to improve reliability.
Google has already reported positive results from implementing Rust on Android. Interestingly, 25% to 33% of new Python extensions are written in Rust; integrating the language into CPython could further boost this trend.
In addition to security, Rust offers high-performance data structures from the standard library . Vectors, hash tables, and mutexes are all implemented with zero overhead and are well documented.
Rust’s macro system is superior to C macros : declarative macros are cleaner and don’t accidentally capture variables, while procedural macros allow for powerful code transformations . The PyO3 library makes extensive use of procedural macros to simplify using the Python API.
Developers have already created a prototype _base64 module that demonstrates the performance improvements achieved using Rust.
Integration will require a new cpython-sys crate with C FFI API definitions. Bindings are generated using bindgen , the official Rust tool also used in Linux and Android projects. Cargo can provide dependencies out of the box, so there’s no need to download packages during compilation.
Rust supports all platforms specified in PEP 11 and later . All Level 1 Python platforms correspond to Level 1 or Level 2 Rust platforms, with a full set of development tools. Cross-compilation is easy to set up: just set the desired target and specify the linker.
The authors abandoned the idea of using the existing PyO3 library within CPython. This would have created an additional layer of abstraction and slowed development: each new API would have been added to PyO3 first, then updated in CPython. With bindgen, new APIs are available automatically.
There’s a dependency problem: the Rust compiler uses Python for bootstrapping . But there are workarounds. You can compile an older version of Python, then Rust, then the new CPython . Rust’s bootstrapping scripts are even compatible with Python 2 , so the problem is solvable. Alternatives are to use PyPy or have Rust decouple from Python during the build process.
Training resources are available for current CPython developers. The Rust Book offers a comprehensive introduction to the language, as well as Rust for C++ Programmers and official training materials .
Plans are underway to create a Rust expert team and add a tutorial to the devguide . Working with function arguments may require adapting Argument Clinic or creating a procedural macro in Rust.
Redazione