| Crates.io | rust_dotenv |
| lib.rs | rust_dotenv |
| version | 0.1.3 |
| created_at | 2024-11-07 13:44:53.839056+00 |
| updated_at | 2025-10-29 09:33:10.621643+00 |
| description | A Rust crate to load environment variables from .env files. |
| homepage | https://github.com/larevuegeek/rust_dotenv |
| repository | https://github.com/larevuegeek/rust_dotenv |
| max_upload_size | |
| id | 1439772 |
| size | 11,847 |
rust_dotenv is a Rust library designed to load environment variables from .env files, supporting multiple environments and local overrides. This library is perfect for managing configurations dynamically across different environments like development, testing, and production.
Note: I'm French dev begginer in Rust, and this is my first Rust project! Feel free to open issues or submit pull requests to suggest improvements and new features. Contributions are more than welcome!
.env file or an environment-specific file like .env.development.rust_dotenv checks for a .local file, such as .env.development.local or .env.local. If this file exists, it will be prioritized over the main .env file, allowing for easy local configuration without modifying environment files directly.get_var), check existence (has_var), and set new environment variables (set_var) without overwriting existing ones.Add rust_dotenv to your Cargo.toml dependencies:
[dependencies]
rust_dotenv = "0.1.0"
rust_dotenv with an EnvironmentUse DotEnv::new() to load variables from a specified environment. Pass the environment name as a string (like "development"), or an empty string "" to load the default .env file.
use rust_dotenv::dotenv::DotEnv;
fn main() {
// Load the default `.env` file
let dotenv = DotEnv::new("");
println!("Loaded variables (default): {:?}", dotenv.all_vars());
// Load environment-specific file, e.g., `.env.development`
let dotenv_dev = DotEnv::new("development");
println!("Loaded variables (development): {:?}", dotenv_dev.all_vars());
}
get_varRetrieve a specific environment variable using get_var.
fn main() {
let dotenv = DotEnv::new("development");
if let Some(value) = dotenv.get_var("DATABASE_URL".to_string()) {
println!("DATABASE_URL = {}", value);
} else {
println!("DATABASE_URL is not set.");
}
}
has_varCheck if a variable exists with has_var.
fn main() {
let dotenv = DotEnv::new("development");
if dotenv.has_var("SECRET_KEY".to_string()) {
println!("SECRET_KEY is set.");
} else {
println!("SECRET_KEY is not set.");
}
}
set_varAdd a new variable dynamically without overwriting existing ones. set_var returns false if the variable already exists.
fn main() {
let mut dotenv = DotEnv::new("development");
let added = dotenv.set_var("NEW_VARIABLE".to_string(), "value".to_string());
if added {
println!("NEW_VARIABLE added successfully.");
} else {
println!("NEW_VARIABLE already exists.");
}
}
DotEnv::new(env: &str) -> DotEnv: Loads variables from .env, .env.{env}, or .env.{env}.local, depending on the environment. If a .local file exists, it will take priority.get_var(&self, key: String) -> Option<String>: Retrieves the value of a variable.has_var(&self, key: String) -> bool: Checks if a variable is defined in the loaded configuration.all_vars(&self) -> HashMap<String, String>: Returns all environment variables as a HashMap.set_var(&mut self, key: String, value: String) -> bool: Adds a new variable only if it doesn’t already exist.This project is open to contributions and suggestions! Since it’s my first Rust project, any feedback or feature requests are appreciated. Don’t hesitate to open an issue or pull request to help improve rust_dotenv.
git checkout -b feature-branch)git commit -am 'Add new feature')git push origin feature-branch)This README introduces rust_dotenv's capabilities, clarifies its default behavior with .local files, and invites the community to contribute. Let me know if any further adjustments are needed!