easy-envar
Easily retrieve, parse, and export environment variables directly within your build script (build.rs).
By loading from an .env file and exporting each variable through Cargo, you can reliably access the same values at compile time in your application code.
Examples
// build.rs
use Envar;
// main.rs
Error Handling
-
Missing
.envfile:
If the.envfile is not found,init()returns an error. In the example, the script prints a warning and exits. -
Missing environment variable:
If a defined variable is not present in the.envfile (or system environment),load()returns an error. -
Parsing errors:
When callingload(), the library attempts to parse the string into the specified type (String,bool, oru16). If parsing fails (e.g.,PORT=not_a_number), an error is returned.
Why Use easy_envar?
-
Compile-Time Guarantees:
By exporting environment variables through Cargo, you can useenv!in your code to ensure the variable is present at compile time, avoiding runtime surprises. -
Type Safety:
easy_envarenforces type parsing in yourbuild.rs. If something doesn’t parse (e.g., aboolisTRUEE), you’ll know early. -
Simple Integration:
The API is concise and straightforward, leveragingdotenvyfor file loading and standard Rust.parse()methods for parsing.