Expand description
Interfaces for working with Errors.
§Error Handling In Rust
The Rust language provides two complementary systems for constructing /
representing, reporting, propagating, reacting to, and discarding errors.
These responsibilities are collectively known as “error handling.” The
components of the first system, the panic runtime and interfaces, are most
commonly used to represent bugs that have been detected in your program. The
components of the second system, Result
, the error traits, and user
defined types, are used to represent anticipated runtime failure modes of
your program.
§The Panic Interfaces
The following are the primary interfaces of the panic system and the responsibilities they cover:
panic!
andpanic_any
(Constructing, Propagated automatically)set_hook
,take_hook
, andPanicHookInfo
(Reporting)#[panic_handler]
andPanicInfo
(Reporting in no_std)catch_unwind
andresume_unwind
(Discarding, Propagating)
The following are the primary interfaces of the error system and the responsibilities they cover:
Result
(Propagating, Reacting)- The
Error
trait (Reporting) - User defined types (Constructing / Representing)
match
anddowncast
(Reacting)- The question mark operator (
?
) (Propagating) - The partially stable
Try
traits (Propagating, Constructing) Termination
(Reporting)
§Converting Errors into Panics
The panic and error systems are not entirely distinct. Often times errors
that are anticipated runtime failures in an API might instead represent bugs
to a caller. For these situations the standard library provides APIs for
constructing panics with an Error
as its source.
These functions are equivalent, they either return the inner value if the
Result
is Ok
or panic if the Result
is Err
printing the inner error
as the source. The only difference between them is that with expect
you
provide a panic error message to be printed alongside the source, whereas
unwrap
has a default mess