Skip to content

Commit eb85a2b

Browse files
committed
regex_set: Avoid allocations in unmatched_items().
1 parent 6bb16ba commit eb85a2b

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/regex_set.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ impl RegexSet {
3232
&self.items[..]
3333
}
3434

35-
/// Returns regexes in the set which didn't match any strings yet
36-
pub fn unmatched_items(&self) -> Vec<String> {
37-
let mut items = vec![];
38-
for (i, item) in self.items.iter().enumerate() {
39-
if !self.matched[i].get() {
40-
items.push(item.clone());
35+
/// Returns an iterator over regexes in the set which didn't match any
36+
/// strings yet.
37+
pub fn unmatched_items(&self) -> impl Iterator<Item = &String> {
38+
self.items.iter().enumerate().filter_map(move |(i, item)| {
39+
if self.matched[i].get() {
40+
return None;
4141
}
42-
}
43-
items
42+
43+
Some(item)
44+
})
4445
}
4546

4647
/// Construct a RegexSet from the set of entries we've accumulated.

0 commit comments

Comments
 (0)