如何标记为条件编译使用语句? [重复]

问题描述 投票:0回答:1

是否有可能标志着某些包括只得到包括在相关的操作系统的?

例如,你可以这样做:

#[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上发现,在Windows上编译(同时仍使其保留将其现有的目标编译的能力 - 即Unix系统及WASM)。目前我正在运行到其中的一些文件中导入特定于平台的部分从std::os(例如use std::os::unix::io::IntoRawFd;),这最终打破在Windows上构建一个问题。

注:我使用的防锈稳定(1.31.1),而不是每晚。

syntax rust conditional-compilation
1个回答
2
投票

你正在寻找的语法是:

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

#[cfg(target_os = "windows")]
use std::os::windows::io::AsRawHandle;
© www.soinside.com 2019 - 2024. All rights reserved.