-
Notifications
You must be signed in to change notification settings - Fork 20
Add try_all
, try_any
, try_position
and try_rposition
methods to Iterator
trait
#361
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
A more general API would be let position: Result<usize, _> = iter.try_and(|it| it.position(|e| e == "bar")); |
Previous conversation: rust-lang/rfcs#3233 (comment) |
use itertools::Itertools; // 0.12.1
fn main() {
let v = [Ok::<u8, ()>(1), Ok(3)]
.into_iter()
.process_results(|mut i| i.all(|i| i > 2));
assert_eq!(Ok(false), v);
let v = [Ok::<u8, ()>(1), Ok(3)]
.into_iter()
.process_results(|mut i| i.position(|i| i > 2));
assert_eq!(Ok(Some(1)), v);
} |
We discussed this in the libs-api meeting and we are happy to add these. However these should be based on the Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. |
@Amanieu where I can find information about this |
Have a look at the existing |
Note that |
Proposal
Problem statement
Currently there is no simple way to use
all()
,any()
,position()
andrposition()
if the predicate can fail, returning anErr
.Motivating examples or use cases
This proposal came from an StackOverflow question, which asks for a fallible method for
position()
. The answer is rather convoluted when compared to the simplicity oftry_for_each()
versusfor_each()
.The original question is:
Solution sketch
I sketched a
FooIterator
with the aforementioned methods so I could use them right away, but I suppose they should be members ofIterator
. Also, I'm aware the implementation below is far from being standardized:Alternatives
I implemented and published the
TryIterator
crate, so I could use these methods immediately. But I believe these methods have their place in the standard library.The text was updated successfully, but these errors were encountered: