如何标记使用语句进行条件编译?

时间:2019-02-04 12:20:26

标签: syntax rust conditional-compilation

是否可以将某些包含标记为仅包含在相关操作系统中?

例如,您可以执行以下操作吗?

#[cfg(unix)] {
    use std::os::unix::io::IntoRawFd;
}
#[cfg(windows)] {
   // https://doc.rust-lang.org/std/os/unix/io/trait.AsRawFd.html  suggests this is equivalent?
   use std::os::windows::io::AsRawHandle;
}

尝试编译以上代码会给我带来语法错误(即error: expected item after attributes)。

我正在尝试修补在GitHub上找到的Rust项目以在Windows上进行编译(同时仍使其能够在其现有目标(即Unixes和WASM)上进行编译)。目前,我遇到了一个问题,其中某些文件从std::os(例如use std::os::unix::io::IntoRawFd;)导入特定于平台的部分,最终破坏了Windows的构建。

注意:我使用的是Rust Stable(1.31.1),而不是每晚使用。

1 个答案:

答案 0 :(得分:1)

您要查找的语法是:

#[cfg(target_os = "unix")]
use std::os::unix::io::IntoRawFd;

#[cfg(target_os = "windows")]
use std::os::windows::io::AsRawHandle;
相关问题