O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5 - Error Handling

Rust's error handling primarily rests on two enums: Option and Result. Nearly all library code that could fail returns values wrapped inside one of those two or a variation of them.

Option replaces the Null type of other languages; it models things that are either something (Some(N)) or may be nothing (None). For instance, a query against a database might return an Option, since the result set of the query might be empty. Neither of these actions need to be considered an error.

The Result type models operations that may succeed (Ok(T)) or may fail (Err(E)). In case of an error, some form of reporting of the error is returned wrapped inside the Err.

To make operating with these types a bit easier, there are helpful ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required