Lecture 2 - Unrecoverable Errors

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

  • unwrap is used to unwrap option and result types to get values back. However, an unwrap will throw a panic if it gets None back from the option type or error from the result type.
  • expect works in exactly the same way as unwrap, but we can also pass a custom panic message.
  • Both unwrap and expect are 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.

Video description

In Rust, errors can be handled through two types of errors: recoverable and unrecoverable. Unrecoverable errors occur when something goes wrong in the program that cannot be handled gracefully, and the program must terminate. In this video, we'll take a deep dive into unrecoverable errors in Rust and explore the use of the unwrap and expect macros, as well as the panic! and assert! macros for handling these types of errors. We'll cover: - What are unrecoverable errors in Rust and how they differ from recoverable errors - How to use the unwrap to unwrap an Option or Result value, and what happens if the value is None or an Err - How to use the expect to provide a custom error message when using unwrap - Different panicking macros By the end of this video, you'll have a solid understanding of unrecoverable errors in Rust and be equipped with the knowledge to handle them in your own Rust programs and spot them during auditing Watch the NEAR Rust Smart Contract Security course: https://youtube.com/playlist?list=PL7Gwuo_MOL740lhKTvouCJvk4sAyuqZqT Code and slide deck : https://github.com/timurguvenkaya/near-sc-security-course Follow me on Twitter: https://twitter.com/timurguvenkaya Lenster: https://lenster.xyz/u/timurguvenkaya Linkedin: https://www.linkedin.com/in/timur-guvenkaya/ Github: https://github.com/timurguvenkaya