Skip to content

Commit 1e067d4

Browse files
committed
Suggest std::sync::LazyLock instead of once_cell::sync::Lazy
1 parent 9870c06 commit 1e067d4

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ version = "0.8.5"
187187
default-features = false
188188

189189
[dev-dependencies]
190-
# For examples.
191-
once_cell = "1.17.1"
192190
# For property based tests.
193191
quickcheck = { version = "1.0.3", default-features = false }
194192
# To check README's example

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,24 @@ compilation itself expensive, but this also prevents optimizations that reuse
8080
allocations internally to the matching engines.
8181

8282
In Rust, it can sometimes be a pain to pass regular expressions around if
83-
they're used from inside a helper function. Instead, we recommend using the
84-
[`once_cell`](https://crates.io/crates/once_cell) crate to ensure that
85-
regular expressions are compiled exactly once. For example:
83+
they're used from inside a helper function. Instead, we recommend using
84+
[`std::sync::LazyLock`] stabilized in [Rust 1.80], the [`once_cell`] crate, or
85+
the [`lazy_static`] crate to ensure that regular expressions are compiled
86+
exactly once. For example:
87+
88+
[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
89+
[Rust 1.80]: https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html
90+
[`once_cell`]: https://crates.io/crates/once_cell
91+
[`lazy_static`]: https://crates.io/crates/lazy_static
8692

8793
```rust
8894
use {
89-
once_cell::sync::Lazy,
95+
std::sync::LazyLock,
9096
regex::Regex,
9197
};
9298

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

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,23 +471,27 @@ to a few **milliseconds** depending on the size of the pattern.) Not only is
471471
compilation itself expensive, but this also prevents optimizations that reuse
472472
allocations internally to the regex engine.
473473
474-
In Rust, it can sometimes be a pain to pass regexes around if they're used from
475-
inside a helper function. Instead, we recommend using crates like [`once_cell`]
476-
and [`lazy_static`] to ensure that patterns are compiled exactly once.
477-
474+
In Rust, it can sometimes be a pain to pass regular expressions around if
475+
they're used from inside a helper function. Instead, we recommend using
476+
[`std::sync::LazyLock`] stabilized in [Rust 1.80], the [`once_cell`] crate, or
477+
the [`lazy_static`] crate to ensure that regular expressions are compiled
478+
exactly once. For example:
479+
480+
[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
481+
[Rust 1.80]: https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html
478482
[`once_cell`]: https://crates.io/crates/once_cell
479483
[`lazy_static`]: https://crates.io/crates/lazy_static
480484
481485
This example shows how to use `once_cell`:
482486
483487
```rust
484488
use {
485-
once_cell::sync::Lazy,
489+
std::sync::LazyLock,
486490
regex::Regex,
487491
};
488492
489493
fn some_helper_function(haystack: &str) -> bool {
490-
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"...").unwrap());
494+
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"...").unwrap());
491495
RE.is_match(haystack)
492496
}
493497

0 commit comments

Comments
 (0)