Rust Environment Variables – Set, Update, List And Delete
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.
Rust
1
2
3
4
5
6
7
8
usestd::env;
fnmain(){
letenv_vars=env::vars();
for(key,value)inenv_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.
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.
Rust
1
2
3
4
5
6
7
8
9
10
11
usestd::env;
fnmain(){
letpath_env=env::var("CARGO_HOME");
ifpath_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.
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.
Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
usestd::env;
fnmain(){
env::set_var("TURRETA_URL","www.turreta.com");
letturreta_url_env=env::var("TURRETA_URL");
ifturreta_url_env.is_ok(){
println!("Turreta URL is {}",turreta_url_env.unwrap());
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.
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.
1397 total views
, 1 views today
Got comments or suggestions?
We disabled the comments on this site to fight off spammers,
but you can still contact us via our Facebook page!.