RS.CLIPPY.REGEX_CREATION_IN_LOOPS
Regular expression compilation performed in a loop
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: regex_creation_in_loops. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for regex compilation inside a loop with a literal.
Why is this bad?
Compiling a regex is a much more expensive operation than using one, and a compiled regex can be used multiple times. This is documented as an antipattern on the regex documentation
Example
for haystack in haystacks {
let regex = regex::Regex::new(MY_REGEX).unwrap();
if regex.is_match(haystack) {
// Perform operation
}
}
can be replaced with
let regex = regex::Regex::new(MY_REGEX).unwrap();
for haystack in haystacks {
if regex.is_match(haystack) {
// Perform operation
}
}