From 6b319db6e453f802f77e28bb09a14593e34f19df Mon Sep 17 00:00:00 2001 From: dinesh Date: Sun, 1 Jul 2018 15:29:47 +0530 Subject: [PATCH] Add an example to read an environment variable --- src/basics.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/intro.md | 2 ++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/basics.md b/src/basics.md index 770e5008..acbaa92b 100644 --- a/src/basics.md +++ b/src/basics.md @@ -38,7 +38,8 @@ | [Parse string into DateTime struct][ex-parse-datetime] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | | [Perform checked date and time calculations][ex-datetime-arithmetic] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | | [Examine the date and time][ex-examine-date-and-time] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | -| [File names that have been modified in the last 24 hours for the working directory][ex-file-24-hours-modified] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] [![cat-os-badge]][cat-os] | +| [File names that have been modified in the last 24 hours for the working directory][ex-file-24-hours-modified] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] [![cat-os-badge]][cat-os] | +| [Read environment variable][ex-read-environment-variable] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] | [ex-std-read-lines]: #ex-std-read-lines @@ -1770,6 +1771,48 @@ fn run() -> Result<()> { # quick_main!(run); ``` +[ex-read-environment-variable]: #ex-read-environment-variable + +## Read environment variable +[![std-badge]][std] [![cat-filesystem-badge]][cat-filesystem] + +Set an environment variable and read it back. If the variable is not found, then fallback +to a default value. + +```rust +# #[macro_use] +# extern crate error_chain; +use std::env; +use std::path::PathBuf; + +# error_chain! { +# foreign_links { +# Utf8(std::str::Utf8Error); +# AddrParse(std::net::AddrParseError); +# } +# } + +fn set_env_variable() { + env::set_var("CONFIG_FILE", "config.toml"); +} + +fn run() -> Result<()> { + set_env_variable(); + + let default_config_file = "config.toml"; + + let config_file_path = match env::var("CONFIG_FILE") { + Ok(val) => PathBuf::from(val), + Err(_) => PathBuf::from(default_config_file) + }; + + println!("Env value is {}", config_file_path.display()); + Ok(()) +} +# +# quick_main!(run); +``` + {{#include links.md}} diff --git a/src/intro.md b/src/intro.md index 26af488b..8d2efc4f 100644 --- a/src/intro.md +++ b/src/intro.md @@ -57,6 +57,7 @@ community. It needs and welcomes help. For details see | [Perform checked date and time calculations][ex-datetime-arithmetic] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | | [Examine the date and time][ex-examine-date-and-time] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | | [File names that have been modified in the last 24 hours for the working directory][ex-file-24-hours-modified] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] [![cat-os-badge]][cat-os] | +| [Read environment variable][ex-read-environment-variable] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] | ## [Encoding](encoding.html) @@ -193,6 +194,7 @@ community. It needs and welcomes help. For details see [ex-extract-links-webpage]: net.html#ex-extract-links-webpage [ex-extract-mediawiki-links]: net.html#ex-extract-mediawiki-links [ex-file-24-hours-modified]: basics.html#ex-file-24-hours-modified +[ex-read-environment-variable]: basics.html#ex-read-environment-variable [ex-file-post]: net.html#ex-file-post [ex-file-predicate]: app.html#ex-file-predicate [ex-file-sizes]: app.html#ex-file-sizes