Rust Error handling Iterating through causes


It is often useful for debugging purposes to find the root cause of an error. In order to examine an error value that implements std::error::Error:

use std::error::Error;

let orig_error = call_returning_error();

// Use an Option<&Error>. This is the return type of Error.cause().
let mut err = Some(&orig_error as &Error);

// Print each error's cause until the cause is None.
while let Some(e) = err {
    println!("{}", e);
    err = e.cause();