std-method
Iterator::inspect — observe a pipeline
Observe items passing through an adapter without transforming them. The closure runs as items are requested, so a downstream take can limit observations. Expected stdout (not executed): seen 1 seen 2 [10, 20] Common mistake: Using inspect to replace the yielded value.
Formal Syntax & Structure
rust syntaxrust
fn inspect<F>(self, f: F) -> Inspect<Self, F> where Self: Sized, F: FnMut(&Self::Item)Working Example
Example Snippetrust
fn main() { let result: Vec<_> = (1..=3).inspect(|n| println!("seen {n}")).map(|n| n * 10).take(2).collect(); println!("{result:?}");}