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.
ie:

// Not a feature.  Mutually exclusive code.
#[cfg(alternative)]
#[path = "alternative.rs"]
mod mymod;

#[cfg(not(alternative))]
#[path = "mainway.rs"]
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.

2 Likes

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