@@ -471,23 +471,20 @@ to a few **milliseconds** depending on the size of the pattern.) Not only is
471
471
compilation itself expensive, but this also prevents optimizations that reuse
472
472
allocations internally to the regex engine.
473
473
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.
477
478
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`:
482
480
483
481
```rust
484
- use {
485
- once_cell::sync::Lazy,
486
- regex::Regex,
487
- };
482
+ use std::sync::LazyLock;
483
+
484
+ use regex::Regex;
488
485
489
486
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());
491
488
RE.is_match(haystack)
492
489
}
493
490
@@ -501,6 +498,9 @@ Specifically, in this example, the regex will be compiled when it is used for
501
498
the first time. On subsequent uses, it will reuse the previously built `Regex`.
502
499
Notice how one can define the `Regex` locally to a specific function.
503
500
501
+ [`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
502
+ [`once_cell`]: https://crates.io/crates/once_cell
503
+
504
504
### Sharing a regex across threads can result in contention
505
505
506
506
While a single `Regex` can be freely used from multiple threads simultaneously,
0 commit comments