Skip to content
Merged
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
19 changes: 7 additions & 12 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ impl fmt::Debug for VarsOs {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
_var(key.as_ref())
}

fn _var(key: &OsStr) -> Result<String, VarError> {
match var_os(key) {
Some(s) => s.into_string().map_err(VarError::NotUnicode),
None => Err(VarError::NotPresent),
fn inner(key: &OsStr) -> Result<String, VarError> {
env_imp::getenv(key)
.ok_or(VarError::NotPresent)?
.into_string()
.map_err(VarError::NotUnicode)

@hkBst hkBst May 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I already said, I prefer this code to call var_os.

View changes since the review

}
inner(key.as_ref())
}

/// Fetches the environment variable `key` from the current process, returning
Expand Down Expand Up @@ -257,11 +256,7 @@ fn _var(key: &OsStr) -> Result<String, VarError> {
#[must_use]
#[stable(feature = "env", since = "1.0.0")]
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
_var_os(key.as_ref())
}

fn _var_os(key: &OsStr) -> Option<OsString> {
env_imp::getenv(key)
env_imp::getenv(key.as_ref())
}

/// The error type for operations interacting with environment variables.
Expand Down
Loading