Skip to content

Commit b80e391

Browse files
noritadaBurntSushi
authored andcommitted
doc: suggest std::sync::LazyLock instead of once_cell::sync::Lazy
This seems fine now given that `LazyLock` has been stabilized for quite some time. Closes #1217, PR #1235
1 parent 71233b5 commit b80e391

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
1.11.2 (2025-08-24)
2+
===================
3+
This is a new patch release of `regex` with some minor fixes.
4+
5+
Improvements:
6+
7+
* [BUG #1217](https://github.com/rust-lang/regex/issues/1217):
8+
Switch recommendation from `once_cell` to `std::sync::LazyLock`.
9+
10+
Bug fixes:
11+
12+
* [BUG #1281](https://github.com/rust-lang/regex/pull/1281):
13+
Remove `fuzz/` and `record/` directories from published crate on crates.io.
14+
15+
116
1.11.1 (2024-10-24)
217
===================
318
This is a new patch release of `regex` that fixes compilation on nightly

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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ 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`], or the [`once_cell`] crate,
85+
if you can't use the standard library.
86+
87+
This example shows how to use `std::sync::LazyLock`:
8688

8789
```rust
88-
use {
89-
once_cell::sync::Lazy,
90-
regex::Regex,
91-
};
90+
use std::sync::LazyLock;
91+
92+
use regex::Regex;
9293

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

@@ -104,6 +105,9 @@ fn main() {
104105
Specifically, in this example, the regex will be compiled when it is used for
105106
the first time. On subsequent uses, it will reuse the previous compilation.
106107

108+
[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
109+
[`once_cell`]: https://crates.io/crates/once_cell
110+
107111
### Usage: match regular expressions on `&[u8]`
108112

109113
The main API of this crate (`regex::Regex`) requires the caller to pass a

src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -471,23 +471,20 @@ 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.
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`], or the [`once_cell`] crate,
477+
if you can't use the standard library.
477478
478-
[`once_cell`]: https://crates.io/crates/once_cell
479-
[`lazy_static`]: https://crates.io/crates/lazy_static
480-
481-
This example shows how to use `once_cell`:
479+
This example shows how to use `std::sync::LazyLock`:
482480
483481
```rust
484-
use {
485-
once_cell::sync::Lazy,
486-
regex::Regex,
487-
};
482+
use std::sync::LazyLock;
483+
484+
use regex::Regex;
488485
489486
fn some_helper_function(haystack: &str) -> bool {
490-
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"...").unwrap());
487+
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"...").unwrap());
491488
RE.is_match(haystack)
492489
}
493490
@@ -501,6 +498,9 @@ Specifically, in this example, the regex will be compiled when it is used for
501498
the first time. On subsequent uses, it will reuse the previously built `Regex`.
502499
Notice how one can define the `Regex` locally to a specific function.
503500
501+
[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
502+
[`once_cell`]: https://crates.io/crates/once_cell
503+
504504
### Sharing a regex across threads can result in contention
505505
506506
While a single `Regex` can be freely used from multiple threads simultaneously,

0 commit comments

Comments
 (0)