Skip to content
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

Suggest std::sync::LazyLock instead of once_cell::sync::Lazy #1235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ version = "0.8.5"
default-features = false

[dev-dependencies]
# For examples.
once_cell = "1.17.1"
# For property based tests.
quickcheck = { version = "1.0.3", default-features = false }
# To check README's example
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,24 @@ 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
[`std::sync::LazyLock`] stabilized in [Rust 1.80], the [`once_cell`] crate, or
the [`lazy_static`] crate to ensure that regular expressions are compiled
exactly once. For example:

[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
[Rust 1.80]: https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html
[`once_cell`]: https://crates.io/crates/once_cell
[`lazy_static`]: https://crates.io/crates/lazy_static

```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
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,27 @@ to a few **milliseconds** depending on the size of the pattern.) Not only is
compilation itself expensive, but this also prevents optimizations that reuse
allocations internally to the regex engine.

In Rust, it can sometimes be a pain to pass regexes around if they're used from
inside a helper function. Instead, we recommend using crates like [`once_cell`]
and [`lazy_static`] to ensure that patterns are compiled exactly once.

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
[`std::sync::LazyLock`] stabilized in [Rust 1.80], the [`once_cell`] crate, or
the [`lazy_static`] crate to ensure that regular expressions are compiled
exactly once. For example:

[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
[Rust 1.80]: https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html
[`once_cell`]: https://crates.io/crates/once_cell
[`lazy_static`]: https://crates.io/crates/lazy_static

This example shows how to use `once_cell`:

```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