This short post shows how to use Rust to work with environment variables in Rust codes. We could retrieve, list, update, and even delete environment variables. All this is possible using functions from the std::env module.
List Environment Variables
The following codes list all environment variables available in the current host machine. First, we need to import the std::env module using the use keyword on line 1. Then, retrieve the environment variables using the vars() function which returns an instance of Vars.
1 2 3 4 5 6 7 8 | use std::env; fn main() { let env_vars = env::vars(); for(key, value) in env_vars.into_iter() { println!("{} = {:?}", key, value); } } |
The struct Vars implements the Iterator trait. Therefore, we can loop through its content and display the key-value data in line 6. The codes generate the following output.
1 2 3 4 5 6 7 8 9 10 | =:: = "::\\" ALLUSERSPROFILE = "C:\\ProgramData" APPDATA = "C:\\Users\\karldev\\AppData\\Roaming" CARGO = "\\\\?\\C:\\Users\\karldev\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\bin\\cargo.exe" CARGO_HOME = "C:\\Users\\karldev\\.cargo" CARGO_MANIFEST_DIR = "C:\\Users\\karldev\\Desktop\\dev\\blogs\\rust\\actix-web-capture-param-and-payload" CARGO_PKG_AUTHORS = "Karl San Gabriel" CARGO_PKG_DESCRIPTION = "" CARGO_PKG_HOMEPAGE = "" ... |
Get Environment Variable
Sometimes, we want to retrieve a particular environment variable. If Rust has the vars() function to retrieve a list of environment variables, it also has the var(key) function to retrieve the value of a specific environment variable. Consider the following example.
1 2 3 4 5 6 7 8 9 10 11 | use std::env; fn main() { let path_env = env::var("CARGO_HOME"); if path_env.is_ok() { println!("Cargo Home is {}", path_env.unwrap()); } else { println!("Env var not found"); } } |
The environment variable is defined because we had installed Rust in our host machine. If we run the codes, we get the following output.
1 2 3 4 5 6 | C:/Users/karldev/.cargo/bin/cargo.exe run --color=always --package environment-variables --bin environment-variables Finished dev [unoptimized + debuginfo] target(s) in 0.22s Running `target\debug\environment-variables.exe` Cargo Home is C:\Users\karldev\.cargo Process finished with exit code 0 |
The var(key) function returns a Result<String, VarError>. Therefore, we can also validate if the environment variable is available using an if-else statement. In line 6, we can either use the is_ok or is_error function. If CARGO_HOME was undefined and we did not have the if-else statement, we would have this run-time error:
1 2 3 4 | thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotPresent', src\libcore\result.rs:1084:5 stack backtrace: 0: backtrace::backtrace::trace_unsynchronized ... |
Set Environment Variable
We can also define with a value or update an environment variable in Rust using the set_var(key, value) function. Consider the following sample codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 | use std::env; fn main() { env::set_var("TURRETA_URL", "www.turreta.com"); let turreta_url_env = env::var("TURRETA_URL"); if turreta_url_env.is_ok() { println!("Turreta URL is {}", turreta_url_env.unwrap()); } else { println!("Env var not found"); } } |
The codes output the following.
1 2 3 4 5 6 | C:/Users/karldev/.cargo/bin/cargo.exe run --color=always --package environment-variables --bin environment-variables Finished dev [unoptimized + debuginfo] target(s) in 0.06s Running `target\debug\environment-variables.exe` Turreta URL is www.turreta.com Process finished with exit code 0 |
Delete a Variable
We can also delete an environment variable in Rust using the remove_var(key) function. This function does not return anything, therefore, no way to check whether the deletion was successful or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | use std::env; fn main() { env::set_var("TURRETA_URL", "www.turreta.com"); let turreta_url_env = env::var("TURRETA_URL"); if turreta_url_env.is_ok() { println!("{}", turreta_url_env.unwrap()); } else { println!("Env var not found"); } env::remove_var("TURRETA_URL"); let turreta_url_env = env::var("TURRETA_URL"); if turreta_url_env.is_ok() { println!("{}", turreta_url_env.unwrap()); } else { println!("Env var not found"); } } |
The codes generate the following output.
1 2 3 4 5 6 7 | C:/Users/karldev/.cargo/bin/cargo.exe run --color=always --package environment-variables --bin environment-variables Finished dev [unoptimized + debuginfo] target(s) in 0.13s Running `target\debug\environment-variables.exe` www.turreta.com Env var not found Process finished with exit code 0 |
In summary, we can use Rust codes to work in environment variables using the std::env module. The module has functions our codes can use to define, update or delete environment variables from our applications.
We used Rust 1.52.1 to test all the codes in this post.