All jobs

Replace lazy_static With std::sync::LazyLock in a Rust Project

Yes. An agent replaces each lazy_static macro block with std::sync::LazyLock, checks the Rust version the project supports and drops the crate from Cargo.lock.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The standard library has had std::sync::LazyLock since Rust 1.80, and it does what the lazy_static! macro does: a static whose value is computed on first use, once, safely across threads. The agent begins with the question that decides whether the swap is allowed at all, which is the oldest compiler the project promises to build on. It reads rust-version in Cargo.toml, any rust-toolchain file, and the toolchains named in CI. In a project like mgdm/htmlq there is no rust-version line, but the manifest says edition = "2024", and that edition needs a newer compiler than LazyLock does. The floor is already above 1.80, so the change costs no user their build. Where the answer is the other way round, the agent stops and reports the conflict instead of raising the minimum version on its own.

Next it finds every use, and who else wants the crate. cargo tree -i lazy_static prints the reverse dependencies; if another crate in the graph depends on it, removing the line from Cargo.toml tidies your manifest but the crate stays in the build. In htmlq the only dependent is htmlq itself, through lazy_static = "1.4.0", and the only use is in src/pretty_print.rs: a lazy_static! block that declares static ref INLINE_ELEMENTS: HashSet<&'static str>, built from a vec! of 54 HTML element names with .into_iter().collect(), and read on one line by INLINE_ELEMENTS.contains(name).

The rewrite replaces the macro block with an ordinary item: static INLINE_ELEMENTS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| ...). Three details change and the agent handles each explicitly. static ref NAME: T becomes static NAME: LazyLock<T>, so the type is now written where a reader can see what the static really is. The initialiser moves into a closure, which is also a chance to drop the detour through a Vec and build the set with HashSet::from([...]). And the call sites stay as they are, because LazyLock<T> dereferences to T just as the macro's hidden wrapper type did, so INLINE_ELEMENTS.contains(name) compiles unchanged. The agent checks that last point by compiling, not by assuming: code that passed &*NAME to a function, or named the macro's generated type, does need edits.

Then it removes use lazy_static::lazy_static;, deletes the dependency line, lets cargo update Cargo.lock, and runs cargo tree -i lazy_static again to show the crate is gone from the graph and not merely from the manifest. It runs the tests, which here means the unit tests and the command-line tests in tests/cli.rs that drive the built binary with assert_cmd. It also looks for the sibling crate. If the project uses once_cell, the agent reports where, since once_cell::sync::Lazy maps to LazyLock and OnceCell to OnceLock the same way, but it leaves that for a separate pull request unless the prompt asks for both.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Replace the lazy_static crate with std::sync::LazyLock in this Rust project. First establish the minimum supported Rust version: rust-version in Cargo.toml, the edition, any rust-toolchain file, and the toolchains used in CI workflows and other build files. LazyLock needs Rust 1.80. If the project supports an older compiler, stop and report the conflict; do not raise the minimum version yourself. Run `cargo tree -i lazy_static` and save the output: say whether this project is the only dependent or another crate also pulls it in. Find every lazy_static! block and every use of the statics it declares. Rewrite each `static ref NAME: T = expr;` as `static NAME: LazyLock<T> = LazyLock::new(|| expr);`, keeping names, types, visibility and attributes. Simplify an initialiser only where the result is plainly the same value, and say where you did. Fix call sites only where the compiler requires it, and list them. Use LazyLock for statics shared across threads; do not switch any to LazyCell or thread-local storage. Remove the use lines and the dependency from Cargo.toml, update Cargo.lock, and run `cargo tree -i lazy_static` again. Run cargo fmt, cargo clippy and cargo test. If once_cell is also a dependency, report its uses but do not change them. Open a pull request with both cargo tree outputs and the test summary.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request touching three files in a repository of this size: src/pretty_print.rs with the macro block replaced, Cargo.toml without the dependency line, and Cargo.lock without the package entry. The description states the minimum Rust version it found and where it read it, shows cargo tree -i lazy_static before, with the project as the sole dependent, and after, where cargo answers that the package is not in the graph, and gives the cargo test summary.

A reviewer checks that each static kept its name, type and visibility, which the diff shows side by side, and that any simplified initialiser builds the same set. For a set of string literals that is a matter of reading the list. They also look at the stated minimum version: if your team builds with a pinned older toolchain that no file in the repository records, this is the moment to say so, and the agent's report of what it checked makes that omission visible.

Run it on every Monday morning

Dependencies that the standard library has made unnecessary are a good fit for a scheduled pass. Add an automation on the Project with a schedule trigger and a weekly cron, with the prompt above. A run that finds no lazy_static in Cargo.toml ends with a one-line report, so the schedule can stay on to catch the crate if a later change brings it back.

Across a workspace or several repositories, run it once per Project. Each run has its own sandbox and its own cargo cache, so one crate's long build does not hold up another, and the automation's max runs per hour bounds how many start in the same slot.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

The edit is small and the checks around it are what take time: finding the real minimum compiler version, asking cargo who else depends on the crate, compiling to see which call sites the type change touches, and asking cargo again afterwards. A person doing this between other tasks skips the first and the last. The agent runs all of them in a sandbox with a clean toolchain and keeps the command output, so the claim that the crate left the build is backed by cargo's own answer.

It also scales without attention. One static in one file is a five-minute change; forty across a workspace is an afternoon of identical edits where the one unusual block, a static with a lifetime trick or a pub(crate) on it, is easy to mishandle. The agent applies the same rule to each and lists the ones that needed anything more than the rule.

Frequently asked questions

What Rust version does LazyLock need?

std::sync::LazyLock and std::cell::LazyCell are stable from Rust 1.80. OnceLock, the lower-level type for a value set once at runtime, has been stable since 1.70. If your crate declares an older rust-version, keep lazy_static or once_cell until you raise it.

Do I have to change the code that uses the static?

Usually not. LazyLock<T> implements Deref<Target = T>, so method calls such as NAME.contains(x) work as before. Places that need the inner value as a plain reference, for example passing it to a function that takes &T, may need &*NAME, and the compiler points to each one.

Is there any difference in behaviour?

Not in ordinary use. Both initialise on first access, run the initialiser once, and make other threads wait until the value is ready. One property worth knowing is documented for LazyLock: if the initialiser panics, the static is poisoned and every later access panics as well. Initialisers that only build a constant table, as most do, never reach that case.

Why is lazy_static still in Cargo.lock after I removed it?

Because another crate in your dependency graph uses it. cargo tree -i lazy_static names which one. You cannot remove it from your side; the agent reports the crate and whether a newer release of it has dropped the dependency.

More jobs like this: Upgrade dependencies weekly

Keep reading