tmp_env
tmp_env is a crate which lets you create temporary environment and be automatically cleaned when not needed.
For example sometimes you need to change the current directory or set environment variables to launch a process but you don't need this temporary environment for the rest of your program.
Then you will use tmp_env
to create environment variable using tmp_env::set_var
instead of std::env::set_var
to get from tmp_env::set_var
a datastructure which will automatically restore the
corresponding environment variable when dropped.
Install
Put this dependency in your Cargo.toml
= "0.1"
Usage
- To temporary change the current directory:
let current_dir = current_dir.expect;
assert!;
- To temporary set an environment variable:
// Because guard `_tmp_env` is dropped then the environment variable is also automatically unset (not restored because no previous value was set)
assert!;
- To temporary create a directory
// The temporary directory is now removed