Welcome to unit 3
on error handling.
Errors will happen no matter
how hard we try.
Errors include programming bugs
such as trying to access an
element beyond the end of a vector;
interactions
with systems outside of our control
that don't go as planned,
such as a network request timing out;
or unexpected inputs, such
as a user of our program entering
data that wasn't planned for.
Forgetting to handle these cases can
lead to frustrating user experiences,
unintended consequences,
and security vulnerabilities.
Rust is designed to help you remember
that errors are a possibility
and to handle them appropriately.
The type system indicates when an operation
might fail
and the compiler won't let you ignore a possible
failure.
Handling errors appropriately might
include stopping the program immediately,
trying a different operation to recover,
using default values,
returning an error for some other programmer
to handle,
or giving a nice error message to the user
and allowing them to try again.
Rust's error handling philosophy
might feel different than in other languages,
especially when you first start a prototype
and you're mostly concerned
with getting the happy path to work.
In the long term,
and for larger programs, Rust's
strategy for handling errors goes
a long way towards making more reliable
programs.
In this unit, we'll get you comfortable
with the idiomatic ways to deal
with errors in Rust.
Module 2
is about the panic! macro,
which stops programs from continuing
when they're in invalid states that
indicate a programming mistake has been made.
In module 3,
we'll talk about the Standard Library types
Result and Option
and what they indicate when a function returns
them.
Module 4 covers how to write
a function that returns a Result,
and propagate any errors that happen in the function
to the caller
using the ? operator.
After covering how to handle errors
in Rust, module 5
discusses why Rust's strategy is
helpful.
In module 6, we'll show how
you can make your own custom error types.
Module 7 will go over how some
crates can make working
with errors easier.
And, finally, module 8 demonstrates
a bunch of useful methods on the Result
and Option types. Let's dive in!