Use and mod with rename

Use case, conditional compilation where a dummy module is included if a feature is off.

# [cfg (somefeature)]
mod foo;
# [cfg (not (somefeature))]
mod dummyfoo as foo;

It would be nice if this worked, but if it doesn't, that's OK.

Possible workaround: Are

pub mod foo;

and

mod foo;
pub use foo;

equivalent?

If so, the workaround is

# [cfg (somefeature)]
pub mod foo;
# [cfg (not (somefeature))]
mod dummyfoo;
# [cfg (not (somefeature))]
pub use dummyfoo as foo;

Or is there a better way?

#[cfg_attr(not(somefeature), path = "dummyfoo.rs")]
mod foo;
9 Likes

Alternatively

#[cfg(not(somefeature), path = "dummyfoo.rs")]
mod foo;

#[cfg(somefeature)]
mod foo;

Or

mod dummyfoo;
mod foo;

#[cfg(somefeature)]
use foo::*;

#[cfg(not(somefeature))]
use dummyfoo::*;

etc...

4 Likes
#[cfg(not(somefeature), path = "dummyfoo.rs")]

Oh, that's convenient. Thanks. Found it in the documentation at

https://doc.rust-lang.org/reference/items/modules.html#the-path-attribute