How to disable the whole function-based example?

There are examples in my Rust project that apply only to specific functions .

I can ignore the main function with:

#[cfg(feature = "foo")]
fn main() {

But other function-dependent statements cause errors at startup cargo test. Therefore, I have to use several cfg attribute operators for functions and use instructions to disable code that depends on this function.

Is there a way to simply ignore the entire example file depending on the function configuration?

Also, since main is hidden without a function, it cargo testhas this error:

error: main function not found

So this is a bad decision.

+4
source share
1

#[cfg], main(), foo , main(), foo :

extern crate blah;
// other code which will still compile even without "foo" feature

#[cfg(feature = "foo")]
fn main() {
    use blah::OnlyExistsWithFoo;
    // code which requires "foo" feature
}

#[cfg(not(feature = "foo"))]
fn main() {
    // empty main function for when "foo" is disabled
}
+1

All Articles