If I understand right, then lazy_static is considered outdated, right? It has first been superseeded by once_cell and then found it's way into std::sync::OnceLock with Rust version 1.70, which was released on June 1st, 2023. This is also explained in the once_cell F.A.Q.:
Should I use std::cell::OnceCell, once_cell, or lazy_static?
If you can use std version (your MSRV is at least 1.70, and you don’t need extra features once_cell provides), use std. Otherwise, use once_cell. Don’t use lazy_static.
It explicitly says, "Don't use lazy_static". So I wonder if the idiomatic way to use regular expressions would then be as follows:
use regex::Regex;
use std::sync::OnceLock;
fn extract_login(input: &str) -> Option<&str> {
static RE: OnceLock<Regex> = OnceLock::new();
let re = RE.get_or_init(|| Regex::new(r"(?x)
^(?P<login>[^@\s]+)@
([[:word:]]+\.)*
[[:word:]]+$
").unwrap()
);
re.captures(input).and_then(|cap| {
cap.name("login").map(|login| login.as_str())
})
}
Yeah, LazyLock is great, and I've already created a lazy_static! macro for it for convenience in my crates, and old code doesn't need an update (and only removed lazy_static dep).
I never really understood why people want a macro for lazy_static; the static ref hiding the type is really not helpful, only confusing, and creating a closure shouldn't be considered so hard/long/noisy as to be undesirable (or even worth creating a macro for).
I recently came across the crate lazy_regex - Rust which seems very nice. It offers a macro for creating a static lazy regex conveniently, and even reports syntax errors (in the regex) at compile-time (all using the ordinary regex crate as a dependency).