Skip to content

Commit 8d41097

Browse files
Rollup merge of rust-lang#81409 - gilescope:chars_count, r=joshtriplett
Slight simplification of chars().count() Slight simplification: No need to call len(), we can just count the number of non continuation bytes. I can't see any reason not to do this, can you?
2 parents 69dd59f + a623ea5 commit 8d41097

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

library/core/src/str/iter.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ impl<'a> Iterator for Chars<'a> {
4747
#[inline]
4848
fn count(self) -> usize {
4949
// length in `char` is equal to the number of non-continuation bytes
50-
let bytes_len = self.iter.len();
51-
let mut cont_bytes = 0;
52-
for &byte in self.iter {
53-
cont_bytes += utf8_is_cont_byte(byte) as usize;
54-
}
55-
bytes_len - cont_bytes
50+
self.iter.filter(|&&byte| !utf8_is_cont_byte(byte)).count()
5651
}
5752

5853
#[inline]

0 commit comments

Comments
 (0)