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

Option and Result

A clear majority of error handling in Rust is done via these two generic types, or a type that looks and behaves very much like them. Operations that might fail do not throw exceptions but rather return one of these types. You may be curious as to why Rust chose this method instead of the more mainstream approach of exceptions and stack unwinding. Let's think about that for a moment.

First of all, exceptions have an overhead. In a nutshell, they can be implemented in languages in two ways:

  • Make the code that runs without errors very cheap, but error cases very expensive
  • Make error cases very cheap, but non-error cases expensive

Neither of these works well with Rust's central philosophy of zero runtime cost.

Secondly, exception-style ...

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