How to pass configurations to `cargo` such that `#[cfg(config)]` will work?

There are documentations on using to print out cargo::rustc-check-cfg.


My question is:

How to pass arguments to cargo, like cargo build --cfg env=1, such that the following will work:

fn main() {
    println!("This is `env` = `1`");
    println!("This is `env` = `2`");

Thank you.

cfg flags are passed to rustc either through the RUSTFLAGS environment variable, or with config.toml.

For example:

RUSTFLAGS='--cfg env=1' cargo build
1 Like

Thank you for your reply.

Is it possible to pass them directly through cargo?