The EnvironmentMap struct provides a way to query environment variables present during the compilation of the project. It can retrieve standard Cargo environment variables (like CARGO_PKG_VERSION) or custom override variables.
To use overrides, prefix the environment variable name with BUILT_OVERRIDE_<CARGO_PKG_NAME>_. For example, if your package name is my-crate, you can override CARGO_PKG_VERSION by setting BUILT_OVERRIDE_my_crate_CARGO_PKG_VERSION in your environment.
// Initialize the map with current environment variables
let env_map = EnvironmentMap::new();
// Get a standard environment variable
if let Some(version) = env_map.get("CARGO_PKG_VERSION") {
println!("Version: {}", version);
}
// Get an override variable with a specific type
// Note: T must implement util::ParseFromEnv
if let Some(opt_level) = env_map.get_override_var::<String>("OPT_LEVEL") {
println!("Override Opt Level: {}", opt_level);
}