Skip to content

Commit

Permalink
Optimise CharIndices::last()
Browse files Browse the repository at this point in the history
The default implementation of last() goes through the entire iterator
but that's not needed here.
  • Loading branch information
ollie27 committed Nov 20, 2016
1 parent de2f617 commit 9e86e18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libcollectionstest/str.rs
Expand Up @@ -919,6 +919,14 @@ fn test_char_indices_revator() {
assert_eq!(pos, p.len());
}

#[test]
fn test_char_indices_last() {
let s = "ศไทย中华Việt Nam";
let mut it = s.char_indices();
it.next();
assert_eq!(it.last(), Some((27, 'm')));
}

#[test]
fn test_splitn_char_iterator() {
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
Expand Down
6 changes: 6 additions & 0 deletions src/libcore/str/mod.rs
Expand Up @@ -511,6 +511,12 @@ impl<'a> Iterator for CharIndices<'a> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

#[inline]
fn last(mut self) -> Option<(usize, char)> {
// No need to go through the entire string.
self.next_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit 9e86e18

Please sign in to comment.