Last active
May 9, 2023 17:03
-
-
Save rcook/5756a268513be6dfbddca59b7d78f98a to your computer and use it in GitHub Desktop.
Optionally get environment variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn var_opt<K>(key: K) -> Result<Option<String>> | |
where | |
K: AsRef<OsStr>, | |
{ | |
use std::env::{var, VarError}; | |
match var(key) { | |
Ok(value) => Ok(Some(value)), | |
Err(VarError::NotPresent) => Ok(None), | |
_ => bail!("environment variable not found"), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment