minimal (mostly) example: https://play.rust-lang.org/?gist=d5a5d6328923271b1ac4a324d6249813&version=stable&mode=debug&edition=2015
I have a regex I need to use in a loop (three lists of 200+ entries each), and that I may want to use in other functions all collected into a single mod.rs file. According to the Regex package it’s bad to redefine a regex for every instance in a loop. How should I be resolving this? Should I be creating it in a parent function then passing it as a parameter? Is there some equivalent of CONST
for these sorts of “static” types? Should I retool my code to do the entire loop within a single function?
It is an anti-pattern to compile the same regular expression in a loop since compilation is typically expensive. (It takes anywhere from a few microseconds to a few milliseconds depending on the size of the regex.) Not only is compilation itself expensive, but this also prevents optimizations that reuse allocations internally to the matching engines.