是否有所有cfg特性的列表?

36
Rust有在构建时检查配置的能力,例如使用#[cfg(target_os = "linux")]if cfg!(target_os = "linux") {...},其中target_os是一个特性。是否有所有(或至少常用的)可以在Rust中检查的特性列表?

请参考相关问题,了解有关属性的信息是否有任何详尽的标准属性列表?

3个回答

54

"条件编译"参考部分列出了必须定义的配置列表(截至Rust 1.14)

  • target_arch 的值包括:
    • x86
    • x86_64
    • mips
    • powerpc
    • powerpc64
    • arm
    • aarch64
  • target_os 的值包括:
    • windows
    • macos
    • ios
    • linux
    • android
    • freebsd
    • dragonfly
    • bitrig
    • openbsd
    • netbsd
  • target_family 的值包括:
    • unix
    • windows
  • unixtarget_family 的快捷方式
  • windowstarget_family 的快捷方式
  • target_env 的值包括:
    • gnu
    • msvc
    • musl
    • ""(空字符串)
  • target_endian 的值包括:
    • little
    • big
  • target_pointer_width 的值包括:
    • 32
    • 64
  • target_has_atomic 的值包括:
    • 8
    • 16
    • 32
    • 64
    • ptr
  • target_vendor 的值包括:
    • apple
    • pc
    • unknown
  • test
  • debug_assertions

感谢提供列表。遗憾的是,它没有任何选项来检测是夜间版还是稳定版。 - Bogdan Mart
1
@BogdanMart 你可能会对这个RFC感兴趣。 - euclio
1
这种方式也能指定 Web Assembly 吗? - pfincent
请注意,Bitrig不再受支持 - Jake Ireland

9
你也可以使用以下命令:rustc --print target-list
每个三元组的格式如下:{arch}-{vendor}-{sys}-{abi}
例如,三元组'arm-unknown-linux-gnueabihf'指的是:
  1. 架构:arm
  2. 供应商:未知。在这种情况下没有指定供应商。
  3. 系统:linux
  4. ABI:gnueabihf

2
这是有用的知识,但在构建cfg属性时并不实用,因为系统不一定与target_os相关(例如,系统是“darwin”,但需要将target_os指定为“macos”)。 - Jake Ireland

3
请参考https://internals.rust-lang.org/t/all-the-rust-features/4322,查看完整的 Rust 特性列表。
需要注意的是,其中一些或大多数特性不会稳定下来,因此只能在夜间版本中使用,并且在它们被稳定或停用之前,它们可能会受到破坏性的改进或升级。
Rust 夜间版中的特性是适者生存。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接