迭代原因
它通常用於除錯目的,以找出錯誤的根本原因。為了檢查實現 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();
}