Auto merge of #41154 - bluss:slice-rfind, r=alexcrichton

Implement .rfind() for slice iterators Iter and IterMut

Just like the forward case find, implement rfind explicitly for slice iterators Iter and IterMut.
This commit is contained in:
bors 2017-04-09 03:30:09 +00:00
commit c2d186a3c9
3 changed files with 27 additions and 0 deletions

View File

@ -1190,6 +1190,19 @@ macro_rules! iterator {
}
}
}
fn rfind<F>(&mut self, mut predicate: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> bool,
{
self.rsearch_while(None, move |elt| {
if predicate(&elt) {
SearchWhile::Done(Some(elt))
} else {
SearchWhile::Continue
}
})
}
}
// search_while is a generalization of the internal iteration methods.

View File

@ -20,6 +20,7 @@
#![feature(fixed_size_array)]
#![feature(flt2dec)]
#![feature(fmt_internals)]
#![feature(iter_rfind)]
#![feature(libc)]
#![feature(nonzero)]
#![feature(rand)]

View File

@ -225,6 +225,19 @@ fn get_unchecked_mut_range() {
}
}
#[test]
fn test_find_rfind() {
let v = [0, 1, 2, 3, 4, 5];
let mut iter = v.iter();
let mut i = v.len();
while let Some(&elt) = iter.rfind(|_| true) {
i -= 1;
assert_eq!(elt, v[i]);
}
assert_eq!(i, 0);
assert_eq!(v.iter().rfind(|&&x| x <= 3), Some(&3));
}
#[test]
fn sort_unstable() {
let mut v = [0; 600];