@@ -318,11 +318,6 @@ impl Error for VarError {
318318///
319319/// # Safety
320320///
321- /// Even though this function is currently not marked as `unsafe`, it needs to
322- /// be because invoking it can cause undefined behaviour. The function will be
323- /// marked `unsafe` in a future version of Rust. This is tracked in
324- /// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
325- ///
326321/// This function is safe to call in a single-threaded program.
327322///
328323/// In multi-threaded programs, you must ensure that are no other threads
@@ -331,7 +326,7 @@ impl Error for VarError {
331326/// how to achieve this, but we strongly suggest not using `set_var` or
332327/// `remove_var` in multi-threaded programs at all.
333328///
334- /// Most C libraries, including libc itself do not advertise which functions
329+ /// Most C libraries, including libc itself, do not advertise which functions
335330/// read from the environment. Even functions from the Rust standard library do
336331/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
337332///
@@ -353,15 +348,26 @@ impl Error for VarError {
353348/// use std::env;
354349///
355350/// let key = "KEY";
356- /// env::set_var(key, "VALUE");
351+ /// unsafe {
352+ /// env::set_var(key, "VALUE");
353+ /// }
357354/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
358355/// ```
356+ #[ cfg( not( bootstrap) ) ]
357+ #[ rustc_deprecated_safe_2024]
359358#[ stable( feature = "env" , since = "1.0.0" ) ]
360- pub fn set_var < K : AsRef < OsStr > , V : AsRef < OsStr > > ( key : K , value : V ) {
359+ pub unsafe fn set_var < K : AsRef < OsStr > , V : AsRef < OsStr > > ( key : K , value : V ) {
361360 _set_var ( key. as_ref ( ) , value. as_ref ( ) )
362361}
363362
364- fn _set_var ( key : & OsStr , value : & OsStr ) {
363+ #[ cfg( bootstrap) ]
364+ #[ allow( missing_docs) ]
365+ #[ stable( feature = "env" , since = "1.0.0" ) ]
366+ pub fn set_var < K : AsRef < OsStr > , V : AsRef < OsStr > > ( key : K , value : V ) {
367+ unsafe { _set_var ( key. as_ref ( ) , value. as_ref ( ) ) }
368+ }
369+
370+ unsafe fn _set_var ( key : & OsStr , value : & OsStr ) {
365371 os_imp:: setenv ( key, value) . unwrap_or_else ( |e| {
366372 panic ! ( "failed to set environment variable `{key:?}` to `{value:?}`: {e}" )
367373 } )
@@ -371,11 +377,6 @@ fn _set_var(key: &OsStr, value: &OsStr) {
371377///
372378/// # Safety
373379///
374- /// Even though this function is currently not marked as `unsafe`, it needs to
375- /// be because invoking it can cause undefined behaviour. The function will be
376- /// marked `unsafe` in a future version of Rust. This is tracked in
377- /// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
378- ///
379380/// This function is safe to call in a single-threaded program.
380381///
381382/// In multi-threaded programs, you must ensure that are no other threads
@@ -384,7 +385,7 @@ fn _set_var(key: &OsStr, value: &OsStr) {
384385/// how to achieve this, but we strongly suggest not using `set_var` or
385386/// `remove_var` in multi-threaded programs at all.
386387///
387- /// Most C libraries, including libc itself do not advertise which functions
388+ /// Most C libraries, including libc itself, do not advertise which functions
388389/// read from the environment. Even functions from the Rust standard library do
389390/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
390391///
@@ -403,22 +404,35 @@ fn _set_var(key: &OsStr, value: &OsStr) {
403404///
404405/// # Examples
405406///
406- /// ```
407+ /// ```no_run
407408/// use std::env;
408409///
409410/// let key = "KEY";
410- /// env::set_var(key, "VALUE");
411+ /// unsafe {
412+ /// env::set_var(key, "VALUE");
413+ /// }
411414/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
412415///
413- /// env::remove_var(key);
416+ /// unsafe {
417+ /// env::remove_var(key);
418+ /// }
414419/// assert!(env::var(key).is_err());
415420/// ```
421+ #[ cfg( not( bootstrap) ) ]
422+ #[ rustc_deprecated_safe_2024]
416423#[ stable( feature = "env" , since = "1.0.0" ) ]
417- pub fn remove_var < K : AsRef < OsStr > > ( key : K ) {
424+ pub unsafe fn remove_var < K : AsRef < OsStr > > ( key : K ) {
418425 _remove_var ( key. as_ref ( ) )
419426}
420427
421- fn _remove_var ( key : & OsStr ) {
428+ #[ cfg( bootstrap) ]
429+ #[ allow( missing_docs) ]
430+ #[ stable( feature = "env" , since = "1.0.0" ) ]
431+ pub fn remove_var < K : AsRef < OsStr > > ( key : K ) {
432+ unsafe { _remove_var ( key. as_ref ( ) ) }
433+ }
434+
435+ unsafe fn _remove_var ( key : & OsStr ) {
422436 os_imp:: unsetenv ( key)
423437 . unwrap_or_else ( |e| panic ! ( "failed to remove environment variable `{key:?}`: {e}" ) )
424438}
0 commit comments