std-method
Result::inspect / inspect_err
Observe success or failure for diagnostics without changing the branch. Avoid exposing sensitive error context in logs. Expected stdout (not executed): reason=offline Err("offline") Common mistake: Believing inspect_err recovers from Err.
Formal Syntax & Structure
rust syntaxrust
fn inspect<F: FnOnce(&T)>(self, f: F) -> Result<T, E>; fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Result<T, E>Working Example
Example Snippetrust
fn main() { let result: Result<i32, &str> = Err("offline"); let result = result.inspect_err(|e| println!("reason={e}")); println!("{result:?}");}