-
Notifications
You must be signed in to change notification settings - Fork 303
Add an example to read an environment variable #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
<a name="ex-std-read-lines"></a> | ||
|
@@ -1770,6 +1771,48 @@ fn run() -> Result<()> { | |
# quick_main!(run); | ||
``` | ||
|
||
[ex-read-environment-variable]: #ex-read-environment-variable | ||
<a name="ex-read-environment-variable"></a> | ||
## 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! { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this example can be completely rewritten without |
||
# foreign_links { | ||
# Utf8(std::str::Utf8Error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neither of these are needed for this example |
||
# AddrParse(std::net::AddrParseError); | ||
# } | ||
# } | ||
|
||
fn set_env_variable() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really sold on using separate function to wrap one liner here. |
||
env::set_var("CONFIG_FILE", "config.toml"); | ||
} | ||
|
||
fn run() -> Result<()> { | ||
set_env_variable(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setting and reading the same env variable does not make a control flow that is close to being realistic. How about we set one variable and read another? |
||
|
||
let default_config_file = "config.toml"; | ||
|
||
let config_file_path = match env::var("CONFIG_FILE") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might a matter of taste but how about rewriting this simple transformation from match to let config_file_path = env::var("CONFIG_FILE")
.map(PathBuf::from)
.unwrap_or_else(|_| default_config_file.into()); |
||
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}} | ||
|
||
<!-- API Reference --> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to link to std documentation for
std::env::set_var
andstd::env::var