Crates.io | env_swapper |
lib.rs | env_swapper |
version | 0.1.2 |
source | src |
created_at | 2024-10-29 17:10:55.408638 |
updated_at | 2024-11-21 01:03:30.2592 |
description | A lightweight Rust library to temporarily set environment variables and automatically restore them when out of scope. |
homepage | |
repository | |
max_upload_size | |
id | 1427347 |
size | 5,655 |
env_swapper
is a lightweight Rust library to temporarily set environment variables, automatically restoring the previous state when out of scope.
Add env_swapper
to your Cargo.toml
:
[dependencies]
env_swapper = "0.1.0"
use env_swapper::EnvSwapper;
use std::env;
fn main() {
let key = "EXAMPLE_VAR";
{
let _swapper = EnvSwapper::new(&[(key, "temporary_value")]);
println!("Inside scope: {:?}", env::var(key)); // Outputs "temporary_value"
}
println!("Outside scope: {:?}", env::var(key)); // Restored to original
}
Temporarily sets and restores environment variables automatically on scope exit. Ideal for testing, sandboxed configurations, and controlled environment changes.
This project is licensed under the MIT License.
bensatlantik