Conditionally compile a module

Sometimes while refactoring a change in one of the modules has impact on every other module, because say a type has been changed or a function parameter has been changed etc. In this case instead of changing everything and then compiling at once is there a way i can compile and run unit tests for only that particular module, tweak it to hearts content and when all is right changed the other modules etc and run the complete/normal cargo build/test ? Of-course one of the ways could be to manually comment/uncomment the module listings in their corresponding root module that introduces them, but is there any other way directly from say cargo test --someflag -only_this_module or something ?

Explaining More:

say I have following modules: a, b, c, d, e, f, g, h where f depends only on g and h but rest of a, b, c, d, e depend on f (and maybe g and h but that should not matter). g and h ofcourse dont depend on anything from above. So if i make a change to f i want to compile only f and test it without changing all of them. Since f depends only on g and h i assume that if things are fine with these three then i should be able to test f even though a, b ... e are broken and crate will not compile if i do the normal cargo build/test . Is this possible?

2 Likes

Rust's unit of compilation is a crate, so you can't compile just individual modules.

Crosspost on stackoverflow: rust - Conditionally compile only one module at a time - Stack Overflow

If you really want this, you can add #![cfg(feature="activate_a")] before the mod a; and create your dependency tree in cargo with features: Page Moved

but the correct way would be to split your crate into multiple crates and let cargo do the dependency and recompilation math for you

2 Likes