std-method

Option::inspect

Observe a present payload without transforming it. The closure borrows the payload and the original Option is returned by value. Expected stdout (not executed): count=6 Some(6) Common mistake: Expecting inspect to change the payload type.

Formal Syntax & Structure

rust syntaxrust
fn inspect<F: FnOnce(&T)>(self, f: F) -> Option<T>

Working Example

Example Snippetrust
fn main() {    let count = Some(6).inspect(|n| println!("count={n}"));    println!("{count:?}");}
Rust Reference