Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to group items that need the same attribute? [duplicate]

Tags:

rust

This is more a style question but still interesting. Is it possible to group things so the attribute is only there once?

For instance, in the following code I am using the same attribute three times:

#[cfg(target_os = "linux")]
extern crate nix;
#[cfg(target_os = "linux")]
extern crate libc;

#[cfg(target_os = "linux")]
use std::{
    mem,
};
like image 579
Juan Leni Avatar asked Aug 22 '18 08:08

Juan Leni


1 Answers

You can use a helper module to cfg the entire module and then reexport its contents:

#[cfg(target_os = "linux")]
mod linux {
    extern crate nix;
    extern crate libc;
    pub use std::{
        mem,
    };
}
#[cfg(target_os = "linux")]
use linux::*;

You still have to mention the cfg twice though.

like image 133
oli_obk Avatar answered Oct 18 '22 17:10

oli_obk