Rust: Error Handling
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
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!
Unwraps are also good to use in tests for things that should be there. If it panics, the test will fail!
this is by far the best explanation of error handling in rust ever
13:35 ... and use a really cool feature inside Swift
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
13:37 should be rust not swift - great vid. thank you
Yes. That is my mistake.
@@TheDevMethodamazing ironic timing though
wow really great explanation!
and how to raise a custom error when you are using the Box method ?
Thank you
Do you know why kind() isn’t available in anyhow? That’s useful
Does anyone know he font? Looks really slick!
I usually use Fira Mono
subscribed
What's your theme?
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.
why on earth everything becomes overly complicated when it comes to rust?
way too verbose... man, it's just error handling!
Better than try catch hell and functions unexpectedly throwing exceptions. Errors should be values and should be easy to see where they can occur
Zoom in gosh