4 levels of Rust error handling

Sdílet
Vložit
  • čas přidán 17. 05. 2024
  • Strengthen your code one level at a time. Rust's errors can be confusing. Learn how to start with an easy path and grow as you knowledge increases. This video was produced first for patrons / timclicks
    📚 Resources:
    // Docs
    - Error Handling in Rust - A Deep Dive www.lpalmieri.com/posts/error...
    - Error Handling in Rust with Jane Lasore-Lusby rustacean-station.org/episode...
    - Error Handling in Rust (old! this one is how I learned!) blog.burntsushi.net/rust-erro...
    // Slides
    docs.google.com/presentation/...
    // Crates
    - crates.io/crates/thiserror
    - crates.io/crates/eyre
    - crates.io/crates/anyhow
    🦀 Rust resources:
    - Rust Documentation: doc.rust-lang.org/book/
    - Rust Playground: play.rust-lang.org/
    - Rust in Action (Tim's book!) mng.bz/4MlD
    - How to Learn Rust (online course!) learning.accelerant.dev/how-t...
    👋 Connect with Tim:
    - Twitter: / timclicks
    - GitHub: github.com/timClicks
    - Mastodon: mastodon.nz/@timClicks
    - DEV: dev.to/timclicks/
    - Patreon (extra learning materials) / timclicks
    Timeline
    00:00:00 Introduction
    00:01:04 Personal story about learning Rust error handling
    00:04:39 Combing errors
    00:05:18 Using std::error:Error trait objects
    00:08:20 Programmer's view of an error
    00:09:40 Type system's view of an error
    00:11:32 What is a Result?
    00:14:12 Individual modules/crates often define their own Error types
    00:16:15 Level 1: Ignore Results
    00:17:10 Using unwrap()
    00:18:35 Using expect()
    00:19:50 Assigning to underscore (_)
    00:24:18 Level 2: Return strings as errors
    00:25:15 Converting errors with map_err
    00:26:40 Defining a function with String as an error type
    00:31:34 Level 3: use enums as errors
    00:37:20 Why bother to define a custom error type
    00:41:50 Level 4: use crates to help
    🔔 Subscribe to the channel and click the bell icon to stay updated with the latest videos and live streams from timClicks: czcams.com/users/timClicks?sub...
    👍 Like this video if you found it helpful, and share it with your friends who are also interested in Rust programming.
  • Věda a technologie

Komentáře • 13

  • @JorgetePanete
    @JorgetePanete Před 9 měsíci +17

    Some video editing to shorten the video would be very useful

  • @ekarademir
    @ekarademir Před 10 měsíci +4

    Thank you for this video.
    I just wanted to add, another advantage of using a crate level Error enum is that if you are emitting the errors in different ways, like printing out, or returning as a server response, or pushing into a central system, your extra work would be just implementing conversions to that enum.

    • @timClicks
      @timClicks  Před 10 měsíci +3

      Excellent point. Thank you for adding your thoughts.

  • @agustindiaz3361
    @agustindiaz3361 Před 4 měsíci

    This is awesome! This kind of high quality, advanced topics in rust is really absent in youtube. Thanks for sharing it :D

  • @bakhtiyargarashov805
    @bakhtiyargarashov805 Před 10 měsíci

    Going through the Rust learning journey with you is amazing. Thanks for that.

  • @pyprem
    @pyprem Před 5 měsíci +1

    There is some confusion at 25:17. However, to make this idea work you can do it like this: `.map_err(|_err| "could not create file")?;`, i.e. `map_err` returns the value contained in the Err variant, not the whole variant itself.

    • @nubunto
      @nubunto Před 2 měsíci

      came here to say this. there's a confusion between the Result type and the Err variant, which is common!

  • @nscheer
    @nscheer Před 5 měsíci

    Really enjoyed your content!
    One thing: The video would be way nicer to watch without all the „ehem“ and „aahmm“ - it is worth it to train and let there be just some silence between the words 😊

    • @timClicks
      @timClicks  Před 5 měsíci +1

      Thanks for the advice. You'll find that more recent videos of mine are much more polished

  • @pmcgee003
    @pmcgee003 Před 5 měsíci

    unwrap .. because Result should be understood as a functor / monad

  • @mustafazakiassagaf1757
    @mustafazakiassagaf1757 Před 10 měsíci +1

    hey it would be nice if you provide timestamps for longer video like this. other than that this is a good video with good story telling 💪

  • @user-kr9on5rw6q
    @user-kr9on5rw6q Před 10 měsíci

    Привет я на этом канале сново увидел шедевр это видео прекрасно как и остальные