How to do it...

  1. In the bin folder, create a file called env_vars.rs

  2. Add the following code and run it with cargo run --bin env_vars:

1   use std::env;
2
3 fn main() {
4 // We can iterate over all the env vars for the current
process
5 println!("Listing all env vars:");
6 for (key, val) in env::vars() {
7 println!("{}: {}", key, val);
8 }
9
10 let key = "PORT";
11 println!("Setting env var {}", key);
12 // Setting an env var for the current process
13 env::set_var(key, "8080");
14
15 print_env_var(key);
16
17 // Removing an env var for the current process
18 println!("Removing env var {}", key);
19 env::remove_var(key);
20
21 print_env_var(key);
22 }
23
24 fn print_env_var(key: &str) {
25 // Accessing an env var
26 match env::var(key) {
27 Ok(val) => println!("{}: {}", key, val),
28 Err(e) => println!("Couldn't print env var {}: {}", key, e),
29 }
30 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.19.75