diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index cc56bbf4890..c7efd4ab2b1 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -767,6 +767,7 @@ fn test_iterator() { pos += 1; } assert_eq!(pos, v.len()); + assert_eq!(s.chars().count(), v.len()); } #[test] diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 196750254af..7f91da53142 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -424,6 +424,17 @@ impl<'a> Iterator for Chars<'a> { }) } + #[inline] + fn count(self) -> usize { + // length in `char` is equal to the number of non-continuation bytes + let bytes_len = self.iter.len(); + let mut cont_bytes = 0; + for &byte in self.iter { + cont_bytes += utf8_is_cont_byte(byte) as usize; + } + bytes_len - cont_bytes + } + #[inline] fn size_hint(&self) -> (usize, Option) { let len = self.iter.len(); @@ -501,6 +512,11 @@ impl<'a> Iterator for CharIndices<'a> { } } + #[inline] + fn count(self) -> usize { + self.iter.count() + } + #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint()