-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Document additional use case for Iterator::inspect #49564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Well, another possibility (YMMV) would be to combine the inspect+flat_map into a filter_map: lines
.iter()
.map(|line| parse_line(line))
.filter_map(|line| match line {
Ok(x) => Some(x),
Err(e) => { error!("Parsing error: {}", e); None },
})
// etc FWIW it took me a second to remember that flat_map over a result does what it does, so I think I prefer the explicitness of the filter_map overall. |
Yeah, you can use lines
.iter()
.map(|line| parse_line(line))
.inspect(|line| {
if let Err(ref e) = *line {
error!("Parsing error: {}", e);
}
})
.filter_map(Result::ok) Sorry, I wasn't trying to draw attention to flat_map 😅 |
I'm happy to mentor anyone who'd like to fix this bug! Here's how: The documentation for this method lives here: rust/src/libcore/iter/iterator.rs Lines 1174 to 1211 in 5092c6b
Add the final example by @nickbabcock above below all of the other ones. I'm happy to answer any more questions! |
I'll take it. I didn't know if there needed to be more discussion about the proposed use case. If I don't figure things out, I'll come back here with question 😉 |
Document additional use case for iter::inspect Adds docs for `iter::inspect` showing the non-debug use case Closes #49564
Uh oh!
There was an error while loading. Please reload this page.
If you'd like to fix this bug, here's how.
Currently iterator::inspect contains the following documentation:
Though I like the never say never wording, I think it might be helpful to document a use case where it's not used for debugging. I use
inspect
in daemon applications for that slurp in stdin lines and discard and log the lines that can't be parsed. Like so:Maybe the documentation of inspect can be updated to something like:
What do you think? Suggestions certainly wanted 😄
The text was updated successfully, but these errors were encountered: