Skip to content

Commit eec911e

Browse files
Rollup merge of rust-lang#151690 - xtqqczze:env-var, r=jhpratt
std: Refactor `env::var` function
2 parents a8d7877 + 0f6b9bd commit eec911e

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

library/std/src/env.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@ impl fmt::Debug for VarsOs {
220220
/// ```
221221
#[stable(feature = "env", since = "1.0.0")]
222222
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
223-
_var(key.as_ref())
224-
}
225-
226-
fn _var(key: &OsStr) -> Result<String, VarError> {
227-
match var_os(key) {
228-
Some(s) => s.into_string().map_err(VarError::NotUnicode),
229-
None => Err(VarError::NotPresent),
223+
fn inner(key: &OsStr) -> Result<String, VarError> {
224+
env_imp::getenv(key)
225+
.ok_or(VarError::NotPresent)?
226+
.into_string()
227+
.map_err(VarError::NotUnicode)
230228
}
229+
inner(key.as_ref())
231230
}
232231

233232
/// Fetches the environment variable `key` from the current process, returning
@@ -257,11 +256,7 @@ fn _var(key: &OsStr) -> Result<String, VarError> {
257256
#[must_use]
258257
#[stable(feature = "env", since = "1.0.0")]
259258
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
260-
_var_os(key.as_ref())
261-
}
262-
263-
fn _var_os(key: &OsStr) -> Option<OsString> {
264-
env_imp::getenv(key)
259+
env_imp::getenv(key.as_ref())
265260
}
266261

267262
/// The error type for operations interacting with environment variables.

0 commit comments

Comments
 (0)