env-required
Ultra-lightweight required environment variable validation for Rust.
- Zero dependencies (std-only)
- One macro:
required! - Helpful panic messages (designed for backend/CLI/server startup)
- Optional
FromStrparsing
If you want your program to fail fast with a clear message when configuration is missing, this crate is for you.
Installation
[]
= "0.1"
Quick Start (single env)
use required;
Validate Many (startup check)
use required;
Type Conversion (FromStr)
use required;
Custom Message
use required;
Why a macro?
required! is a macro because:
- It gives a single, memorable entry point without importing multiple helpers.
- We can provide syntax-level guidance (and
compile_error!for misuses). - It keeps call sites short and consistent in binaries and libraries.
Internally, the macro expands to small std::env helpers. There is no hidden magic.
FAQ
Why panic instead of returning Result?
This crate targets startup configuration validation.
If required configuration is missing, continuing is usually incorrect and leads to harder-to-debug failures later. Fail-fast panics produce a single, clear error early.
If you need a Result-based API (e.g. for a long-running library), consider implementing a thin wrapper around this macro,
or see the "Alternative API" idea in the crate documentation.
Does it load .env files?
No.
This crate intentionally does not include a .env loader to stay dependency-free.
If you want .env support, use a loader in your binary (e.g. dotenvy) and then call required!.
Is an empty string considered missing?
By default, yes (KEY="" is treated as missing).
Enable feature allow-empty to treat empty strings as present:
[]
= { = "0.1", = ["allow-empty"] }
MSRV
Rust 1.56 (Edition 2021).
License
MIT OR Apache-2.0