This seems like the sort of thing features are for. Just define a separate feature for enabling each conditional part of the code, and when you build for a given environment, pass the relevant features to --features.
Yes, it does indeed disambiguate the different environments but is not a clean solution.
This is because, fundamentally, cargo features are meant as a mechanism for users of library crates to determine what codes they want to include from the upstream crate into theirs.
A #[cfg(PROD)] or so would make a much cleaner solution for binary crate and my question is: how can one achieve this?
Sorry for reviving an old question because I am revisiting it after some time, just curious if there is any other way to 'pass' rustflags (e.g. --cfg PROD) at build time via the cargo CLI? Looks like currently, the only way is to go through the .cargo/config.toml.