Handling Errors

In this chapter, we will cover the following recipes:

  • Providing user-defined error types
  • Providing logging
  • Creating a custom logger
  • Implementing the Drop trait
  • Understanding RAII

Introduction

Mistakes happen, and that's okay. We are only human after all. The important thing in life and in programming is not which errors we make, but how we deal with them. Rust helps us with the programming aspect of this principle by providing us with an error handling concept that guarantees that we have to think about the consequences of failure when dealing with functions that can fail, as they don't return a value directly but wrapped in a Result that has to be opened somehow. The only thing left is designing our code in a way that integrates ...

Get Rust Standard Library Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.