Lecture 2 - Unrecoverable Errors
Unwrap, Expect and Panicking Macros
In this video, the presenter discusses how to use unwrap and expect to get values back from option and result types. They also cover panicking macros such as panic, unreachable, unimplemented, todo, assert, assert equal and assert not equal.
Using Unwrap and Expect
unwrapis used to unwrap option and result types to get values back. However, anunwrapwill throw a panic if it gets None back from the option type or error from the result type.
expectworks in exactly the same way asunwrap, but we can also pass a custom panic message.
- Both
unwrapandexpectare great when used in tests or when we are confident that we won't get None or Error back.
Panicking Macros
- Panicking macros are macros which throw panic. There are several of them such as panic, unreachable, unimplemented, todo, assert, assert equal, assert not equal and their debug variants.
- We should always be aware which macros cause panic to either avoid them altogether in production or implement some safeguards so that they won't cause severe issues.
- Leaving todo macro in production code might lead to some malicious actors abusing it if they find the path to the place where todo macro is used.
- The presenter provides an example where different panicking macros are reached depending on user input.
Code Examples
- The presenter provides a simple example of using unwrap and expect with file open.
- The presenter provides an example of using different panicking macros depending on user input.
Overall, the video provides a good overview of how to use unwrap and expect with option and result types, as well as an introduction to panicking macros. The code examples provided help illustrate these concepts in practice.