I want to set a top level const in a Rust file to the value of an environment variable. In something like nodejs which I'm more used to, I would do something like:
const my_const = process.env['ENV_VAR_NAME'] ? process.env['ENV_VAR_NAME'] : 'FALLBACK_VALUE'
How do I achieve such a thing with Rust?
I have tried many things, such as
pub const my_const: &str = option_env!("ENV_VAR_NAME").unwrap_or("FALLBACK_VALUE");
But this yields the error calls in constants are limited to constant functions, tuple structs and tuple variants
I have also tried
pub const my_const: &str = if !option_env!("ENV_VAR_NAME").is_none() { env!("ENV_VAR_NAME") } else { "FALLBACK_VALUE" };
However, this doesn't work because env! is a compile-time macro and generates a compile time error if the environment variable doesn't exist.