Dealing with errors
Error handling deals with the question: how do we communicate program errors to users?
In our project, errors can occur due to two main reasons—there could be a programming error, or an error could occur due to invalid inputs. Let's first discuss the Rust approach to error handling.
In Rust, errors are first-class citizens in that an error is a data type in itself, just like an integer, string, or vector. Because error is a data type, type checking can happen at compile time. The Rust standard library has a std::error::Error trait implemented by all errors in the Rust standard library. Rust does not use exception handling, but a unique approach where a computation can return a Result type:
enum Result<T, E> { Ok(T), Err(E),}
Result<T, E> is an enum with two variants, where Ok(T) represents success and Err(E) represents the error returned. Pattern matching is used to handle the two types of return...