Combining Rust and Python: The Best of Both Worlds?

  Рет қаралды 72,372

ArjanCodes

ArjanCodes

Күн бұрын

Пікірлер: 83
@ArjanCodes
@ArjanCodes 7 ай бұрын
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
@thomasroberts6008
@thomasroberts6008 7 ай бұрын
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
@InverseAgonist
@InverseAgonist 7 ай бұрын
I was going to ask about stub files, glad to know this is supported!
@aflous
@aflous 7 ай бұрын
Nice! Gonna push my team to write some Rust around our python projects I guess
@DJStompZone
@DJStompZone 7 ай бұрын
Babe, wake up. Arjan uploaded a new tutorial
@thatmg
@thatmg 7 ай бұрын
Fair enough
@TheLolle97
@TheLolle97 7 ай бұрын
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.
@pawelkubik
@pawelkubik 7 ай бұрын
poproject.yaml is the new generic format, so Arjan could've simply disabled Poetry plugin and look for a generic one for pyproject files.
@andrewvee880
@andrewvee880 5 ай бұрын
That's right! It always bothered me with poetry. According to Maturin project page they comply with PEP 621. But TBF Arjan actually has incorrectly specified dependencies, they should be an array of strings. Maybe Maturin doesn't care and still reads it correctly but that's why Python VS Code extension is showing an error
@keanuclark4833
@keanuclark4833 7 ай бұрын
This is mind boggling I'm enthralled. The publishing seems so incredibly easy wow
@barricuda5
@barricuda5 7 ай бұрын
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.
@christensencode7538
@christensencode7538 7 ай бұрын
This is awesome! I’ve been waiting for a video like this. Thanks for the great overview 🎉
@ArjanCodes
@ArjanCodes 7 ай бұрын
Im glad you liked the content!
@st0rmw1ndz
@st0rmw1ndz 7 ай бұрын
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!
@ArjanCodes
@ArjanCodes 7 ай бұрын
Glad you enjoyed the video!
@Retrokiller543
@Retrokiller543 7 ай бұрын
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!
@solknar7819
@solknar7819 7 ай бұрын
Thanks so much! Very clear! Will definitely give it a try!!
@ArjanCodes
@ArjanCodes 7 ай бұрын
Glad the video helped you!
@KristianGrönberg
@KristianGrönberg 7 ай бұрын
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.
@marcotroster8247
@marcotroster8247 7 ай бұрын
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.
@horoshuhin
@horoshuhin 7 ай бұрын
Hey Arjan, I'm curious if you see yourself writing rust as a day-to-day? what areas would you apply it to? love the rise of rust tutorials on your channel :)
@LieberLois
@LieberLois 7 ай бұрын
I feel the same way. With Go or similar languages it's easy to find use cases, but Rust ... not so much
@theninjascientist689
@theninjascientist689 6 ай бұрын
@@LieberLois I just used Rust for an embedded project using an E-Ink display on an ESP32, but I've also used Bevy to make games, Leptos for websites, and Clap to create command-line tools.
@Iamsuperb4
@Iamsuperb4 7 ай бұрын
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!
@ArjanCodes
@ArjanCodes 7 ай бұрын
You’re welcome!
@trupalcanada
@trupalcanada 7 ай бұрын
this is very nicely explained! Question how do we add python typehints to rust functions?
@extraordinaryhuman1806
@extraordinaryhuman1806 7 ай бұрын
This is GREAT! That's why I love Python. It's so easy to integrate with other stuff to overcome limitations of Python. Thank you for this video
@ArjanCodes
@ArjanCodes 6 ай бұрын
Glad you enjoyed t!
@___nano
@___nano 7 ай бұрын
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.
@matheusbussinger5092
@matheusbussinger5092 7 ай бұрын
Have you looked into Julia Programming Language? What do you think about the multiple dispatch?
@somikmukherjee6058
@somikmukherjee6058 6 ай бұрын
Can you make one for Go and Python too?
@ewerybody
@ewerybody 7 ай бұрын
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!
@_DRMR_
@_DRMR_ 7 ай бұрын
How do cross-platform builds work when you publish the project to pypi?
@williamdroz6890
@williamdroz6890 7 ай бұрын
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.
@NickDanger3
@NickDanger3 7 ай бұрын
This adds a significant amount of complication to a python project. What I did not see is any measurement at all of how much speed up inserting less modules could achieve! How do I know if I should bother with all this, if I have no idea how much speed up it might get me?
@maxidrones
@maxidrones 7 ай бұрын
Amazing video as usual! :) What do you use to have auto-completion on your terminal? :D
@brennancurrier806
@brennancurrier806 19 күн бұрын
Hmm, I been wanting to make a bulk texture converter and possibly downscaler settings too and this might be useful to expose rusts image and image_dds crates to python to do the conversion in rust itself and all the front end code in PyQt. Maybe create the entire backend code in rust and just front end gui be python.
@EngineerNick
@EngineerNick 7 ай бұрын
Yaaay! :) Rustimport is pretty interesting for cross platform support... publishing compiled binaries for many platforms is a bit hectic
@frankcasanova2132
@frankcasanova2132 7 ай бұрын
what about Cython? i think is less complex and better in performance terms
@cyberdude33
@cyberdude33 7 ай бұрын
@ArjanCodes does the original module written in Rust can still be used as a rust library as well or now after importing py03 it can only be used by Python? Also does it give the same performance benefit of rust when we ran it as a module from Python?
@rafiullah-zz1lf
@rafiullah-zz1lf 7 ай бұрын
Love it .. going back to c after learning python from you.. today i learned how to cheat c compiler. I was coding for luhn check for three days. Today i made a dumb change in the code by suppying an argument which i assigned to something but not used. The compiler wasnt happy and error parameter not in use. So what i did was i just make a variable int error=0; and then error += s; The error was gone programe compiked 🎉🎉🎉🎉🎉. Tomorroelw i will work on how to get rid of the error variable 😂😂😂😂3
@ifeanyinneji7704
@ifeanyinneji7704 7 ай бұрын
Please do more rust tutorials 🙏
@CeccoPierangioliEuge
@CeccoPierangioliEuge 7 ай бұрын
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.
@theninjascientist689
@theninjascientist689 6 ай бұрын
I can't speak for C/C++ but in Rust you publish your package as a "crate" which can then be freely used by other devs. It will be compiled for their architecture when they download and use it (usually with Cargo).
@mith1979
@mith1979 7 ай бұрын
Are you going to create a rust programming course?
@matthiasschuster9505
@matthiasschuster9505 7 ай бұрын
Julia combines both in one language
@Special1122
@Special1122 7 ай бұрын
what is the advantage over c++ modules
@TheDigitalSight
@TheDigitalSight 7 ай бұрын
Is it possible to run python code from rust as well?
@franktewierikholscher
@franktewierikholscher 7 ай бұрын
It looks great! But as an amateur python programmer I don't think I am going to use it. I don't know anything about rust so I have to learn a new language from scratch. When I have done that, I have to understand when it is usefull to to use rust. So I stick by python, if you don't mind....🙂😇
@eliotdexter
@eliotdexter 7 ай бұрын
I'd be curious to see how Cython compares to this
@hubstrangers3450
@hubstrangers3450 7 ай бұрын
Thank you....
@liammcintyre9180
@liammcintyre9180 7 ай бұрын
I will use this for sure. Do you need to think about accidentally spawning new processes?
@bonquaviusdingle5720
@bonquaviusdingle5720 7 ай бұрын
mojo is an existing superset of python which is already 50% faster than rust
@Singlton
@Singlton 7 ай бұрын
does this give python the performance of rust?
@EngineerNick
@EngineerNick 7 ай бұрын
Yes, but with certain caveats. Moving objects back and fourth into python can be "slow"-ish. The more/bigger chunks of work you can shift over the boundary into rust, the bigger the benefits :)
@ewerybody
@ewerybody 7 ай бұрын
no
@default_youtube_profile
@default_youtube_profile 7 ай бұрын
this would create a dependency on an extra toolkit i.e. rust
@demolazer
@demolazer 7 ай бұрын
Time to have chat gpt write me rust modules and scripts to import into my projects, perfect
@EvenTheDogAgrees
@EvenTheDogAgrees 7 ай бұрын
Whatever they do, name the result "Rython". Silly as that name may sound, it sure as hell beats programming in "Pust".
@franktewierikholscher
@franktewierikholscher 7 ай бұрын
I would call it: Ruthon a litlle more credit for Rust 🤣😂😁
@nbansal
@nbansal 7 ай бұрын
Nah, I'll wait for Mojo.
@alexandarjelenic2880
@alexandarjelenic2880 7 ай бұрын
I haven’t tried rust yet. But I might give it a go. But,I haven’t tried go yet but I will wait and see. I haven’t tried c yet.
@NostraDavid2
@NostraDavid2 6 ай бұрын
If C++ can be pronounced as Sepples C++-ians can also be called Sepplesians
@Doggy_Styles_Coding
@Doggy_Styles_Coding 7 ай бұрын
I was thinking how you can reverse the rusty python pun. Like pythony rust or pythoned rust but it won`t make any sense
@superuser8636
@superuser8636 7 ай бұрын
I’d rather just call Rust libraries (like polars) from Python 😂😂
@dartneer
@dartneer 7 ай бұрын
Nice but not convinced. It you really want Python + Performance - you are better off giving Mojo language a go..
@TechExplorer123
@TechExplorer123 7 ай бұрын
I agree that in general this would be the better approach, problem is mojo is missing a lot of features and is atm just a proof of concept, but not a full language.
@nad9969
@nad9969 7 ай бұрын
Also you should stop thinking in just performance, rust offers memory safety.
@dartneer
@dartneer 7 ай бұрын
Fancy words like memory safety, superman performance blah blah are delusional. The lucky enough people who work at netflix level software - you know, the 0.0 something percent - need to worry about memory safety and are normally not watching introduction videos on KZbin. The rest of us - mental model shift is a necessary consideration for our day to day jobs.
@nad9969
@nad9969 6 ай бұрын
@@squishy-tomatoIf you write something in rust vs python, you need less resources, if you are in a cloud, could save you some money and the less power we use, the less carbon we put into the air. ;-)
@judahwilson6756
@judahwilson6756 6 ай бұрын
Make armorers great again
@KirubelTamene
@KirubelTamene 7 ай бұрын
We want the two decade experiance of " How would I do it if i start to learn software engineering today " video, Am sure the internet will expolde if you make such video because of the experiance you have.
@champfisk5613
@champfisk5613 7 ай бұрын
You should start when you’re 10
@markokraljevic1590
@markokraljevic1590 7 ай бұрын
the thing i hate about rust is that you can only get blockchain job with it
@kibernick
@kibernick 7 ай бұрын
Read "maturin init" in a Bri'ish accent for extra lols.
@champfisk5613
@champfisk5613 7 ай бұрын
Why would we use rust when we can do all of this in Python?
@renerebe
@renerebe 7 ай бұрын
More like the worst of both languages
@AyahuascaDataScientist
@AyahuascaDataScientist 7 ай бұрын
Arjan simply does not explain things clearly or coherently. People just like his voice.
@kayakMike1000
@kayakMike1000 7 ай бұрын
I am just not sold on rust... Its likely that AI will just be able to read your C code and find menory vulnerabilities, use after free, buffer overflows... When that happens, i think rust will just fade a lot...
@theninjascientist689
@theninjascientist689 6 ай бұрын
If you haven't already, give Rust a try! It's a lot easier (and more fun) writing code that just doesn't have those memory vulnerabilities in the first place.
@motbus3
@motbus3 7 ай бұрын
It smells bad to me if it uses a non compliant pyproject.toml Why not use another name?
@queueoverflow
@queueoverflow 7 ай бұрын
Babe, wake up. Arjan uploaded a new tutorial
Should You Use Dependency Injection Frameworks?
14:43
ArjanCodes
Рет қаралды 43 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 808 М.
Крутой фокус + секрет! #shorts
00:10
Роман Magic
Рет қаралды 27 МЛН
ЭТО НАСТОЯЩАЯ МАГИЯ😬😬😬
00:19
Chapitosiki
Рет қаралды 3,2 МЛН
小天使和小丑太会演了!#小丑#天使#家庭#搞笑
00:25
家庭搞笑日记
Рет қаралды 41 МЛН
How Fast can Python Parse 1 Billion Rows of Data?
16:31
Doug Mercer
Рет қаралды 219 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 549 М.
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Рет қаралды 62 М.
[UPDATE] Mojo Is Faster Than Rust - Mojo Explains More
52:09
ThePrimeTime
Рет қаралды 265 М.
I spent six months rewriting everything in Rust
15:11
chris biscardi
Рет қаралды 431 М.
Rust is not a faster horse
11:37
No Boilerplate
Рет қаралды 327 М.
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 23 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 392 М.
An introduction to Mojo (for Python developers)
15:21
Underfitted
Рет қаралды 9 М.
Why You Should Use Pydantic in 2024 | Tutorial
13:56
ArjanCodes
Рет қаралды 73 М.
Крутой фокус + секрет! #shorts
00:10
Роман Magic
Рет қаралды 27 МЛН