How do you run cargo with custom cfg options?

I know that rustc has the --cfg=myoption flag but that doesn't seem to be possible with cargo.

// Not a feature.  Mutually exclusive code.
#[path = ""]
mod mymod;

#[path = ""]
mod mymod;

I want to quickly run cargo test, check, run on either version during development, and without having to keep reseting RUSTFLAGS. How? Thanks.

.cargo/config can set RUSTFLAGS too, but there is no better option. I suspect it's on purpose, because you're supposed to just use additive features, and not hack around it.


Why not #[cfg(feature = "alternative")] and #[cfg(not(feature = "alternative"))]?

If there are more dimensions, you can check them in a build script. You could also choose your own environment variable to be separate from RUSTFLAGS, and then have the build script output cargo:rustc-cfg=alternative for the non-feature way.

1 Like