Skip to content

Update README.md to use LazyLock instead of OnceCell #1254

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ compilation itself expensive, but this also prevents optimizations that reuse
allocations internally to the matching engines.

In Rust, it can sometimes be a pain to pass regular expressions around if
they're used from inside a helper function. Instead, we recommend using the
[`once_cell`](https://crates.io/crates/once_cell) crate to ensure that
regular expressions are compiled exactly once. For example:
they're used from inside a helper function. Instead, we recommend using
[`LazyLock`](https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html)
to ensure that regular expressions are compiled exactly once. For example:

```rust
use {
once_cell::sync::Lazy,
std::sync::LazyLock,
regex::Regex,
};

fn some_helper_function(haystack: &str) -> bool {
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"...").unwrap());
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"...").unwrap());
RE.is_match(haystack)
}

Expand Down