Inspect Peeks Inside Iterator Pipelines
Watch values flow through an iterator pipeline with inspect, logging each stage without altering the stream that map and filter_map continue to consume.
Code
fn main() {
let orders = ["tea:2", "coffee:1", "juice:0", "mocha:4"];
// inspect:管道中途偷看每个元素,值原样流向下游
let total: u32 = orders
.iter()
.inspect(|order| println!("recv {order}"))
.filter_map(|order| {
let (name, count) = order.split_once(':')?;
let count: u32 = count.parse().ok()?;
(count > 0).then_some((name, count))
})
.inspect(|(name, count)| println!("valid {name} x{count}"))
.map(|(_, count)| count)
.sum();
println!("total cups: {total}");
}How the snippet works
Inspect Peeks Inside Iterator Pipelines keeps everything in the function main.
Applying it
The demo output is labeled (recv, valid, total cups:), so every printed line maps back to its originating operation.
Statements that matter
let (name, count) = order.split_once(':')?;
let count: u32 = count.parse().ok()?;
.map(|(_, count)| count)
.sum();
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.HAR Waterfall AnalyzerInspect HTTP Archive (HAR 1.2) JSON files locally to summarize total requests, status codes, MIME types, slowest calls, and timing waterfalls.
FAQ
- What does Inspect Peeks Inside Iterator Pipelines print when run?
- No setup needed: run the file and look for labeled lines like `recv`, `valid`, `total cups:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.