about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGiles Cope <gilescope@gmail.com>2021-01-26 11:14:57 +0000
committerGiles Cope <gilescope@gmail.com>2021-01-26 11:14:57 +0000
commit328abfb9431c486ab2d0b9ebd7f6115805f613de (patch)
tree47ee65701bc9957d23afaea520d9b47c7902104f
parent9a9477fada5baf69d693e717d6df902e411a73d6 (diff)
downloadrust-328abfb9431c486ab2d0b9ebd7f6115805f613de.tar.gz
rust-328abfb9431c486ab2d0b9ebd7f6115805f613de.zip
Slight simplification of chars().count()
-rw-r--r--library/core/src/str/iter.rs9
1 files changed, 5 insertions, 4 deletions
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]