Rust: Error Handling

Sdílet
Vložit
  • čas přidán 13. 06. 2022
  • #rust #errors #panic
    There are two groups of errors: recoverable and unrecoverable. Errors are a part of programming and every language handles it differently.
    Recoverable errors are the ones you can catch while the program’s running and have the program decide what to do next depending on the kind of error it is in the current context. For example, maybe you want to read a file but the file cannot be found. Maybe you need to tell the user or the caller of your function that this error occurred because of this reason.
    Unrecoverable errors are ones where the program cannot continue run. Like, reaching beyond the bounds of an array.
    Rust has no exceptions. Instead, we use the Result type for recoverable errors and panic! macro for unrecoverable errors.
    Links
    Chapter 9 of the Rust Programming Book: Error Handling
    doc.rust-lang.org/book/ch09-0...
  • Věda a technologie

Komentáře • 20

  • @UliTroyo
    @UliTroyo Před rokem +8

    You can tell a great explanation from a middling one when at the end you go "wait, THIS is what I wasn't getting?" instead of "oh, I think I understand now". Really well-thought-out demo; thanks!

  • @josephlyons3393
    @josephlyons3393 Před rokem +10

    Unwraps are also good to use in tests for things that should be there. If it panics, the test will fail!

  • @OuranianCyclops
    @OuranianCyclops Před 8 měsíci +2

    this is by far the best explanation of error handling in rust ever

  • @antonaparin
    @antonaparin Před rokem +1

    13:35 ... and use a really cool feature inside Swift

  • @anderslundstrom4388
    @anderslundstrom4388 Před rokem +1

    Just a thought, if u wanted to just read from a file (which is what File::open is used for if i understand correctly), then maby often its not required to actually create one, and if you wanted to write/append to a file, then OpenOptions has .create().append(true).open() unless you want to handle the special case when file does not exist. if so, OpenOption alternative seem to have less nesting, but perhaps this alternative opens up for more kinds of error handling and less convention. But im just beginner so i dont know whats best practice

  • @zeroows
    @zeroows Před rokem +6

    13:37 should be rust not swift - great vid. thank you

  • @FugalBaboon
    @FugalBaboon Před rokem +1

    wow really great explanation!

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

    and how to raise a custom error when you are using the Box method ?

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

    Thank you

  • @fabienpenso
    @fabienpenso Před 2 lety

    Do you know why kind() isn’t available in anyhow? That’s useful

  • @PimpofChaos
    @PimpofChaos Před rokem +3

    Does anyone know he font? Looks really slick!

  • @apidas
    @apidas Před rokem +2

    subscribed

  • @alexandredaubricourt5741

    What's your theme?

  • @jeffg4686
    @jeffg4686 Před rokem

    We need method aliasing so that we can rename unwrap() on Option and Result to match()...
    I can almost guarantee that's where 1/2 fail. Mind is trying to figure out what's being "unwrapped" (there's nothing to unwrap...).
    Or maybe name it "assert_get_val" for Option and "assert_get_result" for Result.
    Nah - that's too crazy. maybe just "match".
    Now that we have ?, it doesn't matter much.

  • @hiongun
    @hiongun Před rokem

    why on earth everything becomes overly complicated when it comes to rust?
    way too verbose... man, it's just error handling!

    • @AJ213Probably
      @AJ213Probably Před rokem +2

      Better than try catch hell and functions unexpectedly throwing exceptions. Errors should be values and should be easy to see where they can occur

  • @mrlectus
    @mrlectus Před rokem

    Zoom in gosh