Skip to content
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

Add an example to read an environment variable #422

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Copy link
Collaborator

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 and std::env::var

to a default value.

```rust
# #[macro_use]
# extern crate error_chain;
use std::env;
use std::path::PathBuf;

# error_chain! {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example can be completely rewritten without error_chain crate just change fn run() -> Result<()> to fn main() and remove Ok(()) and quick_main with all error chain specific code

# foreign_links {
# Utf8(std::str::Utf8Error);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Also the duplication of the "config.toml" string in two places might be slightly confusing how about we use different values?

env::set_var("CONFIG_FILE", "config.toml");
}

fn run() -> Result<()> {
set_env_variable();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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") {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 Result combinators with something like:

    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 -->
Expand Down
2 changes: 2 additions & 0 deletions src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down