File tree Expand file tree Collapse file tree 3 files changed +21
-13
lines changed Expand file tree Collapse file tree 3 files changed +21
-13
lines changed Original file line number Diff line number Diff line change @@ -187,8 +187,6 @@ version = "0.8.5"
187
187
default-features = false
188
188
189
189
[dev-dependencies ]
190
- # For examples.
191
- once_cell = " 1.17.1"
192
190
# For property based tests.
193
191
quickcheck = { version = " 1.0.3" , default-features = false }
194
192
# To check README's example
Original file line number Diff line number Diff line change @@ -80,18 +80,24 @@ compilation itself expensive, but this also prevents optimizations that reuse
80
80
allocations internally to the matching engines.
81
81
82
82
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
86
92
87
93
``` rust
88
94
use {
89
- once_cell :: sync :: Lazy ,
95
+ std :: sync :: LazyLock ,
90
96
regex :: Regex ,
91
97
};
92
98
93
99
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 ());
95
101
RE . is_match (haystack )
96
102
}
97
103
Original file line number Diff line number Diff line change @@ -471,23 +471,27 @@ 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.
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
478
482
[`once_cell`]: https://crates.io/crates/once_cell
479
483
[`lazy_static`]: https://crates.io/crates/lazy_static
480
484
481
485
This example shows how to use `once_cell`:
482
486
483
487
```rust
484
488
use {
485
- once_cell ::sync::Lazy ,
489
+ std ::sync::LazyLock ,
486
490
regex::Regex,
487
491
};
488
492
489
493
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());
491
495
RE.is_match(haystack)
492
496
}
493
497
You can’t perform that action at this time.
0 commit comments