/set_environment_variable.rs Secret
Last active
August 1, 2022 09:37
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::path::Path; | |
use std::error::Error; | |
use std::fs; | |
fn set_env(filename: String) -> Result<(), Box<dyn Error>> { | |
let data = fs::read_to_string(Path::new(&filename))?; | |
for x in data.split("\n") { | |
let v = x.split("=").collect::<Vec<&str>>(); | |
println!("Setting {} to {}", v[0], v[1]); | |
env::set_var(v[0], v[1]); | |
}; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist showcases how environment variables can be set in the process. The env variables which are set are temporal and will be erased once the program concludes. Use cases for this include reading from
.env
files in the project root in projects involving web-development, game development and CLI programming.