Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 在';还有别的吗;?_Rust - Fatal编程技术网

Rust 在';还有别的吗;?

Rust 在';还有别的吗;?,rust,Rust,#[cfg]helper非常模糊,文档也不是很完整,但是通过深入librustc,我得到了一个非常合理的所有可用配置目标的列表(target_os、target_family、target_arch、target_endian、target_word_size、windows、unix),当然您可以使用not(…)来指定组合 但是,我不知道如何使用“默认”实现 使用cfg有什么方法可以做到这一点吗 #[cfg(???)] <--- What goes here? fn thing {

#[cfg]helper非常模糊,文档也不是很完整,但是通过深入librustc,我得到了一个非常合理的所有可用配置目标的列表(target_os、target_family、target_arch、target_endian、target_word_size、windows、unix),当然您可以使用not(…)来指定组合

但是,我不知道如何使用“默认”实现

使用cfg有什么方法可以做到这一点吗

#[cfg(???)] <--- What goes here?
fn thing {
  panic!("Not implemented! Please file a bug at http://... to request support for your platform")
}

#[cfg(target_os = "mac_os"]
fn thing() {
  // mac impl 
}

#[cfg(target_os = "windows"]] 
fn thing() {
  // windows impl
}
这需要大量繁琐的复制和粘贴。这是唯一的办法吗

(恐慌是不好的,对吗?不要这样做?这是针对build.rs脚本的,在该脚本中,您应该而且必须恐慌,以将错误提高到最大值)

这需要大量繁琐的复制和粘贴。这是唯一的办法吗

从关于条件编译的文档和RFC判断,是的,这是唯一的方法。如果有办法指定:

#[cfg(other)]
fn thing {
这将增加解析
cfg
属性的复杂性,因为编译器需要知道只有在未定义
mac_os
windows
时才会编译
thing

还有,这个呢:

#[cfg(other)]
fn thing_some_other {
  panic!("Not implemented! Please file a bug at http://... to request support for your platform")
}

#[cfg(target_os = "mac_os"]
fn thing() {
  // mac impl 
}

#[cfg(target_os = "windows"]] 
fn thing() {
  // windows impl
}
换句话说,它们需要捆绑在一起,类似于C:

#ifdef WINDOWS
    // ...
#elif LINUX
     // ...
#else
     // ...
#endif

没有其他方法可以做到这一点
cfg(不是(任何(…,…))
是唯一的方法

就您的“任何其他”情况而言,对于特定的构建脚本情况,运行时死机是可以接受的,尽管它不会出现在任何其他情况下(顺便说一句,对于这个运行时版本,有
未实现!()
可以方便地进行存根,允许您忽略消息)

尽管如此,我还是倾向于选择显式编译时失败,通常是通过忽略它,但也可能(为了更简单地向用户指出问题所在),您可能会包含一些内容,如果cfg条件忽略它,则这些内容会很好,但如果不忽略它,则会导致编译失败,如下所示:

#[cfg(not(any(target_os = "windows", target_os = "mac")))]
fn thing() {
    sorry! (this function is unimplemented for this platform, please report a bug at X)
}

它在Windows和Mac上可以正常编译,但在Linux上不会编译,前提是周围没有
sorry
宏(并且内容标记化,这禁止在字符串和注释之外使用反斜杠)。还有其他更可靠的方法(例如,不带
let
)的
sorry=“message”
),但我已经被这一种可爱的方式吸引住了。

这不是macos吗?不是macos吗宏?
#[cfg(not(any(target_os = "windows", target_os = "mac")))]
fn thing() {
    sorry! (this function is unimplemented for this platform, please report a bug at X)
}