diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 8b952eab294..c3784343478 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -47,12 +47,13 @@ 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; + let mut char_count = 0; for &byte in self.iter { - cont_bytes += utf8_is_cont_byte(byte) as usize; + if !utf8_is_cont_byte(byte) { + char_count += 1; + } } - bytes_len - cont_bytes + char_count } #[inline]