How to check if module is declared?

Hi there! I'm working in a project and I'm dealing with conditional build flags.

We're trying to bring unstable features from a crate when doing development. We just need to pass a flag which is done now, however, when that happens, we need to change a line a code.

This is my approach in other words: If module_a exists then call module_a::init(), else module_b::init()

In that example, module_a is the module we need to call if we pass a RUSTFLAG to rustc.

Is my approach wrong? Maybe I'm doing what I'm used to do in python. Thanks!

p.s: I'm new here and with Rust

You can't use an if-statement for these things like you would in Python because one of the branches will always be referencing a module that doesn't exist. Which is a compile error.

Instead, I would use #[cfg (...)] and the condition that originally decides which module to enable. Then I would wrap it in some sort of facade (e.g. a function or type definition) so I don't need to infect the rest of my code with conditional compilation.

As long as you set CI up to build your crate with the flag enabled and then it recompiled with the feature disabled, you will know you did the conditional compilation correctly.

1 Like

Interesting, thanks for your suggestion.

I included some lines with your idea, I hope the project I'm working can accept this :slight_smile: ```

fn initialize_logger() {
    debug!("Console_subscriber plugin enabled");
1 Like

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.