I am having trouble using a regular expression in nom's (6.0) re_match parser. This works fine:
fn ident_parser(input: &str) -> IResult<&str, bool> {
let re: Regex = Regex::new("[A-Za-z0-9_:.-]+").unwrap();
match tuple((space0, re_match(re))) {
. . .
When I try to move the regular expression into a lazy_static! like so:
lazy_static! {
static ref RE: Regex = Regex::new("[A-Za-z0-9_:.-]+").unwrap();
}
I get an error that implies that it's not actually being seen as a Regex type. Or, if I try to dereference it, I can't seem to get the correct combination of dereference operators.
re_match(RE)
^^ expected struct `regex::Regex`, found struct `parsers::variables::RE`
I saw a reference in an article that said something like "lazy_static actually lies about its type" and recommended something like &*RE
but I haven't found a combination that works. Either the error refers to it by the module name or complains that it doesn't implement Copy
.
re_match(*RE)
^^^ move occurs because value has type `regex::Regex`, which does not implement the `Copy` trait
re_match(&*RE)
^^^^
|
expected struct `regex::Regex`, found `®ex::Regex`
help: consider removing the borrow: `*RE`
One slightly related question... if I'm not in a loop and my input to the Regex::new
is a static string is do I really need to put it in a lazy_static!
block anyway? Or will the compiler recognize that it is fixed and do the optimizations for me?