Disable block using rustc?


If I want to disable a particular code block/function defn/etc from source, I can do something like this:

#[cfg(feature = "")]

Is there a way of disabling code from rustc itself? For example, something like rustc --disable foo.rs:32 (to disable the item starting at foo.rs line 32).

For context: I have a very large number of benchmarks in my crate, and as a result, doing a release build that includes tests (e.g., cargo bench) takes forever - about 4 minutes on my MacBook. I’m toying around with adding a flag to cargo test and cargo bench which only compiles the tests that are passed as an argument, and in order to do that, I need a way to not selectively disable certain pieces of code.


uhm… you are already using #[cfg(feature = "foo")], so you can just do cargo build --features=foo to enable that piece of code


Sorry, I was just using #[cfg(feature = "foo")] as an example. I want to be able to run this on code that I didn’t write and haven’t edited, so it’s not an option for me.


Then you are out of luck. You’ll need to open pull requests.


Ah oh well.


Fork, and do whatever changes you want in a project. Then put a reference to your git repository in Cargo.toml (perhaps behind a feature). You can later track updates by pulling from original repository if needed.

#[cfg(feature = "bench")]
extern crate example_bench as example;
#[cfg(not(feature = "bench"))]
extern crate example;

And dynamically disable getting original example as so.

example = { version = "version", optional = true }
example_bench = { git = "https://github.com/you/example_bench.git", optional = true }

bench = ["example_bench"]

[target.'cfg(not(feature = "bench"))'.dependencies]
example = "version"