How to use Rust with Python, and Python with Rust (2024)

How to use Rust with Python, and Python with Rust (1)

bySerdar Yegulalp

Senior Writer

how-to

Jan 03, 20245 mins

Programming LanguagesPythonRust

You can merge Python's convenience with Rust's speed, thanks to libraries in both languages. Get started with the PyO3 project.

How to use Rust with Python, and Python with Rust (2)

Credit: ra2 studio/Shutterstock

Python and Rust occupy seemingly opposite ends of the language spectrum. Python, interpreted at runtime, offers developers a flexible and comfortable programming environment, but at the cost of raw speed. Rust provides the speed, plus guarantees of memory safety, but requires that you learn a new paradigm for handling memory operations.

In theory, these languages shouldn’t compete; they should cooperate. And in practice, they can. Rust can benefit from Python’s ease of use, and Python can benefit from Rust’s speed and safety.

If you want to use Rust with Python, or Python with Rust, you’ll need to have at least passing familiarity with both languages to get the best results. You’ll also need to decide which of the two is your primary language, as the options for each approach are significantly different.

Calling Rust from Python with PyO3

If Python is your primary language, integrating with Rust works in conceptually the same way as integrating Python with C. The default implementation of Python, written in C, uses extensions either written in C or using a C-compatible ABI. Extensions written in Rust that use the same ABI will also work, although that isn’t automatic—you have to use crates designed to provide bindings for Rust functions to the Python C API.

Creating Rust bindings in Python

The most widely recognized project for creating Rust bindings in Python is PyO3. It can be used to write Python modules in Rust, or to embed the Python runtime in a Rust binary.

PyO3 leverages another project, Maturin, which is a tool for authoring Rust crates with Python packaging and bindings. When installed in a Python virtual environment, Maturin can be used from the command line to initialize a new Rust project with Python bindings enabled. The developer uses directives in the Rust code to indicate what Rust functions to expose to Python, and how to expose the whole of the Rust project to Python as an importable module.

Mapping Rust and Python types

One of PyO3’s useful aspects is its mappings between Rust and Python types. Functions written in Rust can accept either native Python types or Rust types converted from Python types. For instance, a bytearray or bytes object in Python maps elegantly to a Vec<u8> in Rust, and a str in Python can be rendered as a Rust String.

More complex types, like a Python dictionary or an integer that is too big for a machine-native integer, also have Rust conversions, but some require optional components. For instance, if you want to use Python integers of arbitrary size, you’d install thenum-bigint optional feature in PyO3, and have those integers expressed in Rust asnum_bigint::BigInt ornum_bigint::BigUint types.

Converting from Python to Rust incurs a per-call cost, but it frees you from having to use Python types entirely in the Rust code. In the Cython world, this is akin to the conversions to C types: there’s a cost for each conversion, but they bring major speedups if your goal is numerical processing entirely in C.

Calling Python from Rust

If you’re primarily a Rust developer but want to use Python inside a Rust application, PyO3 also has mechanisms for calling Python from within Rust code.

Python code can be defined in-line in a Rust program, compiled to Python bytecode, and executed using Rust calls. Rust structures likeHashMaporBTreeMapcan be used to pass positional or keyword argument lists.You can even just evaluate single expressions if that’s all you need.

Rust programs can invoke the CPython interpreter and work with it, allowing you to create and manipulate Python objects in Rust and make library calls. Existing Python code files can also be loaded in and used, but it’s a potential security hazard, so don’t load untrusted code or use this option in a situation where you could potentially do so.

If you’re more familiar with Rust than Python, it’s a good idea to have at least passing familiarity with the Python C API and the various Python object types before diving in.

A previous project,the cpython crate, also allowed Python inside a Rust application, but it’s no longer actively maintained and its developers recommend using PyO3 instead.

Performance tip

An important caveat with PyO3 is to always minimize the number of times data is passed back and forth between the two languages. Each call from Python to Rust or vice versa incurs some overhead. If the overhead outweighs the work you’re doing in Rust, you won’t see any significant performance improvement.

As an example, if you’re looping over an object collection, send the object to Rust and perform the looping there. This is more efficient than looping on the Python side and calling the Rust code with each iteration of the loop.

This guideline also applies generally to integrations between Python and other code that uses the Python C ABI, such as Cython modules.

Related content

  • how-toException handling in Java: The basics Learn everything you need to throw, try, catch, and clean up after Java exceptions in your programs.By Jeff FriesenSep 12, 202421 minsJavaProgramming LanguagesSoftware Development
  • newsTypeScript 5.6 now generally available Update of Microsoft’s strongly typed JavaScript disallows nullish and truthy checks on syntax that never varies on nullishness or truthiness.By Paul KrillSep 11, 20243 minsJavaScriptTypescriptProgramming Languages
  • newsRust 1.81 stabilizes Error trait Stabilization of the Error trait in Rust core allows the usage of the trait in #![no_std] libraries. Sort algorithms also were improved in the latest Rust release. By Paul KrillSep 09, 20243 minsRustProgramming LanguagesSoftware Development
  • newsC language slumps in Tiobe popularity index Longtime leading programming language for systems development dropped to fourth in the Tiobe index for September, its lowest position ever. By Paul KrillSep 09, 20243 minsC LanguageProgramming LanguagesSoftware Development
  • Resources
  • Videos
How to use Rust with Python, and Python with Rust (2024)
Top Articles
CBC-MAC
Duel on Revan's flagship
Hotels Near 625 Smith Avenue Nashville Tn 37203
Edina Omni Portal
Metallica - Blackened Lyrics Meaning
Login Page
Wisconsin Women's Volleyball Team Leaked Pictures
Western Union Mexico Rate
The Potter Enterprise from Coudersport, Pennsylvania
877-668-5260 | 18776685260 - Robocaller Warning!
Free VIN Decoder Online | Decode any VIN
Delectable Birthday Dyes
Vanadium Conan Exiles
GAY (and stinky) DOGS [scat] by Entomb
All Obituaries | Ashley's J H Williams & Sons, Inc. | Selma AL funeral home and cremation
Housing Intranet Unt
12 Best Craigslist Apps for Android and iOS (2024)
Olivia Ponton On Pride, Her Collection With AE & Accidentally Coming Out On TikTok
Little Rock Arkansas Craigslist
Purple Crip Strain Leafly
ATV Blue Book - Values & Used Prices
Slag bij Plataeae tussen de Grieken en de Perzen
Methodist Laborworkx
Citymd West 146Th Urgent Care - Nyc Photos
Elbasha Ganash Corporation · 2521 31st Ave, Apt B21, Astoria, NY 11106
Pac Man Deviantart
Overton Funeral Home Waterloo Iowa
Keurig Refillable Pods Walmart
Joann Ally Employee Portal
Hobby Stores Near Me Now
Rimworld Prison Break
C&T Wok Menu - Morrisville, NC Restaurant
Hannah Palmer Listal
Move Relearner Infinite Fusion
Lexus Credit Card Login
European Wax Center Toms River Reviews
Gs Dental Associates
Meta Carevr
Publix Near 12401 International Drive
Buhl Park Summer Concert Series 2023 Schedule
Ezstub Cross Country
Ff14 Laws Order
Cross-Border Share Swaps Made Easier Through Amendments to India’s Foreign Exchange Regulations - Transatlantic Law International
Flashscore.com Live Football Scores Livescore
Ucsc Sip 2023 College Confidential
Honkai Star Rail Aha Stuffed Toy
Perc H965I With Rear Load Bracket
Petfinder Quiz
Crigslist Tucson
How to Do a Photoshoot in BitLife - Playbite
Kenmore Coldspot Model 106 Light Bulb Replacement
Black Adam Showtimes Near Cinemark Texarkana 14
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5822

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.