- Podcasts
- Talk Python To Me
- #563: Getting Started with Rust as Python Devs
#563: Getting Started with Rust as Python Devs
Talk Python To Me
- September 16, 2026
- 1 h 11 min
- Episode 563
About this episodeLint the entire CPython code base from scratch. It takes 0.3 seconds. Three blinks of an eye. That is ruff, and it is written in Rust. So…
That is ruff, and it is written in Rust. So are Pydantic, Polars, uv, and Granian. Rust shows up in Python three ways: tools that happen to be Rust, libraries Python imports, and servers that run Python inside Rust. This is Rust for Python developers, not Rust experts.
Christopher Trudeau is back on Talk Python to discuss Rust and his latest course Up and Running with Rust. The core rule is that only one thing can own a value at a time. Pass it around freely in Python and the garbage collector cleans up. Do that in Rust and it will not compile.
Episode sponsors
Sentry Error Monitoring, Code talkpython26
Python in Production
Talk Python Courses
Links from the show Up and Running with Rust course: training.talkpython.fm
Rust: rust-lang.org
pydantic: pydantic.dev
ruff: docs.astral.sh
granian: github.com
By example: doc.rust-lang.org
rust-lang.org: rust-lang.org
rustup.rs: rustup.rs
crates.io: crates.io
main.rs: main.rs
PyO3: github.com
https://github.com/ritwiktiwari/awesome-python-rs: github.com
ty: docs.astral.sh
pyrefly: pyrefly.org
uv: github.com
polars: pola.rs
Watch this episode on YouTube: youtube.com
Episode #563 deep-dive: talkpython.fm/563
Episode transcripts: talkpython.fm
Theme Song: Developer Rap
🥁 Served in a Flask 🎸: talkpython.fm/flasksong
---== Don't be a stranger ==---
YouTube: youtube.com/@talkpython
Bluesky: @talkpython.fm
Mastodon: @talkpython@fosstodon.org
X.com: @talkpython
Michael on Bluesky: @mkennedy.codes
Michael on Mastodon: @mkennedy@fosstodon.org
Michael on X.com: @mkennedy
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#563: Getting Started with Rust as Python Devs
Lent the entire CPython code base from scratch. It takes 0.3 seconds, three blinks of an eye. That is rough, and it's written in Rust. So are Pydantic, Polars, uv, and Granian. Rust shows up in Python in three ways. Tools that happen to be written in Rust, libraries Python itself imports and runs, and servers that themselves run Python inside of Rust. This episode is Rust for Python developers. And Christopher Trudeau is back on Talk Python to discuss Rust and his latest course up and running with Rust. The core rule is that only one thing can own a value or variable at a time. Pass it around freely in Python and the garbage collector cleans it up. Do that in Rust and you get a compiler error. This is Talk Python To Me, episode 563, recorded Wednesday, September 9th, 2026. Welcome to Talk Python To Me, the number one Python podcast for developers and data scientists. This is your host, Michael Kennedy. I'm a PSF fellow who's been coding for over 25 years. Let's connect on social media. You'll find me and Talk Python on Mastodon, Bluesky, and X. The social links are all in your show notes. You can find over 10 years of past episodes at talkpython.fm. And if you want to be part of the show, you can join our recording live streams. That's right. We live stream the raw uncut version of each episode on YouTube. Just visit talkpython.fm/youtube to see the schedule of upcoming events. Be sure to subscribe there and press the bell so you'll get notified anytime we're recording. This episode is brought to you by Sentry. You know Sentry for the error monitoring, but they now have logs too. And with Sentry, your logs become way more usable, interleaving into your error reports to enhance debugging and understanding. Get started today at talkpython.fm/sentry. And it's brought to you by the Talk Python in Production Book, an inside look at 10 years of the real-world DevOps behind the Talk Python sites and apps. Check it out at talkpython.fm/DevOpsBook. Christopher, welcome back to Talk Python To Me. Always nice to hang out with you.
Yeah, oh yeah, this might not actually be the final Ferris joke or whatever, but no, we're going to talk about Rust for Python devs, specifically not for Rust experts, But I want to know enough Rust so that things like Pydantic, ty, if I ended up poking around them or some other extension, it would make sense, right? Or maybe write a little bit of Rust to speed up some of your Python code. It's obviously the most influential way to make Python extensions or core faster pieces, right?
Why not? Just bring them all in. No, it's a really cool foundation for Python things. So we're going to talk about that and dive into it, compare it to Python, give some examples, give some tools, all those things. But it's been a while since you've been on. Give everyone a quick introduction of who you are and what you've been up to.
So yeah, I'm a consultant. I mostly spend time working with teams either on architecture or technical processes. That tends to mean things like agile and lean and automation, CI/CD, all that kind of good stuff. And in my spare time, I still do a fair bit of teaching. And sometimes that's in person, sometimes that's virtual, and sometimes that's creating online courses for people. I spend a fair amount of my time in the Python space. I'm co-host of the Real Python podcast. So that tends to be where people, if they know my voice, that's from where. And lately I've been getting into Rust and figured you and I could chat about that.
Well, so this is it, right? You hear the rust is a hard language to grasp thing. And I don't, I didn't find it that way, but I don't want to diminish people who are saying that, right? Like this isn't because I'm so much smarter or anything along those lines. It's because I have that background. I was writing C when I was 13 or 14 years old. There weren't a lot of choices when I started out. So I have a grounding and, you know, I wrote device drivers and I wrote embedded systems uh don't haven't done it in a long time but i have that grounding that rust kind of expects you to have and it's closer to the machine there's less virtualization going on uh it's a lower level language than something like python uh in fact uh bob builder russ and i were talking about this because he's got into rust recently and he came from php and python so from that it's this whole other kind of experience and he was talking about the fact that he actually thinks it's made him a better Python developer because now he's thinking about memory and stuff like that a little more
than you necessarily would if you've just come from Python. That's really interesting. I hadn't really thought about it because you and I are, I think, around basically the same age, at least technologically speaking. And I remember getting the C++ Primer Plus. This is so nice. C++. It's so much better. Yes. I just loved working in C++ and so on. So I've never known a world where I came to Python or JavaScript or other languages where I didn't already have that experience of, you know, void star star. What do I do with that? Exactly. These kinds of things. But, of course, that makes sense. You come from Python and PHP, and then you're like, whoa, why am I doing all this stuff? This is crazy.
Yeah. And it's, you know, a big part of it. And I think the thing that folks who are coming to it from languages like Python find difficult is that whole memory management thing. And Python basically takes care of that for you. And so if you've never had to do that before, all of a sudden it's like, I have to do what? And why do I have to do that? And it can get a little persnickety. It's concept of only one thing can own a value at a time. it has objects, but the objects belong to somebody. And in Python, you just pass those things around willy nilly and you don't worry about it. And the garbage collector takes care of it. And that often turns into a compile error in Rust. And that's on one hand, it's the beauty because it makes sure that certain kinds of bugs don't happen. On the other hand, if you're not used to it, you know, there's a, there's a joke in the Rust world that there are no bugs. It's, it's all your time isn't debugging. All your time is getting it to compile. And that's because there's a lot of that, Oh, I'm just going to pass this around and wait, I can't. And so, yeah, that there's a lot of upfront work there. And I think for a new developer, that could be very frustrating because, you know, you don't, you can't play as easily. There's the threshold to getting started with something is higher because you're constantly kind of fighting the compiler. The flip side of it is their compile messages are absolutely fantastic. They, so when errors happen, they're very, very explicit. they often include something like maybe you need to do this. And from what I saw, they were right 80 or 90% of the time. So once you figure out the language, I mean like the, not the language of Rust, but the vocabulary they use for error messages, it's very much, oh, oh, okay. So that's kicking off the borrow checker. Now I understand that. Okay. I'll go fix it. So there's a few things you kind of have to learn to get to that, but it is fairly helpful. But again, that could just be my background and remembering days of C where the compile error was being triggered from three lines before it was actually talking about where the error was. And you kind of had to zone in around roughly where it was reporting it to figure out what had gone wrong. So, yeah, there's been some advances in technology since we were tapping out bits and bytes on rocks. Yes.
Before you do, I just want to wax poetic. Think back to the days of back in the 90s. I remember when I had my C++ Primer Plus book. And yes, folks, it was a book that was like the yellow pages. There was no, literally the internet did not exist. Well, the internet existed. There was Gopher, there was Telnet, but the World Wide Web did not exist at this time, like not for four or five more years. Anyway, I remember thinking, oh my gosh, this is so hard. This is so much work when I'm learning it. And finally, finally I got it to compile. Like, oh, we're good at compiles. Like, no, no, no, no. That's just when the actual real hard debugging, problem solving starts. But you mentioned that with Rust, that the compiler checks are a little bit stronger. They check more things like proper memory management and variable sharing and things like that. So I think while it was a eye-opener that compiling was only the beginning of my suffering when I was learning programming, I think compiling does mean more in Rust than it used to in other languages.
Yeah, there's a whole family of bugs that have to do with memory management. And what's worse is they tend to be a tricky set of bugs. They're the kinds of things that will crash sometimes. So if you go back to C and how it stores a piece of text in memory, it has the, you know, like, let's say we're storing the word Talk Python. Well, we take the bytes for Talk Python and then we put a null on the end of it. And the compiler, when the code is working with it, it looks for that null. And that's how it knows that the string is finished. Well, if you accidentally write something longer than that, there'll be no null and it'll keep walking past that into the next chunk of memory. If you free the memory up, but you still have a pointer that points into that memory. Yep. But your computer will happily read that. And if you've got two strings in a row, what'll happen is you'll get the first string and then the lack of the null, and then you'll get the next string. And you might get a wrong message on the screen, but it won't crash your program. But if you get a string and then some memory that is a little more tricky, then all of a sudden you might actually be doing something like causing your program to fall over. So rust isn't bug free, but there is a category of bugs that it just doesn't let you create. And that makes it harder to compile, but a beauty to work with in the fact that a lot of the things that you might struggle with that are harder to debug are gone. Right. You know, I thought the nature of Python is the looseness of the type system and the dynamicism of it means you can get moving very, very quickly. But if you're not using a type checker, type mismatches are things you discover at runtime. All of that is very strict and very well managed in Rust. And so those kinds of problems just don't exist.
That's awesome. It also takes it's also mind bending. It is. In some ways, right? It's the way you think the way a lot of the ways you think about solving problems in C++ or C or Python can be kind of the same in Rust. Some of these limitations sort of change the algorithms and the data structures that will work in those worlds. Yeah. Yep. Well, and, you know, take
something as simple as an integer in Python. They're boundless, right? And if you're doing certain kinds of math, that's fantastic. There's no upper limit. Python takes care of all of that for you. It figures it out. I actually saw an article a couple of weeks ago where somebody was building a SIMD machine based on integer, based on, doing bitwise operations on massive integers, right? Thousand byte integers, and you can get away with this. And Python just happily goes, yeah, that's a really big integer. No problem. Rust is closer to the machine. So it tends to use integers the way your CPU uses integers. So you have to be aware if you're using an eight bit integer that you can't put nine bits in it. Things go wrong. Now the compiler, most of the time can catch that depends on how you're getting into that situation. And so it, it stops you from doing those things. But of course now you have to be conscious of it, right? You're making a decision when you're designing your program. How many bits am I going to store this in? And most of the time that's not a problem, but sometimes that might mean you're hitting boundaries that you wouldn't otherwise expect to hit.
And interesting, like a lot of that stuff is in Python, but it's buried further down. So you can get at some of those same concepts inside of Python, but if you're just starting out, you don't go digging for those things. Right? So if you're, you know, if you're playing with the network and you're trying to pack bytes together, Python has signed and unsigned and it has those sides pieces. And all of that is very, very important if you're constructing the perfect packet. But if you're just trying to do math on the command line, well, you don't need to think about that kind of thing. Rust doesn't give you the choice. And that's not Rust specific. That's most compiled languages. They don't give you the choice, right? And it's also where the speed comes from, right? So that beauty of Python's limitless integer means there has to be machinery inside of that to handle when you make it bigger than what the current chunk of memory is. And rust doesn't have that machinery. It just says, okay, I'll crash. and so it's, it's a trade-off, right? So you don't have to care for as much, but one of the reasons Python is slower is because there's the overhead that handles these kinds of things. So one of the reasons, you know, the flip side works as well. One of the reasons rust is faster is because it's optimizing for exactly what your CPU does. And that less overhead means your code runs faster.
Right. You're never going to get a Python integer into a CPU register. Right. Like it's just, I don't know. Maybe. I remember when I came to Python, I did C++ and C#, all languages that care about the shape and size of these variables, numbers, and for 15 years. And I came to Python, I'm like, I got to be doing something wrong. How do I tell it again how big the number is? Because what kind, like, is it just, is it a 64 bit? I couldn't figure out like, what is, how do I, it's going to be a problem. And then I'm like, wait, it just grows. That's crazy.
Yeah. Well, I think I started with Python more as a scripting glue kind of thing. And most shell languages don't have, like Python, just don't care about those things. Right. And so when you're using it loosely to glue other things together, I don't think that really kind of ever crossed my mind. And I think I got used to the idea by that time I was doing, you know, web servers and things that were bigger, more interesting projects. And I think a lot of this is where are you coming from and how are you using the tool, right? So I think for the first couple of years I was doing Python, I was writing, you know, at max 50 or a hundred line scripts to solve some immediate little problem. And it's nice not to have to worry about that kind of thing, right? So all of this really is, you know, what is the tool and how are you using it for what you're trying to accomplish?
This portion of Talk Python To Me is brought to you by Sentry. You know Sentry for their great error monitoring. But let's talk about logs. Logs are messy. Trying to grep through them and line them up with traces and dashboards just to understand one issue isn't easy. Did you know that Sentry has logs too? And your logs just became way more usable. Sentry's logs are trace-connected and structured, so you can follow the request flow and filter by what matters. And because Sentry surfaces the context right where you're debugging, the trace, relevant logs, the error, and even the session replay all land in one timeline. No timestamp matching, no tool hopping. From front-end to mobile to back-end, whatever you're debugging, Sentry gives you the context you need so you can fix the problem and move on. More than 4.5 million developers use Sentry, including teams at Anthropic and Disney+. Get started with Sentry logs and error monitoring today at talkpython.fm/sentry. Be sure to use our code, Talk Python26. The link is in your podcast player's show notes. Thank you to Sentry for supporting the show. I do, before we get too much farther jumping into it, I do want to point out that you wrote the Up and Running with Rust course over at Talk Python. So thank you for that. I really enjoy this course. It's both very educational and funny.
The beauty of doing this kind of stuff with a sense of humor is you don't actually have to be a comedian. It's not like everything is a joke, but you can stick some humor in once in a while. So I do wonder occasionally whether, again, because of the similar generation, half the jokes might have been aimed specifically at you. So just because you find it funny doesn't mean other people will.
I think they will. At least the lightheartedness. We'll see what happens. We'll come back to that at the end. This is kind of not exactly the same. It's not just that we're going to turn the course in the podcast, but it's inspired some of our concepts. Now, I want to start by talking about Rust. Just some examples before we dive into how is it compared to Python. We've touched on a little bit of that, but there's a bunch of differences and so on. But let's just appreciate some of the things like Pydantic, Ruff. Give me your thoughts on the last five years of these tools. have kind of supplanted a lot of things that were very well established in Python.
So there's, there's, there's kind of three ways that Rust intersects with the Python world. And one has almost nothing to do with Python. It's they've used Rust to build tools that are Python tools. The, they could have just as easily done that for any other language, right? So when you talk about things like the linters and those kinds of things, they're, they're useful because they're fast. they are not, they're not really integrating with Python. They're a third party tool that you're using to make your Python better. And I think so some of the, some of the popularity in that has come out of the fact that a lot of the tools we've been using historically in the Python world have been written in Python. And so they've been very slow. And most of the time that doesn't matter. You know, there's that, you know, that programming world of, oh, I'll just go get another cup of coffee and it'll be done when it comes back. But your world shifts when you start seeing, you know, orders of magnitude, right? Like 10% faster, you're not going to notice it when you're starting to talk multiple zeros times faster, this changes things. And I think there were a couple of key libraries that started doing this and everyone kind of went, oh, wait a second. There's some, there's some value to this. And so I think there's been this real spring up of things written in Rust that make Python development easier.
Yeah, I think the one that was most stark to me was Ruff because it was just unreasonably fast. Yes. I remember running on the Talk Python training, all the courses, like that app. And that app is much bigger than people think from the outside. It's 170,000 lines of Python or something, which is not huge, but it's not a toy, right? And so I remember running Ruff on it and it goes, found these four hours. I'm like, yeah, no, you didn't. Wait, wait. Did I not tell it to like go into the subject?
Yep. I also, I tend to be fairly slow to upgrade my machines. So I'm often a couple of generations out processor wise, and it's just that much more, right? Because like when you're, when you're operating on a slow machine to begin with, and then you're trying to do something like lint a couple hundred thousand lines of code. Yeah. It makes a huge difference. It makes a huge difference.
Yeah. And I think that's kind of the key to this category, right? whether it's written in rust or C or anything else is kind of indifferent. It's, it's a tool. they, they're particularly with things like linters, you know, the, the, description of the Python language is publicly available. There are tools out there for compiling, for compiling language parsers. So like you can, you could have taken something, else and use that to take the definition of Python and built a thing that parses Python in any other language. It's just that Rust seems to be, has, has become the thing that is now the way to
do some of this stuff. Yeah, absolutely. And just give like other numbers, you know, instead of my own personal example that people can't exactly relate to for parsing for linting and pars, evaluating this, the entire CPython code base from scratch is a 0.3 seconds. Yeah. That's, that's like enter the three blinks of an eye. Yeah. You know, that's pretty ridiculous. Yeah. So that's one category. The other category is closer to our hearts with things like Pydantic and so on.
So like I said, there's kind of three modes. The second mode, and I think it's the most common one, is because Py... And this isn't really... Again, this is one of those things that's not quite Rust-specific. Python was designed from the get-go to integrate well with lower-level languages. And in fact, there's a good chunk of the Python standard library, which is really just a thin wrapper on top of the C standard library. And in fact, the older stuff in the standard library, you can actually see it. It's a little creaky in some of the naming and stuff before they kind of got consistent about how to name things. And you look at it and if you're a C programmer, you're just like, oh, I know exactly what that is.
So Python has been designed to be able to do this. And it just turns out that I think a lot of people are coding in rust have found it easier to code than encoding in c that same interface that c extensions can use can be used by rust and so as a result libraries like pydantic libraries like polars they have written their library in rust and then with a very very thin veneer there doesn't take it doesn't take a lot of effort to expose that up and then python inside of python you can treat that like any other python module so you don't actually have to be conscious of how they And as a result of this, you end up with a lot of speed up because you use that same speed that you were talking about with rough. But now Python, which tends to be a slower language, can now access this kind of stuff. So something like Polars or Pydantic, where you're doing a lot of crunching and you want them to be fast and performative, this marriage between the dynamic library on the hard drive that is a compiled thing as a Python module, that integration has been very, very handy. And I think this is the place where Rust is starting to shine because it's a cleaner, although it can be a little frustrating, it's a cleaner language than C. There's a learning curve, but there are a lot of things it doesn't let you do. There's fewer foot guns. And as a result, it's a nice marriage between a low-level language and some higher-level concepts, right? So it has things like iterators and collections in it. And so if you're coming from Python, that's like, oh, how do I deal with that? Oh, it's the same kind of for in loop and it's the exact same thing, right? So there's enough similarity to more modern languages that this is becoming a nice sort of collection of pieces that worked well together. And in the cases of things that need to be really fast, this is a real answer.
Yeah, absolutely it is. And taking your Rust code and exposing it as a Python package with the tools like Pyotr3 maturing that we're going to talk about obviously they they make it super easy it comes down to just okay well you need to build more variants of your wheels rather than just a source only one yeah i think i have an analogy for these types of things so we have one third uh category i guess which i would put as granian and other things which is a web application server that runs that is built and runs in rust but then your code and python still runs so i have i have an analogy chris tell me what think here we've got the caramel dipped apple where it's caramel is python okay so we've got the caramel dipped apple where python is on the outside with like a rusty core yep that would be pydantic we have maybe python maybe it's the way anyway we've got Granian and other things where it's like more like a caramel m&m on the outside it's like a
So yeah, so that third category is within rust code, you can embed the Python interpreter. And so from a rust, program, you can now use an access Python. So you can kind of use Python as a scripting or, or configuration language within a larger, mechanism and something like granian where you want to actually run Python code for a web framework, that, you know, so that, that inverts the relationship we were talking about before. So yeah, so that's our three, right? So it's separate tools, the using Rust as a module inside of Python. And then the third one is using Python as a compiled library, using the interpreter as a compiled library inside of the
Yeah. And I was about to say, you know, to start with differences, it might actually be easier to start with what's not different. The language itself, there are some things, obviously, if you've never, if you've never seen it before, it takes a little, and that's the same with any language, but it's not going to be completely out of nowhere. If, if I hand a Python developer some fairly clean Rust, you're probably going to have an idea of what it's doing. The loop mechanisms are the same. The if else is similar. There's no L if you actually use if else. I always scratched my head
suspicion. It might've made something early in the early days easier to parse, but rather than parsing two keywords, it gives you a keyword. A lot of the built-in mechanisms, like it's got tuples and arrays, it's got iterables and collections. So a lot of the declaration looks like typed Python. So because it is a strictly typed language and you have to specify what everything is, you do have things like the arrow that this function returns something of this type, right? So you've got that little arrow designator. But if you're used to coding in typed python which i'm not uh then you uh then then it's readable from that perspective it is yeah
the types are quite quite similar uh as they you know um for the way you define them right if you have like a function or something then you just say like arrow goes to i always read that as goes to pool if it like returns a pool the variables are variable name colon type right yeah yeah and
I think it's the same that everybody stole from C, you know, C is the granddaddy of most of the languages we use. And then I think people started adding these kinds of concepts to it. And it's like, well, if you've come from that other language, this makes it simple. So yeah, there seems to be a sort of family of how this stuff works now.
No, I think we've hit the limit of the similarities. So it doesn't take long. It's relatively readable in the same fashion, but it doesn't take long before you have to kind of peel the top layer of that onion and then you're going to get into your differences. At the heart of it, one of the fundamental concepts is it is a compiled language. This is one of those things that I think I always find sometimes frustrates students because you end up with the, well, Python compiles. I can see the code right there. Python is a interpreted language that interprets a binary file. So when Python is compiling, it's creating a binary target. That binary target is not platform specific. It is just a, you can think of it as a data file the same way, like Excel is a data file, right? So when you, when you write a formula in Excel, you don't think of that. It's not quite compiling that formula. It's storing it in binary. And then Excel is running that formula. By contrast, Rust actually compiles to machine code for your platform. So when I come, unlike a collection of Python bytecode, I can hand you that. And I don't have to worry about what platform you're running on because your interpreter is specific for you. The bytecode is not. If I hand you my binary, if you're on the same kind of platform I am, it'll run. But if you're not, it won't. And so what Rust is actually doing there is creating a binary, tight binary file that is specific to your platform.
And you know what? I'm jealous. I'm jealous from a Rust and Python perspective. You can compile it and hear, hey, I'll give you a file. You can run it. You know what I mean? Yes. I know there's a lot of reasons and it's not easy, but wow, would I love a Python --build thing and outcomes just, I don't care what's in the inner core, right? You know, back to the like caramel M&Ms, but just a thing that I could put on a server, I could give to a person, I could put in Docker, and it doesn't have to have a bunch of other stuff there to make it possible.
Yeah, it's a pros and cons thing, right? So your distribution is way easier, but it's very specific distribution, right? So you end up, if you're trying to aim at multiple platforms, you have to compile across all of those platforms, but the distribution of it is basically, here's the execution file, go nuts, and that's it, you're done. Or the collection of them, if you've got dynamically loading libraries. So that is definitely easier. the flip side of it is things like repls aren't technically possible because it needs to compile stuff and in fact there's some hacks out there that are kind of hilarious that are essentially repl like things that are in the background are basically recompiling a program every single time you enter a line in the repl exactly so it's actually somewhere in there yeah it's actually maintaining this program for you in order to make it look repl like and as fast as rust is i found painful i played with it once in a while and i sort of gave up and just went back to i'll have
doing this portion of talk python in me is brought to you by us i'm excited to talk about my first solo book, Talk Python in Production. It's an inside look at how we host all the Talk Python sites, APIs, mobile apps, and way more. Here's the thing. I believe most hosting stories sold to developers and data scientists are way overcomplicated and overpriced. You've heard me say you're not Google, you're not Netflix, so you shouldn't run your infrastructure the way they do. But if not that, then what? This book is both a blueprint for what I chose for Talk Python and a story arc of 10 years of running my own infrastructure from a complete newbie, apprehensive to Linux, to some pretty neat infrastructures code DevOps. It covers Docker, Nginx, Let's Encrypt, self-hosted analytics and monitoring, CDN setup, framework migrations, and a whole philosophy that I've termed stack native, keeping things streamlined, powerful, and free of cloud lock-in. And it's more than just your standard tech book. It comes with code and figure galleries on GitHub, a discussion forum, and something unique, over an hour of audio readers briefs, short conversations that bookend each chapter to prime your focus or broaden your takeaways. Oh, and 0% of this book was written by AI. Every word is mine, written over the course in high months, for better or worse. I've made the first third of the book available for free online. After that, you can grab the DRM-free EPUB and Kindle editions, and I'm working on a paperback edition as well. Please check it out at talkpython.fm/devops, or just click book in the nav bar on the website. It's a great way to support the podcast, and I hope it changes a bit how you
think about running your apps in production. Okay, and so I guess, and that kind of, and we were already sort of talking about this, but this leads us to this idea of the strict typing mechanism, and that really comes out of this idea of the hardware, right? So your CPU has an idea of what an integer is. Your CPU has an idea of what a float is. There are different sized integers. How much memory is that integer going to be stored in? How much memory is that float going to be stored in? So not only is Rust specific about having these things, and you have to declare them when you're using them, but it also has that mapping to what your hardware does. So you have to make a decision between a signed integer versus an unsigned integer. So if you've never played with this kind of stuff before, when you stick a number in memory, that number represents just some bytes and your CPU is interpreting those bytes to mean something. And if you could have it, you could have it represent a positive number. And in the case of a single byte to it, that's from zero to 255. But if you want to also have negative numbers and store it in the same amount of space, well, then now you have to sort of map something from 0 to 255 into the negative space. And so you end up with like minus 127 to a positive 127 or 128. Let's not get into that. But essentially, the computer is interpreting those same eight bytes in a different way to represent a mix of negative and positive numbers. The downside of that is you can store less. You can only store up to was half the size of a positive number. In Python, you don't care about any of this, again, unless you're doing packets, but in Rust and C and other languages like it, you have to actually explicitly decide which of these you are using.
They're not going, they're all random places out in the heap. So, I mean, there's arenas and blocks and all that stuff to try to like get them closer together, but they're still just out there rather than I've got an array of 10 numbers. Well, guess what? Those numbers are contiguous in memory sort of thing, right?
And you've kind of hit on something that is also very explicit in Rust. Now, you can start Rust programming without fully understanding this, but you're not going to get very far without it. It's fairly explicit about what goes on the stack and what goes on the heap. So again, if these are new concepts for you, when your program is written, the stack is where function calls happen. So when you move from one place in code into another in memory, what happens is the new address, the place you're going to, gets put onto a stack. The old address is there before. So when your function returns, it pops off the old stuff and then knows, oh, that's now the thing at the top of the stack. That's now the address I'm going to run at. And so when you add variables into, when you add parameters to your function, those are also getting put on the stack. So you end up with this little stack frame, which is the place you're going to be running and the data that goes with that function. By contrast, the heap is sort of a general space, and that is where you allocate things from memory. And longer lived things or things that you're passing around between functions tend to end up being created in the heap. And how Rust does memory management, we'll gloss over it for now, we'll get into it a little more later, I think, but how Rust does manage memory management differentiates between these two kinds of things and so again this is one of those things you you know you only think about the stack in python when it fails because you get the stack trace but otherwise you more or less ignore it whereas here once you start playing around with passing objects around whether or not you're being explicit about it that concept of am i in the stack or am i in the heap and who owns it becomes very very important and and this isn't new to Rust. This has been there in compiled languages. This is a fundamental way how modern computing machines work, but it's more exposed in Rust than it is in Python. You know, the thing I
think is such, to me, it was a little ironic is Python, you don't worry about pointers. You don't worry about that. You just work. And then like other languages like C, Rust, C#, you talk stack, you talk heap, you talk pointers, you can dereference. Python is more pointer heavy than any of those languages because literally everything is in the heap. There's nothing that's even a
That's like, if everything is a pointer, we can just treat them all like variables, right? And that made the programming simple and you don't have to think about it. But conceptually, Python, everything is in the heap. Everything is a pointer. Even the number one, it's a flywheel pattern. I
Yeah, it's something you don't want to think about too much. A couple sort of cosmetic things, which again, if you're from Python and Python's your first language might take a little to get used to. one is brace brackets so uh like most programming languages space is not important uh and that takes i i remember it taking me a while to get used to that switching to python so i have uh patience and sympathy for people who started in python and have to go the other way this is pretty typical of most programming languages uh the it makes the parser's life easier um and i think after having coded in Python for almost two decades, I think I've come down on the side of screw the parser. So it's a little bit taking a little used to for me to get back to it. But yeah, you, you put things in brace brackets to denote them.
Yeah. I mean, look, the job of the parser is to make your life easy. Not the other way around. So I get, but I have run into situations where some little bit of formatting has been off. Yes. On my indentation and a reformat the code broke the code because maybe I had three indentations, three spaces instead of four for who knows, maybe I hit the delete button on accident or something weird like that. And I said, oh, well, look, this didn't line up right. Like I'll just hit like reformat code. And it's like, oh, well, that needs to go back to the left. And it's like changed actually how it works.
It's really problematic. And with the braces, like it could just have no indentation. It could be all gnarly. It could be minified, whatever. You just hit reformat and boom, it's reliably put back together well. So I'm going to give Python an 80% plus, 20% minus. I agree that it's nice. It's not there, but it's not just there is value to it. Structural correctness value to it.
It really is like, and you know what, there was a gosh, what PyCon was it? I think it was one Cleveland, maybe three, four years ago, there was a PyCon where one of the vendors clearly came. It was a tool vendor that came from all the, they weren't Python specific, but they made tools for Python people. and I would even maybe shout them out, or I'd call them out, but I don't even remember what it was. But they were giving away t-shirts that said tabs versus spaces. And you had to pick, I'm team tab, I'm team spaces. I'm like, what are you doing? Does nobody know that this literally is not even a choice in this language? Why are you here? It's a fun gag, but anyone on not team spaces is not going to run. What is wrong with you?
Yeah. That's a team spaces versus teams. My code won't compile. Yeah. Yeah. An interesting side effect of this that I kind of like in this language. I don't know if I've seen it elsewhere. So like most languages, it has this rust has this concept of a statement versus an expression. Most things are expressions. And in fact, your brace bracket is the definition of a new expression. And as a result of that, the end of the brace bracket can return something. So you can do an assignment to a collection of code in a brace bracket. So this ends up being kind of like an inline function. and so that's kind of interesting to me and it, it, compiles down beautifully. it's, it's got that, and then it does this weird thing where it's got implicit
arbitrary expressions through the brace brackets is kind of elegant. So, so yeah, one point up, one point down, whatever. and then, you know, there's some odd odds and ends, right? It's, it's, uh, FN instead of def, which quite frankly, as soon as I saw it was like, yeah, that actually makes an awful lot more sense. That tells me what it is. Def for define, define what? What am I
liked a lot is, so they release a lot more frequently in Rust. So it's, there's a new, a new version coming out every six weeks, which when you come from the Python world, that sounds insane, right? Like how do you stay on top of this? But they've got this separate concept from versions that they call additions. And an addition is a three-year cycle and things stay compatible within the addition. And what's more, not only do they do that, but the compiler is able to compile to older additions. So when you compile the code, you can actually say, I want to be using an older addition. And so they have basically this forever concept of backward compatibility. So now it's still a young enough language. I suspect at some point they may give up on that. cause this, this to me smells of maintenance hell. It's fine when there's three
fair amount of change in the language, in the last few years, you're not screwed by it. And it's not this constant race to stay on top of it. You can just change the edition target and you're fine. So I kind of like the idea. I think it would be kind of neat if the Python compiler, maybe, you know, if I was, I guess the changes in Python aren't as bad, so maybe it's not as extreme,
but I don't know. This just sort of appealed to me. Yeah, it does to me as well. I love the freshness of it. I just got a new version of Rust this morning. I was trying to update my libraries and tools, and it said there's a new version of Rust. Yes. Okay. So I think that might be a good good time to maybe talk about like getting started and so on as maybe start by installing yeah uh so
So rest up, rustling.org. there is right on the front page. There is a little shell command that you install. This actually installs a tool called rust up rust up is kind of like pie M and that it handles installation updates of the language. It install, it installs, rust C, which is the compiler and more importantly, cargo, which is the tool. That's kind of like uv that is what you actually use. You almost never call rust C directly. So rust up handles the version management of all that kind of stuff. And once you've got that going, then you use cargo to create a new project. Again, very uv-esque here. So you would say, you know, if I'm doing hello world, it would be cargo new hello. That creates a cargo.toml, which is the equivalent of pyproject.toml. It creates a source directory. By default, it creates a git repo and a git ignore file, which drives me nuts. And I turned that off as soon as I could. I want to be in control of that. Don't do it for me. And it creates a little stub main file, which actually has a function in it. So you actually can run cargo new hello and you get a compilable program. Then you write your rest and you run cargo run and cargo run builds and runs the code for you. Now, because it's a compiled language, the result is a target that goes into the target directory. You could run that explicitly, but it's buried like three directories down. So rather than do that, you just call run and it does it for you. And there are flags you can use it to go from debug mode to release version. Your debug tends to be about 30% larger because it includes debug information.
And so it handles all of that kind of good stuff for you. And then this is then the thing that you would distribute if you were trying to distribute it. There's a command for building on its own and then the command for run both builds it and runs it. One of the things I'm still not quite used to is it outputs some information from the build before it actually runs the code. So you get two or three lines of info before you actually see the output from your program, which drives me nuts. But there is a dash queue to tell it not to do that so that if I'm running it, I don't want to see your compiler stuff. I want to actually have it run unless something went wrong. So yeah, cargo really is the key to doing all of this kind of stuff. Like uv, it also manages what they call crates, which are the Rust equivalent of wheels. There is a crates.io is the equivalent of PyPI. And you can write libraries and upload them there. And like with Python, this is a fairly robust community that's writing and trading code here.
No, and it's also not a fair comparison because proportionately the number in crates would actually seem smaller to a Python developer. And what I mean by that is if when you're coming from the batteries included world, yeah, there aren't a lot of batteries in Rust. So something as simple as like a random number, that's a crate. And they've done this to try and keep the footprint small, which I kind of understand. For somebody who's starting out, this can be a little, so like I had this, I'm paranoid, right? So I want to know where my code is coming from. If I'm in the Python world, I trust the standard library. If I stick to the standard library, I know I have a degree of safety. Once I step outside of the standard library, I want somebody to tell me they've used it and they trust it. And, you know, for certain kinds of libraries, there's enough press that you can kind of get the idea. I haven't yet quite figured out how to get that information in the Rust world. And because things like random, which you use all the time, are actually crates, you kind of have to do a little bit of the, there's a little more rolling the dice on do I trust this code to use it kind of thing going on there. But I suspect that's still my newness to the community.
Oh my gosh. And sometimes you hear the pounding on the door. You're like, oh my gosh, go away, please don't do that. Let's just like reflect for a minute. We talked about like cargo crates and all all these things, we've seen a big revolution in Python packaging, deployment, flexibility, project management, all of those things. And much of that has actually come from the Rust side. A lot of the tools like uv and others are seriously inspired by cargo, the crates, and these types of things. But this was built in a world where PyPI already existed and npm already existed. And they could go really good. Ooh, that's not good. Let's do it this way. Right. So there's like this sort of interplay or this circling of these two worlds. Yeah. It's, you know, without,
I'm not trying to start a flame war, but the decision that the PSF made that, managing packages wasn't really part of the language and that we should have this plethora of third-party tools to do that. I think that was a mistake. And I think the Rust people recognize that and decided to bundle this in. it's, it's a lot cleaner and there's a lot less wondering of what's going on. and, now in defense of rust, sorry, in defense, I don't know who I'm defending. Nevermind. I'm not sure whatever I'm going to defend. I'm not going to defend. Uh, the, I think the target for rust is simpler. And, part of the thing that happened in the python world is because of that dynamicism it creates all these other kinds of complications
it's a challenge i agree but also wheels do exist now yep in the python world created them and they didn't before and that was like a super hard problem because yeah everything needed like all these bespoke tools just to install something interesting. And then they said, no, no, no, let's just make wheels. You can just have like, why do we have to make everyone recompile everything from source? So they kind of got there. Another thing I think- They're making progress. Yeah, no, they're making progress. Yeah. Another thing I think is interesting around this is you talked about the sparseness of the standard library equivalent of Rust. I feel like Python batteries included the richer standard library. It is that way because PyPI didn't exist. The concept of a package manager didn't exist when Python, you know, and honestly, like what was 91? Like the web didn't exist. Like getting stuff afterwards was really, really hard. If it were easier, they may have made different choices.
Yeah. It's, it's a consequence of this language being around since the nineties. Right. So the decisions up front were, and what, and what it was designed for versus what it's grown into. you know, that's, we should all be, as software developers, we should all be so lucky as having our initial designs become questionable because 35 years later, people are still using our software all over the planet. Yeah. If people want to criticize mine because it's so popular in 30
years, I'd be happy as well. Yeah, exactly. All right. We've got a little bit of time left, but it's not a ton. I think let's focus on one more section and that would be, how do I go from Rust land to Python land. So there are different libraries for doing this. The most common one out
there is a crate called Py03. And essentially what this does is provide you with the tools to align your Rust code as if they are Python objects. And then there are a couple other tools that you can use to actually do the packaging part. So what Py03 does is it defines the interface the compiled object, which is a dynamic library, so a.so file or a.dll, and then does that in compliance with Python's ABI. And then there are tools out there that you can use, which will put those inside of a virtual environment so that Python can see them. And then in Python, you can load that code that you wrote as if it were a regular Python module written in Python. The how Py03 does this is a couple different things. One, it has Rust objects that are compatible with the Python objects. So as an example, Pyint is a Rust object that represents a Python integer. So if you need access inside of your Rust code to the integer inside of the Python space, this acts as a mechanism to get at it. It will also map Rust concepts. So rather than using pyint, you can just use, say, a int32, and py03 will take care of mapping that up to the Python integer when Python's using it in the Python side of the world. The other thing that it does is Rust has a mechanism that is macros for changing how things get compiled. And py03 ships with a bunch of macros that essentially you attach to something like a function. So if you want the function or a class to be available to Python, you wrap it with one of these macros. It's just a regular Rust function, but the macro is what does the mapping and makes it available. So with the right use of the macro, you can say things like, you know, this is my function. It's got three arguments. These arguments need to map to this, et cetera. And essentially what happens when you compile it is all of this gets put into a.so. And then you use tools like Maturin or others that then put that into a virtual environment and to handle the packaging side of things so that your Python code can use it. And from Python's perspective, once that's installed in the right place, you just import foo from bar as you would with any other module and you've got access to it all.
Interesting. Now, it doesn't have to be all or nothing as well. For example, remind me how to do this because I forgot how to do it. but you can create a project that has some Rust code and some Python code and then build that into a wheel. But you could say only part of my code is gonna be Rust and the rest of it that's not performance critical, that's all Python all day long.
Yeah, so for example, if you look at, if you pay close attention when you install Polars, you're actually getting two wheels, one of which is a universal wheel, which is because it's universal, it only contains Python code. And the other of which is it's figured out what platform you're on and gives you the actual compiled Rust version of the Polars library. So they've kind of got these two things going on. Now, I don't know Polars well enough. I've never looked at their source code, so I don't know what they've got in each one of those packages, but they've built something that is comprised of both of those things. In their case, it's in two separate wheels. It doesn't have to be. And essentially, you're using a tool like Mature and it takes care of this for you. So if you're after a, if you're after building a library where there's a part of it that has to be very performant, but you want to write most of it in Python because, you know, it's easier to code in, then there's, you basically do that and Maturin will let you bundle all of that up. And this is no different than any other plugin mechanism. It's just that these are the tools that Rust provides to build those kinds of plugins. So if you think of tools like Polars or NumPy or any others that do this kind of concept where some of it's written in a lower level language, I O three just lets you do that.
Yeah. The challenge you have to pay is every time you do this, you're crossing a boundary and there is a cost to crossing that boundary. So, there, if you're writing the code in such a fashion that you're constantly going back and forth or your objects are constantly going back and there's overhead to that so writing in and rust doesn't guarantee that it will be faster could make it slower it could make it slower like with everything uh but if you're doing something you know it's something like polar's where you're doing big number crunching uh and you're you know you're calling into the api and then letting it crunch and then it spit another answer out then
yeah you can you can see a significant speed up yeah or pydantic where it comes in as json anyway and it's got to be transformed to something, let that happen in Rust and then talk to it. So yeah, this is really good. I'm going to have Pablo and Laszlo on to talk about the new profiler that's in 3.15. That's really interesting, the profiling module. One of the things that's fascinating about that, I don't have anything in production in 3.15. All my stuff's in 3.14 or lower, like older. Now, one of the things that's cool about the new profiler is you can attach it to stuff running in production, profile it, and then walk away. You know, like, hey, let's just go, it's running. A bunch of people are using this app, like it's an API or something. Let's just hook onto it for 30 seconds, get some data and step back. And then maybe you could look at, oh, actually, if we could just make this one part faster, that would really be a big deal. So I think the possibility for this is just getting easier.
Yeah, well, and, you know, the general advice I give folks when I talk about optimization is don't and then measure, right? So when you're trying to figure it out upfront, it doesn't matter how good you are at this. You tend to make the wrong assumptions eventually. And so, yeah, build something, then profile it, and then figure out where you're spending all your time.
Yeah, I've had a couple of experiences. I won't go into the details of it, but I've told it a time or two way, way back in the day where I was so wrong. Yep. So wrong about what was slow and what was fast. And if I had just pursued my intuition, it would have been months of bad choices.
Yeah. And there's a concept called Amdahl's Law, which is essentially, I'm going to oversimplify it, but if your code is spending 10% of its time in that place, speeding that up by 2x is only ever going to get you a 10% gain because your code's spending the other 90. There's a diminishing return aspect to it as well. And as programmers, I think we often get tied up in the, oh, wouldn't it be cool if I made this code three times harder to read and it'll be, you know, half a millisecond faster. And how often do you call that? Once a year. Oh, okay. Well, there's a month of your life you got back in order to optimize by a millisecond, right? Where you got to find the stuff that is actually causing the problem.
Yeah. You gave a 50% speed up example. Like, let's take it to an insane level. 20% of your code, 20% of time you spend in some bit of code. If you could make it infinitely faster, the best you're going to achieve is your program is going to be 20% faster. You know, maybe, oh, I was running this on a single CPU thread. I'm going to actually get a GPU machine with a crazy thing and do a bunch of GPU programming. And I can parallelize this and I'll get it down to a thousand times faster. Is that worth a 20% speed up? If it takes five milliseconds, you've saved one millisecond. hooray look how much harder your life and how much more expensive deployment is and so on right yeah yeah so it's certainly uh something that people should keep in mind all right well i think let's let's wrap things up here rust is super interesting i think it's got a place in python and even if you don't want to write rust a lot of these really important libraries are somewhat backed by rust so you want to read them and maybe contribute to the to the caramely shell or the noogity inner, but you still kind of want to understand the bits. I think maybe a quick shout out to your course. People should definitely give this a listen or a watch. Just talkpython.fm, click on course at the top. Rust will be, the Rust course will be right at the top of that list.
Rustlang.org is pretty much the place to start. It's remarkably well documented. There's a lot of good guides and things out there. The documentation right on that site, they've got the Rust book right there. It's quite easy to follow. It has a lot of really, really good examples in it. So that's definitely a place to go. The other space, just a bit of a shout out would be, there's a awesome list called Awesome Python RS. And it's just a massive list of the tools and libraries that use Rust in the Python space. So if you're looking for, rather than like education book examples, you're looking for places that actually use it and you want to, and you want to troll through some code, that's a great place to go looking for interesting libraries to take a look at. Yeah. I second that. I'm,
mind, you know? Yeah. And, you know, there's some very classic examples on this list of like drop-in replacement for speed things, right? So there's a cryptography library and there's a JSON library, both of which are basically drop-in replacements for stuff from the standard library. And because they're written in Rust, they're screamingly faster. So, you know, that example of looking for the thing that you need to optimize, there's some good ideas in there that you can dig into.
And, and a lot of the, Python things that are like Python libraries that use rust, a lot of them are actually just thin wrappers to existing rust libraries. So that somebody is rather than writing something from scratch, they're like, Hey, there's this neat library in Rust that does this thing that I like, and I wish it was in Python. And they write like four lines of code that are essentially the API wrapper down to make it, expose it as a module. And now we can take advantage of the fact that there's this whole other community that's trying to do these efficient things.
Yeah, bye-bye. This has been another episode of Talk Python To Me. Thank you to our sponsors. Be sure to check out what they're offering. It really helps support the show. This episode is brought to you by Sentry. You know Sentry for the error monitoring, but they now have logs too. And with Sentry, your logs become way more usable, interleaving into your error reports to enhance debugging and understanding. Get started today at talkpython.fm/sentry. And it's brought to you by the Talk Python in Production Book, an inside look at 10 years of the real-world DevOps behind the Talk Python sites and apps. Check it out at talkpython.fm/DevOps book. If you or your team needs to learn Python, we have over 270 hours of beginner and advanced courses on topics ranging from complete beginners to async code, Flask, Django, HTML, and even LLMs. Best of all, there's no subscription in sight. Browse the catalog at talkpython.fm. And if you're not already subscribed to the show on your favorite podcast player, what are you waiting for? Just search for Python in your podcast player. We should be right at the top. If you enjoyed that geeky rap song, you can download the full track. The link is actually in your podcast blur show notes. This is your host, Michael Kennedy. Thank you so much for listening. I really appreciate it. I'll see you next time.
