Combining Rust and Python: The Best of Both Worlds?

preview_player
Показать описание
In this video, I'll show you how to seamlessly integrate Rust with Python using Pyo3. This library allows you to write Python modules with Rust. This means that we get the speed and safety of Rust along with Python's easy-to-use features!

🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

Social channels:

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
- Kit Hygh
- Alexander Milden
- Bean

🔖 Chapters:
0:00 Intro
1:20 What is Pyo3?
3:56 What is Maturin?
5:14 Building and running the Pyo3
5:45 Writing our Rust code
8:06 Publishing our module
8:22 Bonus
10:03 Final thoughts

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

I used Rust pyo3 to expose some validation tool logic our team wrote in Rust (well, me) to our Python scripts. A useful tip is you can specify a stub pyi file at the top level (alongside your toml files) to provide better type hint and editor support when you import them. Maturin picks these up when it builds the package as long as it's called [module name].pyi

thomasroberts
Автор

Babe, wake up. Arjan uploaded a new tutorial

DJStompZone
Автор

This is mind boggling I'm enthralled. The publishing seems so incredibly easy wow

keanuclark
Автор

Side note: In a way, it's actually Poetry which is non-compliant here. The standard format for pyproject.toml nowadays is defined by PEP 621, which seems to be used by pyo3, while Poetry still clings to their own legacy format.

TheLolle
Автор

This is awesome! I’ve been waiting for a video like this. Thanks for the great overview 🎉

christensencode
Автор

Great video! A follow-up discussing/showing real life problems that can solved with combing Python and Rust together. Boosting performance, increasing safety etc. Personally I'm currently working a lot with async, websockets and JSON messages in python, any significant and potential performance rewards would be great to see.

barricuda
Автор

Really great video and easy to follow guide! When i used pyo3 i decided to make a proc macro the help make wrappers for struct impls so you could basically just add it for python support with little to no hassle. Can definitely say that there are ALOT of limitations on pyo3 that aren't directly visible, maybe a video on these things would be awesome and help some newer people to pyo3!

Retrokiller
Автор

Thanks so much! Very clear! Will definitely give it a try!!

solknar
Автор

Hey this is awesome. I actually picked up Rust recently and it’s been going great. Great timing to see this a month later when I’ve gotten a little more experienced. Great video!

strmwndz
Автор

Amazing video as usual! :)
What do you use to have auto-completion on your terminal? :D

maxidrones
Автор

this is very nicely explained! Question how do we add python typehints to rust functions?

trupalcanada
Автор

Is there some direct benefit of defining your python classes as Rust structs? I can understand that doing number crunching would be useful but not sure about the defining of objects (only). Would be interesting to hear your thoughts on what gives the most “bang for the buck” when combining the languages like this.

Thanks for interesting videos.

seyicpl
Автор

I'm just using the standard `difflib.SequenceMatcher` and for sentences and snippets of code it was just fast enough. But atm I'm trying to throw some 1000s lines of JSON against it and want to display it in some Qt text fields. It's already MUCH faster in Python 3.11 and 12 but might still be a couple seconds! I can imagine that this would be a breeze to do with a little Rust plugin!

ewerybody
Автор

Looks promising. But how about the interaction between Python and Rust regarding memory management? 🤔

Maybe show an example where you parse a numpy array and perform some transformation on it.

marcotroster
Автор

Nice video thanks, I used maturin but didn't know about rustimport. Python and Rust are my favorites language, I also think that they fit well together.

williamdroz
Автор

How do cross-platform builds work when you publish the project to pypi?

_DRMR_
Автор

Ive been slowly incorporating rust through polars which uses pyo3. Im pretty convinced at this point replacing pandas for polars is just better. The api handles the same, its faster and infers column types (this is huge). The fact that these apis just copy sql operations and facilitate working on dataframes make the switch to polars all the more obvious.

___nano
Автор

If rust is more efficient, I might put some bigger/slow projects into rust and see if I can trim the time down. Thanks for the info!

Iamsuperb
Автор

Have you looked into Julia Programming Language? What do you think about the multiple dispatch?

matheusbussinger
Автор

Wow, great tutorial, I always wanted to rewrite the core routines in rust for my project "pyTermTk" (a TUI Lib)
What about cross compilation?
Does Maturing take care of it?
One of the things that prevented me to use rust or c/c++ is that I don't know if I would be able to publish a package compatible with different architectures.

CeccoPierangioliEuge