Skip to content
Merged
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
1.11.2 (2025-08-24)
===================
This is a new patch release of `regex` with some minor fixes.

Improvements:

* [BUG #1217](https://github.com/rust-lang/regex/issues/1217):
Switch recommendation from `once_cell` to `std::sync::LazyLock`.

Bug fixes:

* [BUG #1281](https://github.com/rust-lang/regex/pull/1281):
Remove `fuzz/` and `record/` directories from published crate on crates.io.


1.11.1 (2024-10-24)
===================
This is a new patch release of `regex` that fixes compilation on nightly
Expand Down
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
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,19 @@ 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`], or the [`once_cell`] crate,
if you can't use the standard library.

This example shows how to use `std::sync::LazyLock`:

```rust
use {
once_cell::sync::Lazy,
regex::Regex,
};
use std::sync::LazyLock;

use 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 All @@ -104,6 +105,9 @@ fn main() {
Specifically, in this example, the regex will be compiled when it is used for
the first time. On subsequent uses, it will reuse the previous compilation.

[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
[`once_cell`]: https://crates.io/crates/once_cell

### Usage: match regular expressions on `&[u8]`

The main API of this crate (`regex::Regex`) requires the caller to pass a
Expand Down
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,20 @@ 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`], or the [`once_cell`] crate,
if you can't use the standard library.

[`once_cell`]: https://crates.io/crates/once_cell
[`lazy_static`]: https://crates.io/crates/lazy_static

This example shows how to use `once_cell`:
This example shows how to use `std::sync::LazyLock`:

```rust
use {
once_cell::sync::Lazy,
regex::Regex,
};
use std::sync::LazyLock;

use 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 All @@ -501,6 +498,9 @@ Specifically, in this example, the regex will be compiled when it is used for
the first time. On subsequent uses, it will reuse the previously built `Regex`.
Notice how one can define the `Regex` locally to a specific function.

[`std::sync::LazyLock`]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
[`once_cell`]: https://crates.io/crates/once_cell

### Sharing a regex across threads can result in contention

While a single `Regex` can be freely used from multiple threads simultaneously,
Expand Down
Loading