Rfcs: Blocks for items

Created on 5 May 2017  ·  7Comments  ·  Source: rust-lang/rfcs

It'd be really nice if we could do something like:

#[cfg(feature = "abc")]
{
    extern crate x;
    extern crate y;
    mod z;
    use z::{a, b, c};
}

Right now, we have to attach the attribute to every item, which is a bit redundant. We can put things in a module and then re-export them, but it'd be nice if we could do something like this instead.

T-lang

Most helpful comment

With https://github.com/alexcrichton/cfg-if you can make it look like:

#[macro_use] extern crate cfg_if;

cfg_if! {
    if #[cfg(feature = "abc")] {
        extern crate x;
        extern crate y;
        mod z;
        use z::{a, b, c};
    }
}

All 7 comments

With https://github.com/alexcrichton/cfg-if you can make it look like:

#[macro_use] extern crate cfg_if;

cfg_if! {
    if #[cfg(feature = "abc")] {
        extern crate x;
        extern crate y;
        mod z;
        use z::{a, b, c};
    }
}

@withoutboats Would anonymous modules do this? As something like

#[cfg(feature = "abc")]
mod {
    extern crate x;
    extern crate y;
    mod z;
    use z::{a, b, c};
}

I considered anonymous modules, but the main issue is that things like extern crate, etc. wouldn't semantically fit in a module. It could work, though.

@clarcharr it's actually a pattern, albeit an uncommon one, to put extern crates in submodules.

@ubsan wait, is that actually allowed? huh.

I think this is #2377 so I'm closing in favor of that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3442853561 picture 3442853561  ·  3Comments

burdges picture burdges  ·  3Comments

torkleyy picture torkleyy  ·  3Comments

3442853561 picture 3442853561  ·  3Comments

clarfonthey picture clarfonthey  ·  3Comments