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

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

References

My question is:

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

fn main() {
    
    #[cfg(env="1")]
    println!("This is `env` = `1`");
    
    #[cfg(env="2")]
    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?

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.