diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index a13e1fb33d1..ebbfbefc21b 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -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"; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 1dfd00e11aa..1bd6188f487 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -511,6 +511,12 @@ impl<'a> Iterator for CharIndices<'a> { fn size_hint(&self) -> (usize, Option) { 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")]