Rust Handles Errors Way Better Than Python

16,323
0
2024-04-26ใซๅ…ฑๆœ‰
In this video, Iโ€™ll show you how Rust handles errors and when to use which type of error handling, based on my own experience.

๐Ÿ”ฅ GitHub Repository: git.arjan.codes/2024/rust_error_handling

๐Ÿ’ก Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.

๐Ÿ’ป ArjanCodes Blog: www.arjancodes.com/blog

โœ๐Ÿป Take a quiz on this topic: www.learntail.com/quiz/apifir

๐ŸŽ“ Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level-python
The 30-Day Design Challenge: www.arjancodes.com/30ddc

๐Ÿ›’ GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.

๐Ÿ‘ 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:
๐Ÿ’ฌ Discord: discord.arjan.codes/
๐ŸฆTwitter: twitter.com/arjancodes
๐ŸŒLinkedIn: www.linkedin.com/company/arjancodes
๐Ÿ•ตFacebook: www.facebook.com/arjancodes
๐Ÿ“ฑInstagram: www.instagram.com/arjancodes
โ™ช Tiktok: www.tiktok.com/@arjancodes

๐Ÿ‘€ Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
- Kit Hygh
- Alexander Milden
- Bean

๐ŸŽฅ Video edited by Mark Bacskai: www.instagram.com/bacskaimark

๐Ÿ”– Chapters:
0:00 Intro
0:48 Error handling in Python
3:36 Understanding Rust's Error Handling Paradigm
5:40 The Power of match
7:37 Error Handling with the ? Operator
12:11 Unwrapping Errors: panic!, unwrap and expect
16:05 Final Thoughts
17:37 Outro

#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 con

ใ‚ณใƒกใƒณใƒˆ (21)
  • @Floatharr
    I recently learned Rust as a Python user, and I've been really enjoying the extra clarity the language provides, and how it allows me to make better decisions about how my program works. Very happy to see more Rust content!
  • @martimlobao
    You mentioned the returns package, do you think you could make a video on how it works?
  • @diegol_116
    I really like what Go did along the same lines of handling errors explicitly... but I must admit that Rust seems to handle it better
  • @danielmelo389
    The problem with python error handling is that it's too prone to be abused, and quite frequently it is
  • @matheusaugustodasilvasanto3171
    Rust's type system is truly a treat. I find myself trying to emulate it often in python, with result type hierarchies and frequent type checks, instead of relying on exceptions to propagate signals. However, a downside of this approach is the need to wrap almost all third-party code with type hints indicating what errors might emerge from their execution, which is not always easy, specially if the documentation is sparse. Thank God for context managers, they help a bit with that!
  • @TheEvertw
    I LOVE Rust. I also LOVE Python. And I think the two complement each other marvelously. It is very easy to write Python modules in Rust.
  • @sebastianrossetti6167
    Great video, as usual! Please do more videos on Rust. I've been trying to get into it for a bit but the syntax is a bit scary. For me, you make learning new things as easy as it can be. Thanks!
  • @itopaloglu83
    Can you please add "ignore dead code" macro in rust examples so that we can focus on the main content and not all the warnings?
  • @OkayNoway
    My colleague has been spamming me with rust propaganda, and now this. Do you have a beginners guide to rust? (from python background ๐Ÿ˜€) Also, thanks for the quality content Arjan!
  • @SiricioHernandez
    As Python developers (accustomed to unexpected errors, sometimes in production) to find Rust (a language with which you can feel confident when deploying) is like finding a piece of candy. Rust is complicated. It's not as widespread or has as many libraries as Python by far, but it's solid as a rock. I've been studying it, and my conclusion is that it's as solid as they say, but by-no-way-near has the ease of use that Python offers. Example: In Rust, Errors are types, so at the end, you need to use a "kind of Error wrapper" (like thiserror) to manage them. Solid and efficient, but complex. After fighting with Axum and some cryptic error messages (see decrusting Axum by Jon Gjengset), I've left Rust and jumped to Go to give it a try. That's because I think Go it's a good middle ground between the ease of Python and the robustness of Rust, but we'll see what happens. By now, I am getting good feelings. Sorry to say this, Arjan, but I think that, at some point, Rust builds a hard-to-tear-down-wall for Python developers.
  • @sidward
    Hello! Love the video -- I have a small suggestion if you are open to it: when explaining a new language, it takes a second for someone like to understand the difference between a compiler error and co-pilot suggestion. Possibly skill issue :) but it would be nice to just see the compiler error and not worry about the other things suggested or recommended
  • @EngineerNick
    I recently started using the anyhow crate, and it really steps up the experience to a whole new level of egonomics. Rust errors are fantastic untill you realise a function can only return a single result type. And then anyhow just makes that problem go away. Magic
  • @NicolasChanCSY
    Hi Arjan. Thanks for the new video. The terminal output are often at the bottom of the screen in this video and thus are obscured by the closed captions. Can you please scroll down a bit and make it clearer to see? There are also code unrelated to the current section of the video, e.g. 13:37. Maybe you can hide/fold them or add a bunch of empty lines to them to make them out of sight for a moment?
  • @localhost0148
    It was a really great and calm explanation. Thanks !
  • @sdpayot
    Hi Arjan, for a "best of both worlds" solution, you might want to checkout the anyhow crate. it brings a little bit of extra-convenience to Rust's error handling framework by wrapping all kinds of errors into an anyhow::Error trait.
  • @Optimusjf
    Teacher, this content is excelent.
  • @pudicio
    Errors as values is very old. I think what is great in this example is that the Rust compiler helps you not have errors that aren't dealt with. But imho, it's a tradeoff between readability/boilerplate and safety. In python I can wrap a whole group of nested function calls in a try except block and have the rest of the code be very quick to read. But it's true that the onus is on the programmer to figure out what exceptions could be raised. I therefore like the Java like syntax of function XYZ raises A,B,C My main takeaway is it would be awesome to have some kind of static checker for python that checks which exceptions are unhandled.
  • @Aceptron
    I SEE RUST, I CLICK