]> git.lizzy.rs Git - rust.git/commitdiff
Use iter::from_fn in String::remove_matches
authorTamir Duberstein <tamird@google.com>
Fri, 26 Mar 2021 14:38:32 +0000 (10:38 -0400)
committerTamir Duberstein <tamird@google.com>
Sun, 6 Jun 2021 12:06:03 +0000 (08:06 -0400)
library/alloc/src/string.rs

index dbe5bc1da460d7c42521ec175d5a8c13f539cde9..4aa2be2b1787cd6a36740ac5d9e9200e3c2097dc 100644 (file)
@@ -48,7 +48,7 @@
 use core::hash;
 #[cfg(not(no_global_oom_handling))]
 use core::iter::FromIterator;
-use core::iter::FusedIterator;
+use core::iter::{from_fn, FusedIterator};
 #[cfg(not(no_global_oom_handling))]
 use core::ops::Add;
 #[cfg(not(no_global_oom_handling))]
@@ -1290,15 +1290,9 @@ pub fn remove_matches<'a, P>(&'a mut self, pat: P)
     {
         use core::str::pattern::Searcher;
 
-        let matches = {
+        let matches: Vec<_> = {
             let mut searcher = pat.into_searcher(self);
-            let mut matches = Vec::new();
-
-            while let Some(m) = searcher.next_match() {
-                matches.push(m);
-            }
-
-            matches
+            from_fn(|| searcher.next_match()).collect()
         };
 
         let len = self.len();