-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
README std::cell::OnceLock usage example #1034
Comments
I think you mean
|
Sure, and thx, you are right - I meant OnceLock. Just in case someone would like to use the Readme example with OnceLock, posting bellow. Could you clarify what you meant by a bit lower level - it seems the usage is nearly identical? use regex::Regex;
use std::sync::OnceLock;
fn some_helper_function(haystack: &str) -> bool {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"...").unwrap())
.is_match(haystack)
}
fn main() {
assert!(some_helper_function("abc"));
assert!(!some_helper_function("ac"));
} |
Compare The
There is some more discussion about that in #709. I overall don't think it's a huge problem to leave it as-is. I'm sure it will get migrated eventually, but I prefer to move more slowly. |
Rust documentation for get_or_init says: Should this be handled in the above mentioned code ? |
By now the much better alternative is use regex::Regex;
use std::sync::LazyLock;
fn some_helper_function(haystack: &str) -> bool {
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"...").unwrap());
RE.is_match(haystack)
}
fn main() {
assert!(some_helper_function("abc"));
assert!(!some_helper_function("ac"));
} |
Rust v1.70 introduced a new std::cell::OnceCell primitive that should (in theory) replace the commonly used once_cell crate.
Regex README documents how to use the once_cell crate. Should it be updated with the now standardized OnceCell?
The text was updated successfully, but these errors were encountered: