regex!() { /* proc-macro */ }
Expand description
A simple regular expression macro.
This macro will validate the regular expression and will produce a compile time error if the expression
is invalid. This can allow a developer to know that the hard coded regular expressions are valid. The result will be a valid [regex::Regex
].
For example:
use rust_regex_dsl::regex;
use regex::Regex;
use std::sync::LazyLock;
static VALID_NAME: LazyLock<Regex> = LazyLock::new(|| regex!("[a-z][a-zA-Z_]*"));
Is equivalent to:
use rust_regex_dsl::regex;
use regex::Regex;
use std::sync::LazyLock;
static VALID_NAME: LazyLock<Regex> = LazyLock::new(|| Regex::new("[a-z][a-zA-Z_]*").unwrap());
But this:
ⓘ
use rust_regex_dsl::regex;
use regex::Regex;
static VALID_NAME: LazyLock<Regex> = LazyLock::new(|| regex!("[a-z][a-zA-Z_*"));
will fail with compilation error (because of the missing closing square bracket) while using the same without the macro will fail in run time.