Skip to content

Instantly share code, notes, and snippets.

@abhik-99
Last active August 1, 2022 09:37
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(())
}
@abhik-99
Copy link
Author

abhik-99 commented Aug 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment