Get the latest tech news

Rust error handling


The night is dark and full of errors, so how should we handle these gracefully and safely in our Rust programs? Let’s introduce two of every Rust programmer’s favourite types: Option and Result.

So in this example the print_sqrt function either returns Ok(()), meaning “everything went fine”, or, implicitly, some string indicating an error (“can’t take square root of negative number”). Some languages let you ignore possible errors altogether, automatically propagating them as exceptions, and crashing the program if they’re not handled somewhere. The fact that Rust can catch issues of forgetfulness like this for us is helpful, and the Option and Result types are a very appealing feature of the language.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Rust error handling

Rust error handling