Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active May 9, 2023 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcook/5756a268513be6dfbddca59b7d78f98a to your computer and use it in GitHub Desktop.
Save rcook/5756a268513be6dfbddca59b7d78f98a to your computer and use it in GitHub Desktop.
Optionally get environment variable
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