Env! vs env::var

I discovered an issue recently trying to use CARGO_PKG_VERSION. I had env!("CARGO_PKG_VERSION") but wanted to move the string to a const &str 'static. Well env! won't take a 'static &str. So I decided to use env::var. Well when running my app as a binary standalone CARGO_PKG_VERSION isn't available under env::var but is when using env!. This is somewhat annoying and believe there should be at least some consistency here?

The macro resolves the environment variable during compile time, while the function checks the environment variables of the running program. They are not interchangeable.

4 Likes

Rather than moving "CARGO_PKG_VERSION" to a constant, you could store the environment variable itself

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
3 Likes

Great idea!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.