Skip to content

Proposal: Add the ability to do #[cfg(...)] { ... } at the top level of a file. #2312

Closed
@vext01

Description

@vext01

Hi,

I've never used this RFC repo, so apologies if I'm doing this wrong :)

In rust-lang/rust#47712 someone suggested an RFC would be required.

Occasionally I find I need to conditionally use several libraries. For example, to import data structures used in a conditionally compiled function.

#[cfg(debug_assertions)]    
use std::slice;    
#[cfg(debug_assertions)]    
use std::fs::File;    
#[cfg(debug_assertions)]    
use std::io::prelude::*;    
#[cfg(debug_assertions)]    
use std::convert::AsRef;    
#[cfg(debug_assertions)]    
use std::path::Path;    

Whereas, I'd like to write:

#[cfg(debug_assertions)] {
    use std::slice;
    use std::fs::File;    
    use std::io::prelude::*;    
    use std::convert::AsRef;    
    use std::path::Path;
}

This currently gives:

error: expected item after attributes
  --> src/lib.rs:53:24
   |
53 | #[cfg(debug_assertions)] {

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions