I'm trying to load passwords and sensitive data from the system's environment when my service starts up. I've tried a number of different ways but can't seem to figure out the right way to do this in Rust.
const PASSWORD: String = var("PASSWORD").unwrap();
Doesn't work because method calls in constants are limited to constant inherent methods
. The same applies to static
(except the error says statics, obviously).
The other way I've seen to do it is
const PASSWORD: &'static str = env!("PASSWORD");
But the problem with that is it will be defined at compile time as env!
is a macro (at least that is my understanding).
I also considered simply wrapping the call to var("...").unwrap()
in a function but that solution doesn't really sit right with me, and would also allow the values to change throughout runtime AND not validate them when the service starts.
As you can tell I'm new to Rust. I'd really appreciate if in your answer you could not just explain how to load the const/static at runtime but also explain why what I'm doing is dumb and wrong :)