File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed
Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 77| [ Run piped external commands] [ ex-run-piped-external-commands ] | [ ![ std-badge]] [ std ] | [ ![ cat-os-badge]] [ cat-os ] |
88| [ Redirect both stdout and stderr of child process to the same file] [ ex-redirect-stdout-stderr-same-file ] | [ ![ std-badge]] [ std ] | [ ![ cat-os-badge]] [ cat-os ] |
99| [ Continuously process child process' outputs] [ ex-continuous-process-output ] | [ ![ std-badge]] [ std ] | [ ![ cat-os-badge]] [ cat-os ] [ ![ cat-text-processing-badge]] [ cat-text-processing ] |
10+ | [ Read environment variable] [ ex-read-env-variable ] | [ ![ std-badge]] [ std ] | [ ![ cat-os-badge]] [ cat-os ] |
11+
1012
1113[ ex-parse-subprocess-output ] : os/external.html#run-an-external-command-and-process-stdout
1214[ ex-parse-subprocess-input ] : os/external.html#run-an-external-command-passing-it-stdin-and-check-for-an-error-code
1315[ ex-run-piped-external-commands ] : os/external.html#run-piped-external-commands
1416[ ex-redirect-stdout-stderr-same-file ] : os/external.html#redirect-both-stdout-and-stderr-of-child-process-to-the-same-file
1517[ ex-continuous-process-output ] : os/external.html#continuously-process-child-process-outputs
18+ [ ex-read-env-variable ] : os/external.html#read-environment-variable
19+
1620
1721{{#include links.md}}
Original file line number Diff line number Diff line change 1010
1111{{#include external/continuous.md}}
1212
13+ {{#include external/read-env-variable.md}}
14+
1315{{#include ../links.md}}
Original file line number Diff line number Diff line change 1+ ## Read Environment Variable
2+
3+ [ ![ std-badge]] [ std ] [ ![ cat-os-badge]] [ cat-os ]
4+
5+ Reads an environment variable via [ std::env::var] .
6+
7+ ``` rust,edition2018,no_run
8+ use std::env;
9+ use std::fs;
10+ use std::io::Error;
11+
12+ fn main() -> Result<(), Error> {
13+ // read `config_path` from the environment variable `CONFIG`.
14+ // If `CONFIG` isn't set, fall back to a default config path.
15+ let config_path = env::var("CONFIG")
16+ .unwrap_or("/etc/myapp/config".to_string());
17+
18+ let config: String = fs::read_to_string(config_path)?;
19+ println!("Config: {}", config);
20+
21+ Ok(())
22+ }
23+ ```
24+
25+ [ std::env::var ] : https://doc.rust-lang.org/std/env/fn.var.html
You can’t perform that action at this time.
0 commit comments