about summary refs log tree commit diff
path: root/library
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2021-01-30 13:36:48 +0900
committerGitHub <noreply@github.com>2021-01-30 13:36:48 +0900
commitc26dd4d4140202283221c05758b1b1531b2fbb96 (patch)
treec4a8d8ef3ee93f46e038f003775cd7da268d098d /library
parent7fa991fb8545d8b03f0ab953770cb4fa08921abc (diff)
parenta623ea5301737507a8229c2ddae74b20f727bd1b (diff)
downloadrust-c26dd4d4140202283221c05758b1b1531b2fbb96.tar.gz
rust-c26dd4d4140202283221c05758b1b1531b2fbb96.zip
Rollup merge of #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?
Diffstat (limited to 'library')
-rw-r--r--library/core/src/str/iter.rs7
1 files changed, 1 insertions, 6 deletions
diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs
index 8b952eab294..83f484dc570 100644
--- a/library/core/src/str/iter.rs
+++ b/library/core/src/str/iter.rs
@@ -47,12 +47,7 @@ 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
+        self.iter.filter(|&&byte| !utf8_is_cont_byte(byte)).count()
     }
 
     #[inline]