Simple String Errors
The most simplest of doing error handling in rust is by returning String
as a Box<Error>
.
As you can see by running the example (the "Play" button in upper right of the code block), this only
prints out the last Error
.
If the rust main
function returns an Err(), this Err() will be displayed with std::fmt::Debug
.
use std::error::Error; use std::result::Result; fn do_some_io() -> Result<(), Box<Error>> { Err("do_some_io error")?; Ok(()) } fn func2() -> Result<(), Box<Error>> { if let Err(_) = do_some_io() { Err("func2 error")?; } Ok(()) } fn func1() -> Result<(), Box<Error>> { if let Err(_) = func2() { Err("func1 error")?; } Ok(()) } fn main() -> Result<(), Box<Error>> { func1() }